You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

Step 1: Fix Notification Count Persistence

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);
}
Step 2: Handle Notification Dismissal/Clicks

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);
    }
}
Step 3: Verify ShortcutBadger Setup

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.

Step 4: Restore Badge on App Restart

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);
Common Mistakes to Avoid
  • Hardcoding 1 in ShortcutBadger.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:22:20