我已经找了很长一段时间了,但找不到答案。我的应用程序用Notification.PRIORITY_HIGH显示一个通知,这会使它在Lollipop上显示为提醒通知。
问题是,当单击通知本身(即启动其contentIntent)时,通知将自动清除,即使没有设置Notification.FLAG_AUTO_CANCEL,并且通知具有Notification.FLAG_NO_CANCEL集。我尝试过各种标志组合,包括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());
}编辑:我注意到当应用程序在前台发布通知时,通知变成了常规通知,就像我所期望的那样。将提示通知移开(不管当前的前台应用程序如何)也会产生定期的通知。
发布于 2014-12-03 16:04:01
现在,我提出了以下解决方案:
Activity中检查是否有多余的代码:
@Override
protected void onResume()
{
super.onResume();
if (getIntent().getBooleanExtra("launched_from_notification", false)) {
showNotification(false);
getIntent().putExtra("launched_from_notification", false);
}
}
// If your Activity uses singleTop as launchMode, don't forget this
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
setIntent(intent);
}
private void showNotification(boolean showAsHeadsUp)
{
final Intent intent = getIntent();
intent.putExtra("launched_from_notification", true);
final Notification.Builder nb = new Notification.Builder(this);
nb.setContentTitle("Foobar");
nb.setContentText("I am the content text");
nb.setOngoing(true);
nb.setSmallIcon(android.R.drawable.ic_dialog_info);
nb.setContentIntent(PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
nb.setPriority(Notification.PRIORITY_HIGH);
// Notifications without sound or vibrate will never be heads-up
nb.setDefaults(showAsHeadsUp ? Notification.DEFAULT_ALL : 0);
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(0, nb.build());
}发布于 2014-12-03 16:03:04
我怎么能想象到一个简单的技巧,就是说您的contentIntent执行任何操作,和只是再次发送相同的通知,声明为PRIORITY_DEFAULT或其他什么的。当然,通过使用相同的notyId。
几天前我也有同样的问题..。关键在于,谷歌有意采取这种行为,这意味着,如果你想在向PRIORITY_HIGH或MAX提供建议时宣布你的通知是重要的,它会说这是一个紧急事件,需要立即处理。因此,在这种情况下,用户只能通过滑动左边或右边(通知不会出现在通知抽屉中)或单击通知本身启动contentIntent (因为您的任天堂操作而导致通知消失)来关闭它。
如果有办法避免这种行为,对我来说将是新的。
希望我能帮上忙
https://stackoverflow.com/questions/26786589
复制相似问题