2016-10-26 9 views
1

Я пытаюсь сделать уведомление двумя кнопками. Я могу отображать уведомление.Уведомление Android с кнопками нажмите удалить уведомление

Я использовал 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); 
+0

Пробовали ли вы notificationManager.cancelAll(); –

+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); 
+0

Спасибо большое приятель ... его работало ... –