Search code examples
javaandroidalarmmanager

Android AlarmManager not cancelling alarms correctly


I'm working on an app which will allow users to set multiple notifications, and cancel any notification they choose.

The problem I have is that when I cancel a pending intent using the AlarmManager it is cancelling all alarms. Each of my pending intents has it's own unique request code.

For instance I would call this to create the alarm and then delete:

setAlarm(5062, 1453269670) // Set alarm 1
setAlarm(5063, 1453774418) // Set alarm 2

cancelAlarm(5062) // Cancel alarm 1
cancelAlarm(5063) // Cancel alarm 2

Setting mutiple alarms always works, I can set as many as I like and they all produce the notification. However if I was to cancel alarm 1, it also cancels alarm 2.

I know that the pending intent has to be the same when setting and cancelling, and each pending intent has it's own unique request code, so I don't know why it's not working. I've spent many hours googling, but none of the suggested answers have helped me.

void setAlarm(int request_code, long alarm_time) {
    Intent intent = new Intent(context, NotificationReceiver.class);
    intent.putExtra("request_code", request_code);
    PendingIntent pendingIntent = PendingIntent.getService(context, request_code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarm_time, pendingIntent);
}

void cancelAlarm(int request_code) {
    Intent intent = new Intent(context, NotificationReceiver.class);
    intent.putExtra("request_code", request_code);
    PendingIntent pendingIntent = PendingIntent.getService(context, request_code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    pendingIntent.cancel();
    alarmManager.cancel(pendingIntent);
}

Solution

  • The answer is simple

    Intent firstIntent = new Intent(this, Receiver.class);
    intent.setAction("action 1");
    intent.putExtra("extra", "extra1");
    firstPendingIntent = PendingIntent.getBroadcast(this, 0, firstIntent, 0);
    
    Intent secondIntent = new Intent(this, Receiver.class);
    intent.setAction("action 2");
    intent.putExtra("extra", "extra2");
    secondPendingIntent = PendingIntent.getBroadcast(this, 0, secondIntent, 0);
    

    if you compare two PendingIntents created in ABOVE code they are NOT EQUAL

    But in code BELOW:

    Intent firstIntent = new Intent(this, Receiver.class);
    intent.setAction("action 1");
    intent.putExtra("extra", "extra1");
    firstPendingIntent = PendingIntent.getBroadcast(this, 0, firstIntent, 0);
    Intent secondIntent = new Intent(this, Receiver.class);
    intent.setAction("action 1");
    intent.putExtra("extra", "extra2");
    secondPendingIntent = PendingIntent.getBroadcast(this, 0, secondIntent, 0);
    

    if you compare two PendingIntents they gonna be EQUAL, so doesn't matter what you pass in putExtra method