首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通知未正确更新

通知未正确更新
EN

Stack Overflow用户
提问于 2013-09-04 12:04:37
回答 1查看 2.5K关注 0票数 3

我运行一个服务,它通过startForeground(int id, Notification notification配置为前台服务,我想更新这个通知。实现这一目标的代码大致如下:

代码语言:javascript
复制
private void setForeground() {
    Notification foregroundNotification = this.getCurrentForegroundNotification();

    // Start service in foreground with notification

    this.startForeground(MyService.FOREGROUND_ID, foregroundNotification);
}

...

private void updateForegroundNotification() {
    Notification foregroundNotification = this.getCurrentForegroundNotification();

    // Update foreground notification

    NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(MyService.FOREGROUND_ID, foregroundNotification);
}

并根据服务状态生成通知:

代码语言:javascript
复制
private Notification getCurrentForegroundNotification() {
    // Set up notification info

    String contentText = ...;

    // Build notification

    if (this.mUndeliveredCount > 0) {
        String contentTitleNew = ...;

        this.mNotificationBuilder
        .setSmallIcon(R.drawable.ic_stat_notify_active)
        .setContentTitle(contentTitleNew)
        .setContentText(contentText)
        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_stat_notify_new))
        .setNumber(this.mUndeliveredCount)
        .setWhen(System.currentTimeMillis() / 1000L)
        .setDefaults(Notification.DEFAULT_ALL);
} else {
        this.mNotificationBuilder
        .setSmallIcon(R.drawable.ic_stat_notify_active)
        .setContentTitle(this.getText(R.string.service_notification_content_title_idle))
        .setContentText(contentText)
        .setLargeIcon(null)
        .setNumber(0)
        .setWhen(0)
        .setDefaults(0);
    }

    // Generate Intent

    Intent intentForMainActivity = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentForMainActivity, 0);

    // Build notification and return

    this.mNotificationBuilder.setContentIntent(pendingIntent);
    Notification foregroundNotification = this.mNotificationBuilder.build();

    return foregroundNotification;
}

问题是我的通知没有得到正确的更新:当我启动服务在前台运行时,使用this.mUndeliveredCount > 0多次调用this.mUndeliveredCount > 0,然后再用this.mUndeliveredCount == 0调用它,通知右下角的小通知图标不会消失,即使没有提供大图标。根据setSmallIcon(int icon)类的NotificationBuilder方法,这种行为并不完全是预期的。在这种方法中,只有在指定了大型图标的情况下,小图标才应该出现在右下角:

public Notification.Builder setSmallIcon (int icon) 设置小图标资源,用于表示状态栏中的通知。扩展视图的平台模板将在左侧绘制此图标,除非还指定了一个大图标,在这种情况下,小图标将被移动到右侧。

我在这里更新服务通知做错了什么?或者这是一个Android的错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-05 17:00:27

在确定不是我在这里的错误导致了不必要的小通知图标之后,我搜索并找到了解决上述bug的简单方法:

当通过我的Notification方法更新updateForegroundNotification()时,我用“重置”通知生成并更新通知ID。“重置”通知配置如下:

代码语言:javascript
复制
this.mNotificationBuilder
    .setSmallIcon(0)
    .setContentTitle("Reset")
    .setContentText("Reset")
    .setLargeIcon(null)
    .setNumber(0)
    .setWhen(0)
    .setDefaults(0);

有了这样的通知,我打电话给

代码语言:javascript
复制
Notification resetForegroundNotification = this.getResetForegroundNotification();

this.mNotificationManager.cancel(MyService.FOREGROUND_NOTIFICATION_ID);
this.mNotificationManager.notify(MyService.FOREGROUND_NOTIFICATION_ID, resetForegroundNotification);

在执行预期的通知更新之前,不需要的右下角图标在下一个只设置一个小图标的通知中消失。

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

https://stackoverflow.com/questions/18613351

复制
相关文章

相似问题

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