Jim Clermonts
Jim Clermonts

Reputation: 2660

how to reset badge count when android app opens on android

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.

Upvotes: 5

Views: 6285

Answers (1)

Jim Clermonts
Jim Clermonts

Reputation: 2660

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

Upvotes: 7

Related Questions