megha
megha

Reputation: 301

Deleting the scheduled notification

I am scheduling events and creating the notification for the same on weekly basis or daily basis.The notification is being displayed at correct time.

Problem I want to delete the notification once the scheduled event is deleted. But I get the notification even if event is deleted. I tried deleting the Pending Intent by passing the same unique_id(eventId) that I have used for creating that intent, but its not deleting. Please find my code below:

creating notification

Intent intent = new Intent(this, MyReceiver.class);
        HABIT_NAME  = habitName.getText().toString();
        intent.putExtra("HABIT_NAME", habitName.getText().toString());

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), eventId, intent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        if(daysNo.size() == 7){           //alarm for every day
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,reminderLifeTS.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 
        }else{                            // specific days of week
            for(int i=0;i<daysNo.size();i++){
                int day = getWeek(daysNo.get(i));
                Calendar c = Calendar.getInstance();
                c.set(Calendar.DAY_OF_WEEK, day);
                c.set(Calendar.HOUR_OF_DAY, reminderLifeTS.get(Calendar.HOUR_OF_DAY));
                c.set(Calendar.MINUTE, reminderLifeTS.get(Calendar.MINUTE));    
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                        c.getTimeInMillis(),7*24*60*60*1000, pendingIntent);
            }
        }

Service class

mManager = (NotificationManager) this.getApplicationContext().
                   getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);

           Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

           intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

           PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
                   this.getApplicationContext(),0, intent1,PendingIntent.FLAG_ONE_SHOT);

           // megha
           NotificationCompat.Builder mBuilder =
                   new NotificationCompat.Builder(this)
                   .setSmallIcon(R.drawable.app_icon)
                   .setContentTitle("DharmaLife")
                   .setContentText(AddScheduleEventActivity.HABIT_NAME)
                   .setContentIntent(pendingNotificationIntent);

           Notification note = mBuilder.build();
           note.defaults |= Notification.DEFAULT_VIBRATE;
           note.defaults |= Notification.DEFAULT_LIGHTS;
           note.flags|= Notification.FLAG_AUTO_CANCEL;

           PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
           WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
                   PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
           wakeLock.acquire();

Deleting Notification

Intent intent = new Intent();
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), eventId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            pendingIntent.cancel();
            AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);

            am.cancel(pendingIntent);

EDIT to keep the list of all the pending intents created, I am saving it in arrayList along with its unique id:

notificationData.setAlarmManager(alarmManager);
notificationData.setPendingIntent(pendingIntent);
notificationData.setEventId(eventId);
LifeLiteral.NOTIFICATION_STACK.add(notificationData);

to delete the notification:

for(NotificationData i : LifeLiteral.NOTIFICATION_STACK){
    if(i.getEventId() == eventId){
    pendingIntent = i.getPendingIntent();
    alarmManager = i.getAlarmManager();
    alarmManager.cancel(pendingIntent);
    pendingIntent.cancel();
    }
}

but still notifications are not getting deleted. Please guide.

Upvotes: 4

Views: 2520

Answers (1)

David Wasser
David Wasser

Reputation: 95578

When attempting to delete the PendingIntent you aren't passing an Intent that matches the original Intent (because the Intent you pass to getBroadcast() is empty). Change your delete code to this:

Intent intent = new Intent(this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(),
    eventId, intent, 0);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
// Cancel the `PendingIntent` after you've canceled the alarm
pendingIntent.cancel();

Upvotes: 4

Related Questions