且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更新徽章的图标,当应用程序被关闭

更新时间:2022-12-22 11:57:56

如果您的应用程序被关闭或在后台推送通知不会唤醒它。你需要做服务器端,包括你想看到的图标在您的通知有效载荷数:

  {
    APS:{
        提醒:您的通知消息,
        徽章:1
    }
}

看一看在推送通知编程指南

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

    }

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