IOS development - setting applicationIconBadgeNumber and message push in IOS 8

Summary: Setting applicationIconBadgeNumber in IOS7 will not be a problem, but setting applicationIconBadgeNumber directly in IOS8 will report an error

because in IOS8, if you want to set applicationIconBadgeNumber, you need user authorization. In IOS8, you need to add the following code:
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

UIApplication *app = [UIApplication sharedApplication];
// The number in the upper right corner of the application
app.applicationIconBadgeNumber = 0;

However, if these two sentences are run in the IOS7 system, an error will be reported, so you need to judge the IOS version first. The complete code is as follows:
float version = [[[UIDevice currentDevice] systemVersion] floatValue];

if (version >= 8.0) {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

UIApplication *app = [UIApplication sharedApplication];
// The number in the upper right corner of the application
app.applicationIconBadgeNumber = 0;

If it is a registration message push, you need to write:
float version = [[[UIDevice currentDevice] systemVersion] floatValue];

if (version >= 8.0) {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
}

UIApplication *app = [UIApplication sharedApplication];
// The number in the upper right corner of the application
app.applicationIconBadgeNumber = 0;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326402813&siteId=291194637