我有一個創建/更新通知的方法:如何刪除通知操作
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);
這消除了動作按鈕,但是仍然留下通知屏幕,按鈕曾經是一片空白。是否有一種「正確」的方式來刪除通知中的操作,以及它使用的空間?
很好。爲我工作! – 2017-12-05 19:27:35