android - 通知未正确更新

标签 android service notifications

我运行一个服务,通过 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 ,通知右下角的小通知图标不会消失,即使没有提供大图标。根据 documentation of the setSmallIcon(int icon) ,这种行为并不完全符合预期。 NotificationBuilder 的方法如果指定了大图标,则说明小图标只应出现在右下角的类:

public Notification.Builder setSmallIcon (int icon)

Set the small icon resource, which will be used to represent the notification in the status bar. The platform template for the expanded view will draw this icon in the left, unless a large icon has also been specified, in which case the small icon will be moved to the right-hand side.

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

最佳答案

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

当通过我的 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);

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

关于android - 通知未正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18613351/

相关文章:

javascript - 为什么 scrollView 在我的代码中不能以垂直方式工作?

Android 蓝牙套接字关闭 : read return: -1

java - 应用程序的小部件、服务和模型(单例)之间的通信(类似 MVC 的架构)

grails - 包装内的服务

javascript - 当 channel 开始直播时从 Youtube API 获取通知?

安卓动态壁纸 : start rendering below notification bar

php - 为什么 jsonObject 为空?

android - 如何从 SQLite Openhelper 调试 SQLite 数据库?

android - 关于 Service 和 IntentService 之间特定 "difference"的问题

ruby-on-rails - 我对在 Rails 中创建通知系统的看法