2014-12-04 54 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());