Search code examples
androidflagsandroid-pendingintentandroid-alarms

Only one alarm fired when multiple ones are set


My goal is to fire an alarm for every event from eventsDb.getFavouriteEvents() but only one is fired everytime.

In my opinion, it may be caused by setting PendingIntent.FLAG_UPDATE_CURRENT, however I need this flag to post Extras with an intent. Is there any way not to set this flag and still post Extras with the intent?

I'm storing PendinIntents in a Collection (pendingIntents) because I'm canceling them in onDestroy() of this Service class.

This is code from onCreate():

for(Event event : eventsDB.getFavouriteEvents()) {
  Intent intent = new Intent(AlarmReciever.ONCOMING_EVENT);
  intent.putExtra(Event.TITLE, event.getTitle());

  PendingIntent pendingIntent = PendingIntent.getBroadcast(EventAlarmService.this, 0, intent, 
    Intent.FLAG_GRANT_READ_URI_PERMISSION | PendingIntent.FLAG_UPDATE_CURRENT);
  alarmManager.set(AlarmManager.RTC_WAKEUP, event.getTime(), pendingIntent);

  pendingIntents.add(pendingIntent);
}

Thanks for help!


Solution

  • The PendingIntent will be overwritten on send if they share the same code. Changing the code for each intent on send should correct this problem.