Reject current action notification

I have a custom notification with an 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 that I clicked on the Close action button.

I know that I need a notification ID to cancel it, but the way I did the code is when I click the Close button and create a CancelNotification class that extends BroadCastReceiver. I get the notification ID of the last notification, and therefore closes the last notification, even if I click on the first notification I created.

What can i do wrong?

+8
java android push-notification
source share
2 answers

I found him

You pendingIntent always send request code == 0;

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

So try changing:

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 checked your code here and it worked after the change I made.

+4
source share

It is always better to use the notification builder. Here is 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") 

Then you will create the intent for which you want to open the notification by clicking

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

Then create a 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 that you always follow Android standards for notifications.

+1
source share

All Articles