问题
I'm adding two action buttons to my notification, when I click either of them they perform the action desired however the notification remains in my notification drawer. I know it's possible to remove the notification from the notification drawer when an action button is clicked as that is how Gmail functions. If I click the main notification it opens the app and removes the notification from the notification drawer.
Here is a snippet of my code:
Intent completeIntent = new Intent(getApplicationContext(), MarkComplete.class);
completeIntent.putExtra("rowid", inrowid);
completeIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
PendingIntent markCompleteIntent = PendingIntent.getActivity(getApplicationContext(), inrow, completeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("text")
.setContentIntent(notificationReleaseIntent)
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true)
.addAction(R.drawable.complete, "Mark Complete", markCompleteIntent);
Edit - As zionpi pointed out I needed to call notification.cancel(); to remove the notification once the addAction button had been clicked. I simply added this method to my classes where the PendingIntent was pointing.
public static void CancelNotification(Context ctx, int notifyId) {
String s = Context.NOTIFICATION_SERVICE;
NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
mNM.cancel(notifyId);
}
回答1:
So you want to remove your notification, the preliminary is to have a notification id.
then invoke method similar as below to eliminate it.
public static void CancelNotification(Context ctx, int notifyId) {
String s = Context.NOTIFICATION_SERVICE;
NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
mNM.cancel(notifyId);
}
You may want to refer to this and this post alternatively.
来源:https://stackoverflow.com/questions/18335037/remove-notification-when-addaction-called