通知setAutoCancel(true)在点击操作
时不起作用我收到了包含操作的通知。当我点击通知时,它会从列表中删除。但是,当我单击Action它成功完成Action(即调用)时,但当我返回到通知列表时,它仍然存在。 AlarmReceiver的相关代码:
public class AlarmReceiver extends BroadcastReceiver {
Meeting meeting;
/**
* Handle received notifications about meetings that are going to start
*/
@Override
public void onReceive(Context context, Intent intent) {
// Get extras from the notification intent
Bundle extras = intent.getExtras();
this.meeting = extras.getParcelable("MeetingParcel");
// build notification pending intent to go to the details page when click on the body of the notification
Intent notificationIntent = new Intent(context, MeetingDetails.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.putExtra("MeetingParcel", meeting); // send meeting that we received to the MeetingDetails class
notificationIntent.putExtra("notificationIntent", true); // flag to know where the details screen is opening from
PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
// build intents for the call now button
Intent phoneCall = Call._callIntent(meeting);
if (phoneCall != null) {
PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);
int flags = Notification.FLAG_AUTO_CANCEL;
// build notification object
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Call In")
.setContentText(intent.getStringExtra("contextText"))
.setTicker("Call In Notification")
.setColor(ContextCompat.getColor(context, R.color.colorBluePrimary))
.setAutoCancel(true) // will remove notification from the status bar once is clicked
.setDefaults(Notification.DEFAULT_ALL) // Default vibration, default sound, default LED: requires VIBRATE permission
.setSmallIcon(R.drawable.icon_notifications)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(meeting.description))
.addAction(R.drawable.icon_device, "Call Now", phoneCallIntent)
.setCategory(Notification.CATEGORY_EVENT) // handle notification as a calendar event
.setPriority(Notification.PRIORITY_HIGH) // this will show the notification floating. Priority is high because it is a time sensitive notification
.setContentIntent(pIntent).build();
notification.flags = flags;
// tell the notification manager to notify the user with our custom notification
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
}
答案 0 :(得分:1)
我今天遇到了这个问题,发现FLAG_AUTO_CANCEL和setAutoCanel(true)都在点击通知时起作用, 但不适用于行动点击
如此简单,在目标服务或行动活动中,取消通知
Y
或者如果有更多通知
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancelAll();
答案 1 :(得分:0)
您已在两者中创建了两个待处理的意图使用,并且也更改了标记。
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);
PendingIntent phoneCallIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), phoneCall, PendingIntent.FLAG_UPDATE_CURRENT);
//更改此线
PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);
答案 2 :(得分:0)
使用此标志:
Notification.FLAG_AUTO_CANCEL
在里面:
int flags = Notification.FLAG_AUTO_CANCEL;
Notification notification = builder.build();
notification.flags = flags;
答案 3 :(得分:-1)
好的结果证明这已经是一个已知的问题,需要额外的代码才能完成(通过id保持对通知的引用)。不知道为什么API没有提供这个,因为这似乎很合乎逻辑。但无论如何,
请参阅此 answer in stackoverflow:
当您在通知管理器上调用通知时,您为其指定了一个ID - 这是您稍后可以用来访问它的唯一ID(来自通知管理器:
notify(int id, Notification notification)
要取消,请致电:
cancel(int id)
具有相同的ID。所以,基本上,你需要跟踪id或者可能将id放入你添加到PendingIntent里面的Intent的Bundle中?