Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss current notification on Action clicked

I have a custom notification with a action button:

public class NotificationReceiver extends com.parse.ParsePushBroadcastReceiver {
    @Override
    public void onPushReceive(Context context, Intent intent) {

    ...

    NotificationActivity notification = new NotificationActivity();
    notification.createNotification(context, message, notificationId);

    Log.i("tag", "Notification Received!");
}

public class NotificationActivity {

    public int notificationId;

    public void createNotification(Context context, String message, String studentId, String notificationId){
         this.notificationId = Integer.parseInt(notificationId);

         // manager
         NotificationManager notificationManager = 
         (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

         // notification
         Notification.Builder mBuilder = new Notification.Builder(context);
         mBuilder.setContentTitle("My Title");
         mBuilder.setContentText(message);
         mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
         mBuilder.setAutoCancel(true);
         mBuilder.setStyle(new Notification.BigTextStyle()
             .bigText(message));

         // cancel intent
         Intent cancelIntent = new Intent(context, CancelNotification.class);
         Bundle extras = new Bundle();
         extras.putInt("notification_id", this.notificationId);
         cancelIntent.putExtras(extras);
         PendingIntent pendingCancelIntent = 
             PendingIntent.getBroadcast(context, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;
         mBuilder.addAction(R.drawable.notification_close, "Fechar", pendingCancelIntent);

        // notify
        Notification notification = mBuilder.build();
        notificationManager.notify(Integer.parseInt(notificationId), notification);
    }

    public static class CancelNotification extends BroadcastReceiver {

        private int id;

        @Override
        public void onReceive(Context context, Intent intent) {
             id = intent.getIntExtra("notification_id", 1);
             NotificationManager notificationManager =
                  (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
             notificationManager.cancel(id);
        }
    }
}

I want to cancel the notification which I clicked the action button "Close".

I know that I need the id of the notification to cancel it, but the way I did the code, when I click the "Close" button and create the class CancelNotification which extends BroadCastReceiver I'm getting the notification ID of the last notification, and so, is closing the last notification even if I click on the first notification I created.

What I could be doing wrong?

like image 608
Ravers Avatar asked Feb 06 '16 02:02

Ravers


People also ask

How do you dismiss a notification?

To dismiss a notification, touch it and swipe left or right. Tap the dismiss icon to dismiss all notifications. On newer versions of Android, you can manage some notifications from the lock screen. Double-tap a notification to open the app or swipe left or right to dismiss the notification.

How do I know if my Android notification was dismissed?

You can see your Android notification history on Android 11 through the Settings app. You first need to turn on Notification history in your phone's notification settings. You will then be able to see all the alerts you dismissed in the past 24 hours.


2 Answers

It is always better to use a Notification builder. Heres an example:

    NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(this);
    mBuilder.setContentTitle("Your title");
    mBuilder.setOnlyAlertOnce(true);
    mBuilder.setAutoCancel(true);
    mBuilder.setContentText("main content")
    mBuilder.setSubText("subtext")

Next you will have create an intent to which activity you want to open on notification clicked

    intent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

Then create your notification manager

    notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = mBuilder.build();
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(notificationID, notification);

notificationID can be any integer value. Using this type gives you the advantage of always following android norms for notifications.

like image 69
Ragesh Ramesh Avatar answered Sep 19 '22 02:09

Ragesh Ramesh


I found it

You pendingIntent is always sending request code == 0;

Since you have multiple Notifications, each one should use a different requestCode.

So, try to change:

From:

PendingIntent pendingCancelIntent = 
         PendingIntent.getBroadcast(context, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;

To:

PendingIntent pendingCancelIntent = 
         PendingIntent.getBroadcast(context, this.notificationId, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;

I tested your code here and it's working after the change I did.

like image 26
W0rmH0le Avatar answered Sep 21 '22 02:09

W0rmH0le