如何在 Android 应用程序打开时重置徽章计数?

huangapple go评论65阅读模式
英文:

how to reset badge count when android app opens on android

问题

打开应用后,我希望徽章计数被移除。目前,只有当我通过锁定屏幕上的推送消息打开应用时,徽章计数才会被移除。因此,我也可以这样提问:“如何从锁定屏幕中移除推送消息”。

使用这段代码,我可以检索通知:

@Override
protected void onStart() {
    super.onStart();
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        StatusBarNotification[] notifications = notificationManager.getActiveNotifications();
        for (StatusBarNotification statusBarNotification : notifications) {
            statusBarNotification.getNotification().number = 0;
        }
    }
}

但是,当将数字设置为0时,什么也不会发生。

英文:

After I open the App, I want the badge count to be removed. Right now, it only gets removed when I open the App via the Push message in the lock screen. The push message then gets removed from the lock screen, so I could also phrase this question as "How to remove push message from lock screen".

With this code I can retrieve the notification:

@Override
protected void onStart() {
    super.onStart();
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        StatusBarNotification[] notifications = notificationManager.getActiveNotifications();
        for (StatusBarNotification statusBarNotification : notifications) {
            statusBarNotification.getNotification().number = 0;
        }
    }
}

But nothing happens when settings the number to 0.

答案1

得分: 6

Correct answer was submitted by @Alex Kamenkov.

@Override
protected void onStart() {
super.onStart();
resetBadgeCounterOfPushMessages();
}

private void resetBadgeCounterOfPushMessages() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (notificationManager != null) {
notificationManager.cancelAll();
}
}
}

英文:

Correct answer was submitted by @Alex Kamenkov.

   @Override
    protected void onStart() {
        super.onStart();
        resetBadgeCounterOfPushMessages();
    }

    private void resetBadgeCounterOfPushMessages() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            if (notificationManager != null) {
                notificationManager.cancelAll();
            }
        }
    }

huangapple
  • 本文由 发表于 2020年1月6日 21:29:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定