【发布时间】:2018-01-10 18:46:36
【问题描述】:
我研究过,我能找到的唯一答案是:
- 如何禁止下拉通知栏。
-
如何使用以下方法取消通知:
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(0);
这两个都不是我需要的,因为通知被取消了。我在通知中设置了两个操作:“关闭”和“打开活动”。顾名思义,点击两者后,上面的代码会执行并清除通知,但不会导致通知栏返回。这是必需的,特别是当第二个通知操作导致 Activity 启动时。
我在 Android Lollipop 和 Nougat 上进行了尝试,但通知栏都没有恢复。因此,如果有人可以告诉我这是否可能以及如何。 谢谢。
构建通知的代码:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder
.setSmallIcon(icon).setTicker(message).setWhen(when)
.setAutoCancel(true).setContentTitle("Kindly record your Voice")
.setColor(Color.RED);
notificationIntent = new Intent(this,ReminderReceiver.class);
notificationIntent.setAction("Record");
PendingIntent pendIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.common_google_signin_btn_icon_dark, "Record", pendIntent1);
notificationIntent2 = new Intent(this, ReminderReceiver.class);
notificationIntent2.setAction("Dismissed");
PendingIntent pendIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 1, notificationIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.starticon, "Dismiss", pendIntent2);
notificationIntent3 = new Intent(this, CancelReceiver.class);
PendingIntent pendIntent3 = PendingIntent.getBroadcast(getApplicationContext(), 1, notificationIntent3, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setDeleteIntent(pendIntent3);
notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, notification);
【问题讨论】:
-
你在通知生成器中尝试过 .setAutoCancel(true) 吗?
-
是的,我有。没用。
-
您是否在活动中使用此代码? NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(0);为什么要为所有通知设置相同的通知 ID,您是否希望只显示一个通知?
-
是的。它是一个通知,包含所有这些单独的操作。另外,取消的代码是写在广播接收器中的。
-
在任何操作上单击代码都应该在内部工作,所以不需要它。无论如何,您将通知 id 设置为 1 (notificationManager.notify(1, notification);),但您正在尝试关闭 id 为 0 的通知 (notificationManager.cancel(0);)。尽量不要使用删除意图(除非您需要知道通知因其他原因被取消)或使用相同的通知 ID。
标签: android android-notifications