2017-03-06 34 views
1

我想添加取消按鈕通知。 在點擊我想從通知欄中刪除我的通知。 有沒有辦法做到這一點,或者我正在做一個錯誤的方式。我如何取消通知使用mBuilder.addAction

noti=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     mBuilder=new NotificationCompat.Builder(this); 
     mBuilder.setSmallIcon(R.drawable.and); 
     mBuilder.setContentTitle("Downloading"); 
     mBuilder.setContentText("Downloading in progress"); 
     mBuilder.addAction(R.drawable.and,"cancil",); 
+0

你嘗試過讓我們知道了什麼? –

+0

取消 – 007

+0

後,我應該通過什麼待定的意圖,你想要自定義通知嗎?與取消按鈕 –

回答

0
Intent intent = new Intent(this, MainActivity.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT); 

//Button as addAction is deprecated use this one. 
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Cancel", pendingIntent).build(); 

Notification notification = new NotificationCompat.Builder(this) 
     .setSmallIcon(R.mipmap.ic_launcher) 
     .setContentText("Back to Application ?") 
     .setContentTitle("Amazing news") 
     .addAction(action) //add buton 
     .build(); 

//Send notification 
NotificationManager notificationManager = (NotificationManager) 
     this.getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify(1, notification);