U
U
Username2016-07-10 17:32:52
Android
Username, 2016-07-10 17:32:52

How to make delayed notifications on android?

There is a schedule that the user can subscribe to, and you need to notify him 15 minutes before the start. I use AlarmManager, but the problem is that at any time a change in the schedule time can occur, or the user can change the time in the settings for which to warn him (for example, set not 15 minutes, but 5). When and the output time, the notification should change. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mitekgrishkin, 2016-07-10
@dalv_happy

Bind to requestCode. When you have the time change listener called, simply overwrite the alarmIntent with the appropriate id.
Those. store the alarmId in the requestCode field.
Something like this:

public void setAlarm(Context context, Integer alarmId, long milliseconds) {
      AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      Intent intent = new Intent(context, AlarmReceiver.class);
      intent.putExtra(ID, alarmId);
      PendingIntent alarmIntent = PendingIntent.getBroadcast(context, alarmId, intent, 0);

      Log.d("AlarmReceiver", "Started");
      alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
         SystemClock.elapsedRealtime() + milliseconds, alarmIntent);
   }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question