Question

I am facing a problem with push notification application badge number value updation.

I am doing like:

-(void)application:(UIApplication *)application 
       didReceiveRemoteNotification:(NSDictionary *)userInfo {

       UIApplicationState state = [application applicationState];
       if (state == UIApplicationStateActive) {
          // do stuff when app is active

       }else{
          // do stuff when app is in background
          [UIApplication sharedApplication].applicationIconBadgeNumber = 
          [UIApplication sharedApplication].applicationIconBadgeNumber+1;  
               /* to increment icon badge number */
       }
 }

But, the icon is showing the badge number as '1' always, and it is not incrementing when more notifications are there/ one notification came after another.

Any advice is appreciable...

Was it helpful?

Solution

The badge number is set by the operating system when you receive a JSON notification payload that resembles the following:

{
    "aps" : {
        "alert" : "New notification!",
        "badge" : 2
    }
}

As you see, it's the server who is responsible for setting the correct number in the badge key. Your server needs to track or compute the number of pending notifications for each user and generate the badge number before sending the notification to Apple.

The client responsibility is to clear the notification badge, or decrement it, when the user sees a notification. The code to do so is

application.applicationIconBadgeNumber = application.applicationIconBadgeNumber - 1; // Decrement counter

or

application.applicationIconBadgeNumber = 0; // Reset counter assuming the user is able to see all notifications at once.

OTHER TIPS

you can create a static variable instead, and assign it to the applicationIconBadgeNumber:

static int i=1;
[UIApplication sharedApplication].applicationIconBadgeNumber = i++;

I had the same problem and solved it by creating int. variable in class.h

like this :

a custom class.H

@property int badge;

a custom class.M

-(id)init{

_badge=0;

self = [super init];
if (self) {

}
return self;}

-(void)reminderCreator:(NSDate*)onTime withText:(NSString*)text{

_badge += 1;

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = onTime;
localNotification.alertBody = text;
localNotification.soundName=UILocalNotificationDefaultSoundName;

localNotification.applicationIconBadgeNumber=_badge;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; }

so if you initialize this custom class, somewhere (maybe at your viewController) and then call the reminderCreator method several times to setup few localNotifications, it will assign incremented number to each notification.

+1 if this helped :)

The -(void)application:didReceiveRemoteNotification: will only be called when your app is running in the foreground.

If you want the badge to be increase when your app isn't running you should set the badge number in the push notification payload. You should there for keep track of the badge number server side, since the badge property of the push notification payload will be use as the badge number. It will not increment the badge number for you.

Since the system handles the incoming push notifications your app is not informed of received push notifications for you app. Only when you app is running in foreground will the -(void)application:didReceiveRemoteNotification: be called. There is no way to get you app to repsond to push notification when it is not in the foreground.

Yes it seems that it always returns badgeNumber 1 in spite it has value increased to 2.

when using such code:

// update badge count
        let currentBadgeCount = UIApplication.shared.applicationIconBadgeNumber
        let badge = (aps["badge"] as? Int) ?? 1
        UIApplication.shared.applicationIconBadgeNumber = currentBadgeCount + badge

so I think the only solution will be to store it in UserDefaults and then update value of UserDafaults and then it is set then update badge count?

This should work:

static var notificationsCount: Int {
        set {
            let userDefaults = UserDefaults(suiteName: SharedAppData.appGroup)
            userDefaults?.set(newValue, forKey: "notificationsCount")

            UIApplication.shared.applicationIconBadgeNumber = newValue
        }
        get {
            let userDefaults = UserDefaults(suiteName: SharedAppData.appGroup)
            return userDefaults?.integer(forKey: "notificationsCount") ?? 0
        }
    }

And then

SharedAppData.notificationsCount = 0 // to reset badge count

And this to increment

 // update badge count
        SharedAppData.notificationsCount += 1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top