如何正确地更新通知后11 API? [英] How to properly update a notification post api 11?

查看:117
本文介绍了如何正确地更新通知后11 API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • Notification.Builder 应运而生方式更新的通知,这是已经在通知托盘是调用 setLatestEventInfo(),然后发送通知回通过 NotificationManager。通知()与相匹配的第一个通知()打电话给你做。

  • Before Notification.Builder came into existence the way to update a notification that was already in the notification tray was to call setLatestEventInfo() and then send the notification back through the NotificationManager.notify() call with an ID that matches the first notify() call you made.

现在<一href="http://developer.android.com/reference/android/app/Notification.html#setLatestEventInfo%28android.content.Context,%20java.lang.CharSequence,%20java.lang.CharSequence,%20android.app.PendingIntent%29"相对=nofollow> setLatestEventInfo() 是pcated的消息去$ P $ 使用Notification.Builder 代替。但我不能找到有关如何使用 Notification.Builder 正确更新通知的任何文件。

Now setLatestEventInfo() is deprecated with the message: Use Notification.Builder instead. But I cannot find any documentation about how to properly update a notification using Notification.Builder.

你只是想重新创建一个新的通知实例每次需要更新的通知时间?然后,只需传递到 NotificationManager.notify()的ID,你以前使用过?

Are you just suppose to recreate a new Notification instance every time you need to update the notification? Then simply pass that to NotificationManager.notify() with the ID you used before?

这似乎工作,但我想看看是否任何人有任何官方的确认,这是新的办法做到这一点?

It seems to work but I wanted to see if anyone had any official verification that this is the new "way to do this"?

有真正的原因,我问这个是因为在的Andr​​oid 4.1.1果冻豆的通知现在每次闪烁通知()被调用。当更新进度条<一个href="http://developer.android.com/reference/android/app/Notification.Builder.html#setProgress%28int,%20int,%20boolean%29"相对=nofollow> setProgress() 这看起来非常糟糕,使得它很难挖掘的通知。这是不是在4.1或previous版本的情况。所以我想确保我正确地做这个之前,我提交的bug。

There real reason I am asking this is because in Android 4.1.1 Jelly Bean, the notification now flashes everytime notify() is called. When updating a progress bar with setProgress() this looks really bad and makes it hard to tap on the notification. This was not the case in 4.1 or previous versions. So I want to make sure I am doing this correctly before I file a bug.

推荐答案

我通过调用 setWhen(0)我Notification.Builder解决了这个问题。这似乎为这一说法Android的默认值不适合更新通知鉴于位,而整个通知淡出/英寸。

I resolved this issue by calling setWhen(0) on my Notification.Builder. It seems Android's default value for this argument doesn't suit updating bits of the notification view without the entire notification fading out / in.

Notification.Builder builder = new Notification.Builder(c)
                .setContentTitle("Notification Title")
                .setSmallIcon(R.drawable.ic_notification_icon)
                .setProgress(max_progress,current_progress,false)
                .setWhen(0);
                notification = builder.getNotification();

mNotificationManager.notify(NOTIFICATION_ID, notification);

更新:

作为<一个href="http://stackoverflow.com/questions/11512986/how-to-properly-update-a-notification-post-api-11#comment41810560_12486197">WolframRittmeyer指出,使用当= 0 不是一种优雅的方式。我像形成以下解决方案:

Update:

As WolframRittmeyer stated, using when=0 is not an elegant way. I formed a solution like following:

if(mNotif == null) {
//either setting mNotif first time
//or was lost when app went to background/low memory
    mNotif = createNewNotification();
}
else {
    long oldWhen = mNotif.when;
    mNotif = createNewNotification();
    mNotif.when = oldWhen;
}
mNotificationManager.notify(NOTIFICATION_ID, mNotif);

这篇关于如何正确地更新通知后11 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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