带按钮的Android通知点击移除通知

时间:2016-10-26 06:46:31

标签: android notifications android-pendingintent

我正在尝试用两个按钮发出通知。我可以显示通知。

我使用了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);

1 个答案:

答案 0 :(得分:1)

在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);