Android - How to update notification number

后端 未结 3 1508
慢半拍i
慢半拍i 2021-02-06 06:23

Hi i want to show all the notification in a single view .. and want to update number of notification in status bar ... its updating all info but showing number always 1.. please

3条回答
  •  遇见更好的自我
    2021-02-06 07:12

    You have to keep track of the count. You could extend the Application class:

    public class AppName extends Application {
        private static int pendingNotificationsCount = 0;
    
        @Override
        public void onCreate() {
            super.onCreate();
        }
    
        public static int getPendingNotificationsCount() {
            return pendingNotificationsCount;
        }
    
        public static void setPendingNotificationsCount(int pendingNotifications) {
            pendingNotificationsCount = pendingNotifications;
        }
    }
    

    And you should modify the onReceive:

    @Override
    public void onReceive(Context context, Intent intent) {
        ...
        int pendingNotificationsCount = AppName.getPendingNotificationsCount() + 1;
        AppName.setPendingNotificationsCount(pendingNotificationsCount);
        notification.number = pendingNotificationsCount;
        ...
    }
    

    And you could reset the count when the user open the notification:

    AppName.setPendingNotificationsCount(0);
    

提交回复
热议问题