徽章未在 iOS 8 中设置

badge doesn't get set in iOS 8

正在尝试使用此代码通过远程通知设置徽章编号

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

..
}

但我没有在图标上看到徽章。

是否有我缺少的配置?

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.

你不手动设置 here 中引用的 badge.As 它是根据你的 payload.And 中的 "badge" 自动管理它的负载是你在服务器端代码中管理负载。

像这样:

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

此外,首先您必须在 iOS8 中向用户请求许可,例如 -didFinishLaunchingWithOptions 委托方法中的 this

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

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

如果您的应用收到通知,然后您的应用将激活,您有责任像这样重置徽章编号:

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

   application.applicationIconBadgeNumber = 0;
}

这是下面引用的另一个 reference

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.