Harshil_Jain
Harshil_Jain

Reputation: 11

Notification is not getting Cancelled

Before saying anything please read whole question first.

Here is my code for creating a notification:-

public void showNotification(String Name, String Rate, int Image_Source, int PandP, int Repeat) {

    remoteview = new RemoteViews(getPackageName(), R.layout.notification_layout);
    context = this;
    pi = PendingIntent.getActivity(context, 0, new Intent(this, SongsListActivity.class), 0);
    P_and_P_I = new Intent("P_and_P_Clicked");
    R_I = new Intent("R_Clicked");
    N_I = new Intent("N_Clicked");
    P_I = new Intent("P_Clicked");
    P_and_P_PI = PendingIntent.getBroadcast(context, 1, P_and_P_I, 0);
    R_PI = PendingIntent.getBroadcast(context, 2, R_I, 0);
    N_PI = PendingIntent.getBroadcast(context, 3, N_I, 0);
    P_PI = PendingIntent.getBroadcast(context, 4, P_I, 0);

    notification = new NotificationCompat.Builder(this)
            .setContent(remoteview)
            .setPriority(2)
            .setOngoing(true)
            .setTicker(NameD.getText())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(NameD.getText())
            .setContentText(RateD.getText())
            .setContentIntent(pi)
            .setAutoCancel(false)
            .setCustomBigContentView(remoteview)
            .build();


    remoteview.setOnClickPendingIntent(R.id.P_and_P_N, P_and_P_PI);
    remoteview.setOnClickPendingIntent(R.id.Repeat_N, R_PI);
    remoteview.setOnClickPendingIntent(R.id.Next_N, N_PI);
    remoteview.setOnClickPendingIntent(R.id.Previous_N, P_PI);
    remoteview.setImageViewResource(R.id.Repeat_N, Repeat);
    remoteview.setImageViewResource(R.id.P_and_P_N, PandP);
    remoteview.setTextViewText(R.id.Name_N, Name);
    remoteview.setTextViewText(R.id.Rate_N, Rate);
    remoteview.setImageViewResource(R.id.Image_N, Image_Source);
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(111111, notification);


}

I try to cancel the notification by calling this method:-

@Override
protected void onDestroy() {
    super.onDestroy();
    notificationManager.cancelAll();
    if(mediaplayer != null)
    {
        mediaplayer.release();
        mediaplayer = null;
        mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
    }
}

I use this showNotification several times in the code. I am sure that onDestroy is being called.

In the begining, when the showNotification is called only once, and then if the app is being destroyed, the notification gets cancelled, but when showNotification gets called several times, even twice, then on destroying the app, it doesn't gets cancelled.

all variables are public throughout the program for all methods.

and yeah, their is only one notification being showed which gets updated everytime when i call showNotification.

What should i do so that the notification gets cancelled even if the method gets called several times?

Thanks in advance.

Upvotes: 1

Views: 567

Answers (2)

lucidbrot
lucidbrot

Reputation: 6156

I had the same symptoms. My problem turned out to be that I was calling showNotification on a sensor Event. These events triggered new notifications between cancelAll and the end of onDestroy.

The solution for me was to create a class-wide boolean stahp and only show the notification if !stahp. I set stahp to true before calling cancelAll

Upvotes: 0

Gary Bak
Gary Bak

Reputation: 4798

I don't know why that is happening, but have you tried the following to troubleshoot?

Try moving the cancelAll() into the onPause() and see if it works there.

Try grabbing the reference to the notification manager again in the onDestory()

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Try a cancel with the notification ID instead cancel(111111)

Just a suggestion, you should check to see if the notificationManager is null in the onDestory(). If a notification was never showing, it will throw a null pointer and any code afterwards will never run.

Update

If your app is APU 18 or greater, you can check to see if you have any active notifications before you attempt to post it again:

getActiveNotifications()

Upvotes: 1

Related Questions