如何跟踪通知以了解何时显示摘要通知 [英] How to keep track of notifications to know when to show a summary notification

查看:226
本文介绍了如何跟踪通知以了解何时显示摘要通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想模拟Gmail关于通知栏通知的应用程序行为,该行为符合建议的Android模式: http://developer.android.com/design/patterns/notifications.html

I want to emulate Gmail's app behavior regarding notification bar notifications, which complies with the recommended Android pattern: http://developer.android.com/design/patterns/notifications.html

当应用程序处于后台并且收到新电子邮件时,我会在通知栏中收到如下通知:

When the app is in background and I get a new e-mail I get a notification in the notification bar like this:

Line 1 : Jane Smith
Line 2 : Hi John, this is a sample message...

也就是说,特定于单个消息的通知,然后点击它会导致显示该特定电子邮件的屏幕.如果我通过刷卡或全部清除"来清除通知,那么当我收到一条新消息时,将收到另一条特定于消息的通知.但是,如果我不清除它,而又收到另一封电子邮件,则该通知将变成摘要通知,显示"2条新邮件",然后点击它会进入收件箱.

That is, a notification that is specific for a single message, and tapping it leads to a screen showing that specific e-mail. If I clear the notification either by swiping it or with "Clear all", then when I get a new message I will get another single message specific notification. But if I don't clear it and I get another e-mail then the notification will turn into a summary notification saying "2 New messages", and tapping it leads to the inbox.

我知道如何更新通知,问题是如何确定通知栏中是否仍存在哪些通知.答案不是那么简单,因为通知不会反映出我有多少未读消息,它必须反映出用户仍然无法通过点击通知或清除通知来确认哪些消息.

I know how to update a notification, the question is how do I figure out which notifications are still there in the notification bar, if any. The answer is not that simple because the notification will not reflect how many unread messages I have, it must reflect which messages are still not acknowledged by the user either by tapping the notification, or clearing it.

我是否应该通过保存我们启动的通知列表,被点击的通知(内容意图)和已清除的通知(删除意图)来跟踪通知?我认为这种方法的安全性不够安全……例如:如果由于我启动手机而清除了通知,会发生什么情况?我应该在哪里跟踪仍显示的通知?共享的偏好?

Should I keep track of notifications by keeping a list of notifications that we launched, the ones that were tapped (content intent) and the ones cleared (delete intents)? I don't think that approach is fail safe enough... for example: What happens if notifications get cleared because I boot my phone? Where am I supposed to keep track of notifications still showing? Shared preferences?

您通常如何解决此问题?

How do you usually solve this?

推荐答案

我只是遇到了同样的问题,我还查看了Gmail的适当行为.我首先研究了以下情形:

I just had the same issue and I also looked at Gmail for the appropriate behaviour. I first studied the following scenarios:

假设用户正在积极使用Gmail应用程序(不考虑与浏览器版本的任何交互),然后从中导航:

Assuming a user is actively using the Gmail app (disregard any interaction with the browser version), and then navigates away from it:

  1. 从那时起,如果用户收到新电子邮件,它将收到通知.
  2. 如果通知仍然存在,并且用户收到一封新电子邮件,则该通知将更新为摘要,其中包含自用户上次使用该应用以来的N封最新电子邮件的标题.
  3. 如果用户取消了该通知而又收到另一封电子邮件,则会发出另一个摘要通知.
  4. 如果用户导航回应用程序(通过单击通知或不单击通知),则所有现有的通知都将被关闭.

在API 18中,Android添加了支持,以使用

In API 18, Android added support to retrieve active notifications using NotificationListenerService, however that's too recent for the app I'm working on (min API 14), not to mention you can't rely on that to get the existing notification information if it has already been dismissed by the user.

因此,我找到的解决方案是保留有关已在本地发出的通知的信息,使用该信息创建单个通知或摘要,并在用户再次打开应用程序时清除所有内容.

So the solution I found was to persist information about the notifications already issued locally, use that information to create either a single notification or a summary, and clear everything when the user opens the app again.

为了保留通知信息,我创建了一个类似于以下内容的简单模型:

To persist the notification information, I created a simple model similar to the following:

public class NotificationBundle {
    private String mText;
    // Add any other relevant information about your notification here, 
    // particularly what you used to create your notification intent 
    // i.e. an item/message id to highlight, maybe?

    public String getText() {
        return mText;
    }

    public void setText(final String text) {
        mText = text;
    }
}

这个想法是为您发出的每个通知创建一个实例.

The idea is to create an instance of those for each notification you issue.

然后,您必须有一种方法来持久保存 NotificationBundle 对象的列表.我使用了 SharedPreferences 来做到这一点,但是您可以使用其他更适合您的方法(例如数据库表).为了将 List< NotificationBundle> 持久保存在 SharedPreferences 中,我使用了 Gson 将数组序列化为json,然后将其保存为字符串.假设您可以使用 SharedPreferences ,则可以在

Then you have to have in place a way to persist your list of NotificationBundle objects. I used SharedPreferences to do that, but you could use whatever else suits you better (like a DB table). To persist a List<NotificationBundle> in SharedPreferences I used Gson to serialize the array into json, and then saved that as a string. Assuming you can use SharedPreferences, you can find how to do the serialization in this answer.

有了该结构,基本上要做的就是:

With that structure in place, basically what's left for you to do is:

  • 当您必须发出通知时:

  • When you have to issue a notification:

  1. 使用您要通知的信息创建一个新的 NotificationBundle .
  2. SharedPreferences
  3. 中检索您现有的 List< NotificationBundle>
  4. 如果捆绑包列表为空,则将发出一个通知.如果不是,您将发布摘要-在这种情况下,您可以使用捆绑包列表来构建摘要的内容.关于摘要通知的好文章是[使用大视图样式].
  5. 将新的 NotificationBundle (来自 1 )添加到现有的 List< NotificationBundle> (来自 2 )并将其保存到 SharedPreferences .
  6. 使用 NotificationManager.notify()发布通知.如果您在此处始终使用相同的通知 id ,如果当前看不到您的应用程序中的任何通知,它将创建一个新通知,或者如果先前的通知可见,则将更新该通知.
  1. Create a new NotificationBundle with the information you want to notify.
  2. Retrieve your existing List<NotificationBundle> from SharedPreferences
  3. If your list of bundles is empty, you will issue a single notification. If it's not, you will issue a summary - in this case you can use your list of bundles to construct the content of your summary. A good article on summary notifications is [Using Big View Styles].
  4. Add your new NotificationBundle (from 1) into your existing List<NotificationBundle> (from 2) and save it to SharedPreferences.
  5. Issue your notification using NotificationManager.notify(). If you always use the same notification id here, it will create a new notification if none from your app are currently visible, or it will just update it if a previous notification is visible.

  • 在主Activity的 onResume()方法上,确保使用 NotificationManager.cancelAll()关闭所有通知.还要确保从您的 SharedPreferences 中删除现有的 List< NotificationBundle> .

  • On your onResume() method of your main Activity, make sure to dismiss all notifications with NotificationManager.cancelAll(). Also make sure to remove from your SharedPreferences your existing List<NotificationBundle>.

    这应该可以解决问题.

    这篇关于如何跟踪通知以了解何时显示摘要通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆