'adding a alarm cancel button in the notification

I have been working on setting alarms using AlarmManager.

Set:

Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
                                getApplicationContext(), reqCode, myIntent, 
                                PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);

cancel:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
                                getApplicationContext(), reqCode, myIntent, 
                                PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.cancel(pendingIntent);

in my alarm receiver I want to be able to receive a notification which has a button that allows me to call cancelAlarm(). however I don't see the best approach to this and looking for the best and most reliable way of doing this.

I get the requestCodes so I am able tell which alarm to cancel



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source