2

I am trying to update the badge icon for my app(closed) when I received a PN.

I have tried adding the codes into but it's not working when my app is closed. It works when the app is running in the foreground.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
       NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

//Accept push notification when app is not open
    if (remoteNotif) {
      [application setApplicationIconBadgeNumber:100];
    return YES;
    }

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

            [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 30];

    }
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
LDWP
  • 77
  • 2
  • 5

3 Answers3

4

If you app is closed or in the background, a Push notification won't wake it up. You need to do this server side and include the number you want to see on icon in your notification payload:

{
    "aps" : {
        "alert" : "Your notification message",
        "badge" : 1
    }
}

Have a look at the Apple doc on Push Notification programming guide

Eric Genet
  • 1,260
  • 1
  • 9
  • 19
  • Thanks for your clarification. I read the Push Notification programming guide and saw this sentence : "If the target application isn’t running when the notification arrives, the alert message, sound, or badge value is played or shown. If the application is running, the system delivers the notification to the application delegate as an NSDictionary object. The dictionary contains the corresponding Cocoa property-list objects (plus NSNull)." I always thought the app can listen while closed. – LDWP Jul 22 '13 at 09:33
0

for that set applicationIconBadgeNumber = 1 or 0 in didFinishLaunchingWithOptions: method like bellow...

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

See Another answer for UILocalNotification From this link ios-badge-number-live-update

Also another link for RemoteNotifications from this link RemoteNotificationsPG Guide

Community
  • 1
  • 1
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
0

Since push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification.

But you can send the badge number in the payload of the push notification, but the you will have to do the calculation server side.

The payload could look like this:

    {
       "aps" : {
       "alert" : "You got your emails.",
       "badge" : 1
    }
  }

Now the app application badge icon will show 1.

Arpit Kulsreshtha
  • 2,452
  • 2
  • 26
  • 52
  • Thanks for your clarification. I read the Push Notification programming guide and saw this sentence : "If the target application isn’t running when the notification arrives, the alert message, sound, or badge value is played or shown. If the application is running, the system delivers the notification to the application delegate as an NSDictionary object. The dictionary contains the corresponding Cocoa property-list objects (plus NSNull)." I always thought the app can listen while closed – LDWP Jul 22 '13 at 09:34