简体   繁体   中英

Offline push notification - reminder feature

I'm interested in adding notifications to my application, in the form of reminders.

I've looked into notifications, but I have yet to find a way to do it without the app being open.

For example, I want to add a time setting, that will store the time in day to send the notification.

Then, each day (or w\\e), at the given time, I want the user to receive a notification, which he can click to enter the application.

How is it done?

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

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);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM