首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >棒棒糖:点击时提示通知被取消。

棒棒糖:点击时提示通知被取消。
EN

Stack Overflow用户
提问于 2014-11-06 18:19:28
回答 2查看 1.4K关注 0票数 4

我已经找了很长一段时间了,但找不到答案。我的应用程序用Notification.PRIORITY_HIGH显示一个通知,这会使它在Lollipop上显示为提醒通知。

问题是,当单击通知本身(即启动其contentIntent)时,通知将自动清除,即使没有设置Notification.FLAG_AUTO_CANCEL,并且通知具有Notification.FLAG_NO_CANCEL集。我尝试过各种标志组合,包括Notification.FLAG_ONGOING_EVENT,但是行为保持不变。

我希望通知变成“正常”通知,而不是被取消.有什么办法解决这个问题吗?医生对这个问题一点也不清楚.

复制代码:

代码语言:javascript
复制
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());
}

编辑:我注意到当应用程序在前台发布通知时,通知变成了常规通知,就像我所期望的那样。将提示通知移开(不管当前的前台应用程序如何)也会产生定期的通知。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-03 16:04:01

现在,我提出了以下解决方案:

  1. 向contentIntent添加一个额外的,指示它是从通知中启动的。
  2. 在已启动的Activity中检查是否有多余的
  3. 如果存在额外的通知,请重新发布通知,但要确保它不会成为提醒通知。

代码:

代码语言:javascript
复制
@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());
}
票数 2
EN

Stack Overflow用户

发布于 2014-12-03 16:03:04

我怎么能想象到一个简单的技巧,就是说您的contentIntent执行任何操作,只是再次发送相同的通知,声明为PRIORITY_DEFAULT或其他什么的。当然,通过使用相同的notyId

几天前我也有同样的问题..。关键在于,谷歌有意采取这种行为,这意味着,如果你想在向PRIORITY_HIGHMAX提供建议时宣布你的通知是重要的,它会说这是一个紧急事件,需要立即处理。因此,在这种情况下,用户只能通过滑动左边或右边(通知不会出现在通知抽屉中)或单击通知本身启动contentIntent (因为您的任天堂操作而导致通知消失)来关闭它。

如果有办法避免这种行为,对我来说将是新的。

希望我能帮上忙

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26786589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档