通知未能正确更新

3

我运行的服务通过startForeground(int id, Notification notification)配置为前台服务,我想要更新这个通知。我用以下代码来实现:

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);
}

根据服务状态生成通知:

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;
}

问题是我的通知没有正确更新:当我启动服务在前台运行时,多次调用updateForegroundNotification(),其中一些带有this.mUndeliveredCount > 0参数,并在此之后再使用this.mUndeliveredCount == 0参数进行调用。尽管没有提供大图标,但通知底部右侧的小图标却不会消失。根据NotificationBuilder类的文档中所述,这种行为并非完全符合预期,其中指定了只有在指定了大图标时,小图标才会出现在右下角:

public Notification.Builder setSmallIcon (int icon)

设置将用于表示状态栏中的通知的小图标资源。在扩展视图的平台模板中,除非也指定了一个大图标,否则该图标将绘制在左侧,否则小图标将被移动到右侧。

那么在更新服务通知时我做错了什么呢?或者这是Android的bug?

该行为似乎是一个错误;下面提供了一种解决方法。您可以在官方Android错误跟踪器上访问错误报告:http://code.google.com/p/android/issues/detail?id=59771&can=1&q=notification%20small%20icon&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars - Jan Z.
1个回答

0

在确定不是我的错误导致了不需要的小通知图标后,我搜索并找到了一个针对上述错误的简单解决方法:

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

this.mNotificationBuilder
    .setSmallIcon(0)
    .setContentTitle("Reset")
    .setContentText("Reset")
    .setLargeIcon(null)
    .setNumber(0)
    .setWhen(0)
    .setDefaults(0);

有了这样的通知,我调用

Notification resetForegroundNotification = this.getResetForegroundNotification();

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

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


这就是你所做的全部吗?我也遇到了同样的问题,取消并通知并不能解决它。 - John Sardinha
实际上,那就是我所做的一切,但 API 的行为可能已经发生了变化。 - Jan Z.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接