我在我的通知中添加了两个操作按钮,当我单击它们中的任何一个时,它们会执行所需的操作,但通知仍保留在我的通知抽屉中。我知道单击操作按钮时可以从通知抽屉中删除通知,因为这就是 Gmail 的工作方式。如果我单击主通知,它会打开应用程序并从通知抽屉中删除通知。
这是我的代码 fragment :
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 指向的类中。
public static void CancelNotification(Context ctx, int notifyId) {
String s = Context.NOTIFICATION_SERVICE;
NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
mNM.cancel(notifyId);
}
最佳答案
所以你要去掉你的通知,前提是要有一个通知id。
然后调用类似于下面的方法来消除它。
public static void CancelNotification(Context ctx, int notifyId) {
String s = Context.NOTIFICATION_SERVICE;
NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
mNM.cancel(notifyId);
}
关于android - 调用 addAction 时删除通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18335037/