2014-12-04 88 views
3

我有一個創建/更新通知的方法:如何刪除通知操作

private void createNotification(Boolean removeAction) { 
     getButtonPendingIntent().cancel(); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("My App") 
       .setOngoing(true) 
       .setPriority(NotificationCompat.PRIORITY_MAX); 

     NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); 
     bigTextStyle.setBigContentTitle("My App"); 

     if (removeAction) { 
      String msg = "Some message"; 
      bigTextStyle.bigText(msg); 
      builder.setContentText(msg); 
      // this will remove button but leaves an empty space where the button existed 
      builder.addAction(0, null, null); 
     } else { 
      String msg = "You are logged in. Slide down to logout."; 
      bigTextStyle.bigText(msg); 
      builder.setContentText(msg); 
      builder.addAction(R.drawable.some_icon, "My Button", getButtonPendingIntent()); 
     } 

     builder.setStyle(bigTextStyle); 

     // Creates an explicit intent for an Activity in your app 
     Intent resultIntent = new Intent(this, ResultActivity.class); 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
     stackBuilder.addParentStack(ResultActivity.class); 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
     builder.setContentIntent(resultPendingIntent); 
     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(100, builder.build()); 
    } 

有時我需要刪除我的通知創建的操作。如果我只是創建一個新的構建並通知沒有該操作的新事件,那麼操作按鈕仍然存在。

我加入這個代碼:

builder.addAction(0, null, null); 

這消除了動作按鈕,但是仍然留下通知屏幕,按鈕曾經是一片空白。是否有一種「正確」的方式來刪除通知中的操作,以及它使用的空間?

回答

0

我最近有同樣的問題。我能想到的唯一解決方案是在我更新之前先取消先前的通知。

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.cancel(100); 
notificationManager.notify(100, builder.build()); 
0

試試這個:

if (removeAction) { 
      String msg = "Some message"; 
      bigTextStyle.bigText(msg); 
      builder.setContentText(msg); 
      // this will remove button but leaves an empty space where the button existed 
      if(!builder.mActions.isEmpty()) builder.mActions.clear();// remove button (: 
     } else { 
      String msg = "You are logged in. Slide down to logout."; 
      bigTextStyle.bigText(msg); 
      builder.setContentText(msg); 
      builder.addAction(R.drawable.some_icon, "My Button", getButtonPendingIntent()); 
     } 

祝你好運!

2

從上次回答中花了很多時間。但我認爲對某人來說它可能有用。在NotificationCompat.Builder有一個ArrayList - mActions。我通過撥打myNotificationBuilder.mActions.clear()清除了這位Arraylist。它幫助我從通知中刪除操作按鈕而不重新創建它。

+0

很好。爲我工作! – 2017-12-05 19:27:35

0

我與尤金同意,在我的Notification類我建立這個方法:

/** 
* Cancel the notification by its id 
* @param contexto 
* @param id 
*/ 
public static void cancell(Context contexto, int id){ 
    NotificationManager notificacaoManager = (NotificationManager) contexto.getSystemService(Activity.NOTIFICATION_SERVICE); 
    notificacaoManager.cancel(id); 
} 
0

我認爲,如果你添加.setAutoCancel(true)到您的製造商可能做的工作

0

只是清除通知的動作和通知它。

notificationBuilder.mActions.clear(); 
notificationManager.notify(id, notificationBuilder.build());