离线推送通知 - 提醒功能

时间:2016-04-04 16:11:40

标签: android notifications

我有兴趣以提醒的形式向我的应用添加通知。

我已经查看了通知,但是我还没有找到一种方法,可以在不打开应用程序的情况下完成。

例如,我想添加一个时间设置,用于存储发送通知的时间。

然后,在给定时间的每一天(或者w / e),我希望用户收到通知,他可以点击该通知进入应用程序。

怎么做?

2 个答案:

答案 0 :(得分:1)

You need to write a AlarmManager based program to schedule your application's calls and then send Local Notifications to the UI with NotificationCompat.Builder
Have a look at the link below, it will get you started easily
http://developer.android.com/training/scheduling/alarms.html

答案 1 :(得分:0)

Even better, you may want to look into GCMNetworkManager which is another great way for scheduling tasks to happen at a specific time.

You shouldn't need the App to be running for either Alarms, or other scheduled events to occur. There are few different options for generating timed events to trigger the notification.

Here is an example, you would set the PendingIntent to start you notification:

    private void setAlarm() {
        final AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        final Intent i = new Intent(this, MyNotifService.class);
        final PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        am.cancel(pi);

        final Calendar cal = Calendar.getInstance();
        final String notifTime = prefs.getString("notif_time", "07:30");
        final String[] times = notifTime.split(":");
        final int hour = Integer.parseInt(times[0]);
        final int min = Integer.parseInt(times[1]);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, min);

        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pi);
  }