iOS 8. Change app badge number without any notific

2019-09-01 08:16发布

问题:

In my app I need to set badge onto the app icon and set numbers for this badge. The problem is that I need to register user notification settings like this:

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

in order to make it work on iOS8. If I run this code then on start system presents an alert to the user which says something like:ApplicationName would like to send you notifications. Notifications may include bla la bla. It's the standard one, I'm sure you've seen it many times. Just because I registered notification settings the system thinks my app is going to post some notifications. But it's not! I don't want to post notifications. All I wanna do is to set badge onto the icon and a number onto this badge. Is there any possibility to achieve this on iOS 8 without registering user notifications or registering but not showing that system alert? Any ideas, guys?

回答1:

The documentation is explicit on that point, on iOS8 and later, you have to register for user notifications :

@property(nonatomic) NSInteger applicationIconBadgeNumber; // set to 0 to hide. default is 0. In iOS 8.0 and later, your application must register for user notifications using -[UIApplication registerUserNotificationSettings:] before being able to set the icon badge.