调用 addAction 时删除通知? [英] Remove Notification when addAction called?
问题描述
我在通知中添加了两个操作按钮,当我单击其中一个按钮时,它们会执行所需的操作,但通知仍保留在我的通知抽屉中.我知道可以在单击操作按钮时从通知抽屉中删除通知,因为这就是 Gmail 的功能.如果我点击主通知,它会打开应用并从通知抽屉中删除通知.
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.
这是我的代码片段:
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);
编辑 -正如 zionpi 指出的,我需要调用 notification.cancel();单击 addAction 按钮后删除通知.我只是将此方法添加到 PendingIntent 指向的类中.
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);
}
推荐答案
所以你要移除你的通知,初步是要有一个通知 id.
然后调用类似下面的方法来消除它.
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.
这篇关于调用 addAction 时删除通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!