单击 android 中通知中的按钮时自动隐藏通知
autohide the notification when clicked on the buttons in notification in android
我已经尝试了 setAutocancel
以及 FLAG_AUTO_CANCEL
,但是 none 这些工作,我在通知中有两个按钮可以打开两个 diff 活动..请帮助我
public void createNotification(View view) {
Intent yesIntent = new Intent(this, MyDialog.class);
yesIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
yesIntent.putExtra("NOTI_ID", NOTIFICATION_ID);
yesIntent.putExtra("ACTION", 1);
PendingIntent yespIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), yesIntent, 0);
Intent noIntent = new Intent(this, MyDialog.class);
noIntent.putExtra("ACTION", 0);
PendingIntent nopIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), noIntent, 0);
NotificationCompat.Builder noti = new NotificationCompat.Builder (this)
.setContentTitle("New Project Approval")
.setContentText("Project Description")
.setAutoCancel(true)
.setSmallIcon(R.mipmap.bell)
.addAction(R.mipmap.approve_ic, "Approve", yespIntent)
.addAction(R.mipmap.rejecticon, "Reject", nopIntent) ;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.build().flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_ID, noti.build());
}
您必须在 activity 中手动取消通知。将通知 ID 传递给您的 PendingIntent 以在您的 Activity 中检索它并通过 NotificationManager 取消相应的通知,例如:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(yourNotificationId);
一个很好的完整例子:How to dismiss notification after action has been clicked
之前我在 onCreate()
方法之外调用了 NotificationManager,但它不起作用。但是在 Whosebug threadr suggested by @bubu 之后,我通过在 onCreate()
中调用 NotificationManager 解决了它;
我已经尝试了 setAutocancel
以及 FLAG_AUTO_CANCEL
,但是 none 这些工作,我在通知中有两个按钮可以打开两个 diff 活动..请帮助我
public void createNotification(View view) {
Intent yesIntent = new Intent(this, MyDialog.class);
yesIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
yesIntent.putExtra("NOTI_ID", NOTIFICATION_ID);
yesIntent.putExtra("ACTION", 1);
PendingIntent yespIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), yesIntent, 0);
Intent noIntent = new Intent(this, MyDialog.class);
noIntent.putExtra("ACTION", 0);
PendingIntent nopIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), noIntent, 0);
NotificationCompat.Builder noti = new NotificationCompat.Builder (this)
.setContentTitle("New Project Approval")
.setContentText("Project Description")
.setAutoCancel(true)
.setSmallIcon(R.mipmap.bell)
.addAction(R.mipmap.approve_ic, "Approve", yespIntent)
.addAction(R.mipmap.rejecticon, "Reject", nopIntent) ;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.build().flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_ID, noti.build());
}
您必须在 activity 中手动取消通知。将通知 ID 传递给您的 PendingIntent 以在您的 Activity 中检索它并通过 NotificationManager 取消相应的通知,例如:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(yourNotificationId);
一个很好的完整例子:How to dismiss notification after action has been clicked
之前我在 onCreate()
方法之外调用了 NotificationManager,但它不起作用。但是在 Whosebug threadr suggested by @bubu 之后,我通过在 onCreate()
中调用 NotificationManager 解决了它;