我有一个创建/更新通知的方法:如何删除通知操作
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