How to correctly set application badge value in iO

2019-01-31 17:51发布

问题:

Old method

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:count];

is now gives error Attempting to badge the application icon but haven't received permission from the user to badge the application.

Then I tried to use new API (that I'm think is related to badge value)

CKModifyBadgeOperation * operation = [[CKModifyBadgeOperation alloc] initWithBadgeValue:50];
[operation setModifyBadgeCompletionBlock:^(NSError *error) {
      NSLog(@"%@", error);
}];
[operation start];

But I'm receiving error <CKError 0x165048a0: "Not Authenticated" (9/1002); "This request requires an authenticated account">

How to set badge or receive some new permissions?

回答1:

In addition to Daij-Djan's answer: it's possible to stack the enums so you can request them all at once. Like follows:

UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

Debug output mentions I should ask for Application Badge permission



回答2:

to modify the badge under ios8 you have to ask for permissions

    let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)

or in objC

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];


回答3:

Additional info for previous posts (in complete to registerUserNotificationSettings):

Apple makes new API for registering notifications and working with badges.

See WWDC 2014 session video , text version and documentation.

User can change permissions for every UIUserNotificationType (UIUserNotificationTypeBadge, UIUserNotificationTypeSound, UIUserNotificationTypeAlert) in Settings.

Before changing badge you must check permissions.

Code sample from my AppDelegate:

#ifdef __IPHONE_8_0

- (BOOL)checkNotificationType:(UIUserNotificationType)type
{
    UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

    return (currentSettings.types & type);
}

#endif

- (void)setApplicationBadgeNumber:(NSInteger)badgeNumber
{
    UIApplication *application = [UIApplication sharedApplication];

    #ifdef __IPHONE_8_0
    // compile with Xcode 6 or higher (iOS SDK >= 8.0)

    if(SYSTEM_VERSION_LESS_THAN(@"8.0"))
    {
        application.applicationIconBadgeNumber = badgeNumber;
    }
    else
    {
        if ([self checkNotificationType:UIUserNotificationTypeBadge])
        {
            NSLog(@"badge number changed to %d", badgeNumber);
            application.applicationIconBadgeNumber = badgeNumber;
        }
    else
        NSLog(@"access denied for UIUserNotificationTypeBadge");
    }

    #else
    // compile with Xcode 5 (iOS SDK < 8.0)
    application.applicationIconBadgeNumber = badgeNumber;

    #endif
}

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

The CurrentUserNotificationSettings method is available in the UI application instance and will give you the most up-to-date user notification preferences.

Working with badge number:

[self setApplicationBadgeNumber:0];

instead of

application.applicationIconBadgeNumber = 0;

PS: Checking at compiling (#ifdef __IPHONE_8_0) due to the need to build in Xcode5 and Xcode6. If you do not have this need, the code can be simplified.



回答4:

I write a class to handle it when I use swift:

class ZYUtility
{
    /// Set badge
    class func setApplicationBadgeNumber(badge: Int) {
        if ZYUtility.SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO("8.0") {
            if UIApplication.sharedApplication().currentUserNotificationSettings().types & UIUserNotificationType.Badge != nil {
                UIApplication.sharedApplication().applicationIconBadgeNumber = badge
            } else {
                println("No permission to set badge number")
            }
        } else {
            UIApplication.sharedApplication().applicationIconBadgeNumber = badge
        }
    }

    /// System check
    class func SYSTEM_VERSION_EQUAL_TO(version: String) -> Bool {
        return UIDevice.currentDevice().systemVersion.compare(version,
            options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedSame
    }

    class func SYSTEM_VERSION_GREATER_THAN(version: String) -> Bool {
        return UIDevice.currentDevice().systemVersion.compare(version,
            options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedDescending
    }

    class func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
        return UIDevice.currentDevice().systemVersion.compare(version,
            options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedAscending
    }

    class func SYSTEM_VERSION_LESS_THAN(version: String) -> Bool {
        return UIDevice.currentDevice().systemVersion.compare(version,
            options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedAscending
    }

    class func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool {
        return UIDevice.currentDevice().systemVersion.compare(version,
            options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedDescending
    }
}


回答5:

update to 8.3, ObjC: we should add Daij-Djan script to replace NSLog(@"access denied for UIUserNotificationTypeBadge"); in Spidy & KepPM solution above. Hope this help s.o.



标签: ios8