我已经搜索了相当长的一段时间,但是无法找到答案。我的应用程序使用Notification.PRIORITY_HIGH
显示通知,这会导致它在Lollipop上显示为悬浮通知。
问题在于,即使未设置Notification.FLAG_AUTO_CANCEL
并且通知已设置Notification.FLAG_NO_CANCEL
,单击通知本身(即启动其contentIntent
),通知也会自动清除。我尝试过各种标志组合,包括Notification.FLAG_ONGOING_EVENT
,但行为仍然相同。
我希望通知变成“普通”通知,而不是被取消...有任何解决方法吗?文档对此问题并不清楚...
重现代码:
private void showHeadsUpNotification()
{
final Notification.Builder nb = new Notification.Builder(this);
nb.setContentTitle("Foobar");
nb.setContentText("I am the content text");
nb.setDefaults(Notification.DEFAULT_ALL);
nb.setOngoing(true);
nb.setSmallIcon(android.R.drawable.ic_dialog_info);
nb.setContentIntent(PendingIntent.getActivity(this, 0, getIntent(), 0));
// Commenting this line 'fixes' it by not making it heads-up, but that's
// not what I want...
nb.setPriority(Notification.PRIORITY_HIGH);
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(0, nb.build());
}
编辑:我注意到当应用程序在前台时发布通知时,通知变成了普通的通知,就像我所期望的那样。无论当前前台应用是什么,滑动 Heads-up 通知都会产生一个普通通知。
PRIORITY_HIGH
。由于声音和/或振动也是使通知栏弹出的必要条件,我的方法只是禁用了第二个通知的这些功能。这样,它仍然是PRIORITY_HIGH
,但不会弹出通知栏。 - jclehner