Android 带按钮的通知点击删除通知
Android notification with buttons click remove notification
我正在尝试使用两个按钮进行通知。我可以显示通知。
我使用了 setOnClickPendingIntent,一旦我点击按钮我就无法删除通知。
我试过 notification.flags |= Notification.FLAG_AUTO_CANCEL;
并且 notificationManager.cancel(id);
它不起作用。请指导我我做错了什么。如果我点击 setContentIntent
它会被删除,但不会在按钮中点击。
这里是代码
PendingIntent pendingIntent;
Intent intent = new Intent(this, ChatListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("accept", "true");
pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
// Create remote view and set bigContentView.
RemoteViews expandedView = new RemoteViews(getApplicationContext().getPackageName(),
R.layout.notification_message_remote);
expandedView.setOnClickPendingIntent(R.id.btn_accept, pendingIntent);
Notification notification = new NotificationCompat.Builder(getApplicationContext())
//Vibration
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
//LED
.setLights(Color.RED, 3000, 3000)
//Ton
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setColor(getResources().getColor(R.color.colorAccent))
.setSmallIcon(R.drawable.icon)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(remoteMessage.getData().get("Title"))
// .setStyle(new NotificationCompat.BigTextStyle().bigText(mTitle))
.setContentTitle("Notification").build();
notification.bigContentView = expandedView;
// hide the notification after its selected
//notification.flags |= Notification.FLAG_AUTO_CANCEL;
//notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
notificationManager.cancel(0);
在你的 ChatListActivity.onCreate()
中试试这个
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//in this case, it should be 0
manager.cancel(notificationId);
//dismiss notification panel
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(it);
我正在尝试使用两个按钮进行通知。我可以显示通知。
我使用了 setOnClickPendingIntent,一旦我点击按钮我就无法删除通知。
我试过 notification.flags |= Notification.FLAG_AUTO_CANCEL;
并且 notificationManager.cancel(id);
它不起作用。请指导我我做错了什么。如果我点击 setContentIntent
它会被删除,但不会在按钮中点击。
这里是代码
PendingIntent pendingIntent;
Intent intent = new Intent(this, ChatListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("accept", "true");
pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
// Create remote view and set bigContentView.
RemoteViews expandedView = new RemoteViews(getApplicationContext().getPackageName(),
R.layout.notification_message_remote);
expandedView.setOnClickPendingIntent(R.id.btn_accept, pendingIntent);
Notification notification = new NotificationCompat.Builder(getApplicationContext())
//Vibration
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
//LED
.setLights(Color.RED, 3000, 3000)
//Ton
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setColor(getResources().getColor(R.color.colorAccent))
.setSmallIcon(R.drawable.icon)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(remoteMessage.getData().get("Title"))
// .setStyle(new NotificationCompat.BigTextStyle().bigText(mTitle))
.setContentTitle("Notification").build();
notification.bigContentView = expandedView;
// hide the notification after its selected
//notification.flags |= Notification.FLAG_AUTO_CANCEL;
//notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
notificationManager.cancel(0);
在你的 ChatListActivity.onCreate()
中试试这个NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//in this case, it should be 0
manager.cancel(notificationId);
//dismiss notification panel
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(it);