Android中如何统计已接收通知并设置应用图标角标
Hey there! Let's figure out why your badge only shows 1 and fix this issue. The core problem here is almost certainly that you're not properly persisting and incrementing the notification count—let's break this down step by step.
You mentioned using MODE_PRIVATE, so let's make sure we're correctly storing and updating the count in SharedPreferences every time a new notification comes in. Right now, it sounds like you might be setting the count to 1 every time instead of incrementing it.
Here's how to adjust your logic inside the NotificationMaker class:
// Call this method every time a new notification is received private void updateNotificationCount(Context context) { // Get access to shared preferences for storing the count SharedPreferences prefs = context.getSharedPreferences("NotificationTracker", MODE_PRIVATE); // Fetch current count (default to 0 if no previous value exists) int currentCount = prefs.getInt("unread_notifications", 0); // Increment count for the new notification int updatedCount = currentCount + 1; // Save the updated count to preferences SharedPreferences.Editor editor = prefs.edit(); editor.putInt("unread_notifications", updatedCount); editor.apply(); // Update the app badge with the new count ShortcutBadger.applyCount(context, updatedCount); }
Don't forget to decrement the count when a notification is cleared or tapped! Otherwise, your badge will show a higher number than the actual unread notifications.
Add this logic where you handle notification interactions or dismissals:
// Call this when a notification is tapped or dismissed private void decrementNotificationCount(Context context) { SharedPreferences prefs = context.getSharedPreferences("NotificationTracker", MODE_PRIVATE); int currentCount = prefs.getInt("unread_notifications", 0); // Only decrement if count is greater than 0 to avoid negative numbers if (currentCount > 0) { int updatedCount = currentCount - 1; SharedPreferences.Editor editor = prefs.edit(); editor.putInt("unread_notifications", updatedCount); editor.apply(); ShortcutBadger.applyCount(context, updatedCount); } }
Double-check you have the correct dependency added to your module-level build.gradle file:
dependencies { implementation 'me.leolin:ShortcutBadger:1.1.22@aar' }
Also, ensure you're never passing a hardcoded 1 to ShortcutBadger.applyCount()—this is likely why your badge only shows 1 right now. Always pass the dynamically updated count from SharedPreferences.
When your app is killed and restarted, you'll want to restore the badge count. Add this to your app's main Activity or Application class onCreate() method:
SharedPreferences prefs = getSharedPreferences("NotificationTracker", MODE_PRIVATE); int savedCount = prefs.getInt("unread_notifications", 0); ShortcutBadger.applyCount(this, savedCount);
- Hardcoding
1inShortcutBadger.applyCount()(the most likely culprit here) - Not persisting the count across app restarts (so every launch resets the count to 0, then the first notification sets it to 1)
- Forgetting to decrement the count when notifications are dismissed or interacted with
That should resolve your badge issue! If you run into device-specific problems (some manufacturers have unique badge rules), ShortcutBadger handles most major brands out of the box, but feel free to tweak the logic if needed.
内容的提问来源于stack exchange,提问作者J.Rashit

