Notification is not being dismissed (Android)
Asked Answered
S

4

5

Notification setAutoCancel(true) doesn't work if clicking on Action

I have a notification with an action within it. When I tap on the notification it gets removed from the list. However, when I click on the Action it successfully completes the Action (namely makes a call), but when I return to the list of notifications, it remains there. Relative code of the AlarmReceiver:

public class AlarmReceiver extends BroadcastReceiver {
Meeting meeting;

/**
 * Handle received notifications about meetings that are going to start
 */
@Override
public void onReceive(Context context, Intent intent) {

    // Get extras from the notification intent
    Bundle extras = intent.getExtras();
    this.meeting = extras.getParcelable("MeetingParcel");

    // build notification pending intent to go to the details page when click on the body of the notification
    Intent notificationIntent = new Intent(context, MeetingDetails.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    notificationIntent.putExtra("MeetingParcel", meeting);      // send meeting that we received to the MeetingDetails class
    notificationIntent.putExtra("notificationIntent", true);    // flag to know where the details screen is opening from

    PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    // build intents for the call now button
    Intent phoneCall = Call._callIntent(meeting);
    if (phoneCall != null) {

        PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);

        int flags = Notification.FLAG_AUTO_CANCEL;

        // build notification object
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        Notification notification = builder.setContentTitle("Call In")
                .setContentText(intent.getStringExtra("contextText"))
                .setTicker("Call In Notification")
                .setColor(ContextCompat.getColor(context, R.color.colorBluePrimary))
                .setAutoCancel(true)                    // will remove notification from the status bar once is clicked
                .setDefaults(Notification.DEFAULT_ALL)  // Default vibration, default sound, default LED: requires VIBRATE permission
                .setSmallIcon(R.drawable.icon_notifications)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(meeting.description))
                .addAction(R.drawable.icon_device, "Call Now", phoneCallIntent)
                .setCategory(Notification.CATEGORY_EVENT)   // handle notification as a calendar event
                .setPriority(Notification.PRIORITY_HIGH)    // this will show the notification floating. Priority is high because it is a time sensitive notification
                .setContentIntent(pIntent).build();

        notification.flags = flags;

        // tell the notification manager to notify the user with our custom notification
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notification);
    }
  }
}

enter image description here

Sholes answered 2/6, 2016 at 15:5 Comment(1)
Async - did you find the solution of your this issue.. Currently i am facing the same issue?Louvain
S
2

Ok turns out it's a known problem already, and it needs extra code to be done (keeping reference to notification through id). Have no clue why API does not provide this, as it seems very logical to do. But anyways,

see this answer in stackoverflow:

When you called notify on the notification manager you gave it an id - that is the unique id you can use to access it later (this is from the notification manager:

notify(int id, Notification notification)

To cancel, you would call:

cancel(int id)

with the same id. So, basically, you need to keep track of the id or possibly put the id into a Bundle you add to the Intent inside the PendingIntent?

Sholes answered 3/6, 2016 at 9:40 Comment(0)
A
2

use this flag:

Notification.FLAG_AUTO_CANCEL inside this:

int flags = Notification.FLAG_AUTO_CANCEL;
Notification notification = builder.build();
        notification.flags = flags;

Documentation

Admiral answered 3/6, 2016 at 9:17 Comment(4)
i updated the code like you suggested, but still no luck.. pls see my edited code in the question (is it correct?)Sholes
@Sholes , yes. Have to tryed to use a very-very simple notification to test?Admiral
ok, turns out this is a known issue already, I will post link to other stackoverflow question about thisSholes
@Sholes , and have you tried to reboot your device?Admiral
S
2

Ok turns out it's a known problem already, and it needs extra code to be done (keeping reference to notification through id). Have no clue why API does not provide this, as it seems very logical to do. But anyways,

see this answer in stackoverflow:

When you called notify on the notification manager you gave it an id - that is the unique id you can use to access it later (this is from the notification manager:

notify(int id, Notification notification)

To cancel, you would call:

cancel(int id)

with the same id. So, basically, you need to keep track of the id or possibly put the id into a Bundle you add to the Intent inside the PendingIntent?

Sholes answered 3/6, 2016 at 9:40 Comment(0)
L
1

I faced this problem today, and found that FLAG_AUTO_CANCEL and setAutoCanel(true), both work when click on notification, but not work for action click

so simply, in the target service or activity of action, cancel the notification

NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancelAll();

or if have more notification

manager.cancel(notificationId);
Lancelot answered 4/4, 2018 at 7:34 Comment(0)
K
0

You have created two pending intent use in boths and change Flag too.

  PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);

  PendingIntent phoneCallIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), phoneCall, PendingIntent.FLAG_UPDATE_CURRENT);

// CHANGE TO THIS LINE

   PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

   PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);
Khaddar answered 2/6, 2016 at 15:13 Comment(2)
sorry, that, didn't helpSholes
still does not cancel the notification :(Sholes

© 2022 - 2024 — McMap. All rights reserved.