5

I am making an app where the user can set an alarm based on GPS location. I only want 1 alarm to be active at any one time. So, when the user sets a 2nd alarm, I want to cancel the notification for the 1st alarm (then set a new notification for the 2nd alarm).

Right now, my notifications continue to stack up (as in I can't delete them, so they are all active). Here is my code where I am trying to delete the alarm and notification(s):

// Stop the location alarm activity
Intent intentAlarmService_delete = new Intent(v.getContext(), AlarmService.class);
stopService(intentAlarmService_delete); // I think this calls onDestroy() in AlarmService class ...

mNtf = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNtf.cancelAll();

Intent alarmIntent2 = new Intent(getApplicationContext(), OneTimeAlarmReceiver.class);
PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(getApplicationContext(), PENDING_INTENT_REQUEST_CODE1, 
    alarmIntent2, PendingIntent.FLAG_CANCEL_CURRENT);
pendingIntentAlarm.cancel();

This is the onDestroy() function in my AlarmService.class (I'm not really sure when this is called...)

public void onDestroy(){
    super.onDestroy();
    mNtf.cancel(NOTIFICATION_ID1);

    Intent alarmIntent = new Intent(getApplicationContext(), OneTimeAlarmReceiver.class);

    PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(getApplicationContext(), PENDING_INTENT_REQUEST_CODE1, 
        alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    pendingIntentAlarm.cancel();

    Intent intentAlarmService = new Intent(getApplicationContext(), AlarmService.class); 
    stopService(intentAlarmService); 

    mNtf.cancel(NOTIFICATION_ID1);
    mNtf.cancelAll();
}

Then, this is how I am setting a new alarm and notification:

Intent intentAlarmService2 = new Intent(v.getContext(), AlarmService.class);
startService(intentAlarmService2);

By the way, my AlarmService.class is working for sure.

Thanks in advance.

Yasir Malang
  • 835
  • 3
  • 10
  • 15

2 Answers2

7

First, get rid of getApplicationContext(). You almost never need it and it is frequently the wrong choice. Replace it with this, since whatever you are calling getApplicationContext() on is a Context.

In the code you have listed, you never raise a Notification. Hence, it is difficult to help you figure out why you are getting more than one. Calling cancelAll() on the NotificationManager should get rid of all outstanding notifications from your application.

My best guess is that onDestroy() is not being called on your service. That would occur if something else is keeping the service in memory (e.g., you have an active bound connection to it via bindService()). Or, possibly, you have something strange in your <service> element in the manifest (e.g., an unnecessary android:process attribute) that is fouling up the NotificationManager cancel() operation.

Kushal
  • 8,100
  • 9
  • 63
  • 82
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hmm, I checked all the things you suggested, but none of them work... (I can't use "this" because I seem to get an error - this is probably cause all that code is in a "public void onClick(View v)" function.) + (i'm not using onBind(); ) + ( no strange stuff in ) – Yasir Malang Aug 13 '10 at 00:21
  • I am going to describe exactly what is happening: I try to call cancelAll() on the NotificationManager, but: 1) I set a new notification (it shows up in the "notification window" - when you swipe finger from top of screen to bottom) 2) I delete the notification (it disappears from "notification window" 3) I set a new notification ***(it shows up in the "notification window", BUT then the previous notification (which was deleted) shows up on top of the newest notification (which was just set), and the 2 notifications keep flickering / alternating in the "notification window" – Yasir Malang Aug 13 '10 at 00:22
  • @Yasir Malang: "I can't use "this" because I seem to get an error" -- you are in an inner class, then, and need to use `MyOuterClass.this` (substituting your activity class name for `MyOuterClass`). – CommonsWare Aug 13 '10 at 00:27
  • `cancelAll()` was what I was looking for. – Roman Holzner Mar 11 '14 at 18:55
  • in my fault: I call cancelAll and can() after call unregisterReceiver(), just call before function unregisterReceiver() in service :)) – nobjta_9x_tq Jan 06 '19 at 09:30
2

You need to make sure you are always referencing the same instance of your NotificationManager. Different instances will produce different notifications. I'd recommend using a service to manage notifications.

http://developer.android.com/guide/topics/fundamentals.html

fredley
  • 32,953
  • 42
  • 145
  • 236
  • 7
    `NotificationManager` is merely a proxy to a system service running in another process. I have a sample project (http://github.com/commonsguy/cw-android/tree/master/Notifications/Notify1/) that does not work the way you describe. If you have evidence that `NotificationManager` works as you describe, please point me to it! – CommonsWare Aug 11 '10 at 19:02
  • Using different NotificationManager instance will not generate different notifications. It all depends on you Notification Id. – Rahul Rastogi May 13 '14 at 16:36