英文:
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();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论