Delete scheduled notification

I plan events and create a notification for it weekly or daily. A notification is displayed at the right time.

Problem I want to delete a notification after deleting a scheduled event. But I get a notification even if the event is deleted. I tried to delete the Waiting for Intent by passing the same unique_id ( eventId ) that I used to create this intent, but did not delete it. Please find my code below:

create 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);
            }
        }

Class of service

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();

Delete 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 save a list of all pending intentions, I save it to arrayList along with my unique id:

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

To delete a notification:

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

. , .

+4
1

PendingIntent Intent, Intent ( Intent, getBroadcast(), ). :

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();
+4

All Articles