Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

iphone - Update badge icon when app is closed

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];

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...