10

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?

Ravers
  • 988
  • 2
  • 14
  • 45
  • Did you try to add Notification.FLAG_AUTO_CANCEL? Does this flag has the behavior that you are expecting? – guipivoto Feb 06 '16 at 02:10
  • I updated the question with everything I have in notification.builder. Right now when I click "Close" button, its just does nothing. – Ravers Feb 06 '16 at 02:20
  • When you click those buttons, the intent is broadcasted to the system. Maybe, you need to handle the intent.. Try this asnwer: http://stackoverflow.com/q/16869777/4860513 – guipivoto Feb 06 '16 at 02:32
  • @GuilhermeP I edited my question, if you could, take a look again please – Ravers Feb 06 '16 at 20:22

3 Answers3

11

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.

guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • I did know also hahah During my tests, I accidentally insert the ID instead of "0" and it was working. While checking why, I notice that difference and voila!! Best Regards – guipivoto Feb 06 '16 at 21:30
  • but... if the notification id is retrieved with "id = intent.getIntExtra("notification_id", 1);" why would the change you suggest - which is a parameter change - make it work? – Marco Zanetti Apr 20 '16 at 14:21
  • Problem in this issue was happening during intent creation. Intent was always created with ID=0. So, getIntExtra("notification_id", 1) always return 0 and then, only last notification received was being canceled (even clicking on first notification)... – guipivoto Apr 20 '16 at 14:37
3

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.

Ragesh Ramesh
  • 3,470
  • 2
  • 14
  • 20
  • cool, but how do you retrieve the notification Id in the BroadcastReceiver in order to be sure you're canceling the right notification? – Marco Zanetti Apr 20 '16 at 14:22
0

You will need to run the following code after your intent is fired to remove the notification.

NotificationManagerCompat.from(this).cancel(null, notificationId);

https://stackoverflow.com/a/61483371/7308789

Houssin Boulla
  • 2,687
  • 1
  • 16
  • 22