0

Trying to set the badge number via a remote notification with this code

UIUserNotificationType types = (UIUserNotificationTypeAlert|
                                    UIUserNotificationTypeSound|
                                    UIUserNotificationTypeBadge);

 UIUserNotificationSettings *settings;
    settings = [UIUserNotificationSettings settingsForTypes:types
                                                 categories:nil];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    [application setApplicationIconBadgeNumber:7];

..
}

but I don't see the badge on the icon.

Is there a configuration that I am missing?

Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244

1 Answers1

4

Badge: The number to display as the badge of the app icon. If this property is absent, the badge is not changed. To remove the badge, set the value of this property to 0.

You don't manually set the badge.As referenced in here it is the payload that manages it automatically according to "badge" in your payload.And it is you who manages the payload in server side code.

Something like this:

     {
    "aps" : {
        "alert" : {
            "title" : "Game Request",
            "body" : "Bob wants to play poker",
            "action-loc-key" : "PLAY"
        },
        "badge" : 5,
    },
    "acme1" : "bar",
    "acme2" : [ "bang",  "whiz" ]
}

Also, first you have to ask for a permission to user in iOS8, like this in your -didFinishLaunchingWithOptions delegate method

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

If your app received notifications and then your app will become active, it is your responsibility to reset the badge number like this:

- (void)applicationDidBecomeActive:(UIApplication *)application {

   application.applicationIconBadgeNumber = 0;
}

This is another reference quoted below:

The badge icon number is governed by the badge property in the payload received as push message. The app itself has no role to play in what number shows up till the app becomes active. Setting the 'badge' property to any integer value (Note : it should not be string and should NOT be enclosed within "") does what is needed. The OS on receiving the notification looks for the value for 'badge' and immediately sets it on the app badge icon. All this happens when app is not active. To make sure it increments or decrements based on what happens in the app, the app should send message to server with the updated badge number.

In the active state, it is app's responsibility to handle the remote notification and change the badge icon.

Community
  • 1
  • 1
Okhan Okbay
  • 1,374
  • 12
  • 26