36

旧方法

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:count];

现在给出错误Attempting to badge the application icon but haven't received permission from the user to badge the application

然后我尝试使用新的 API(我认为这与徽章值有关)

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

但我收到错误<CKError 0x165048a0: "Not Authenticated" (9/1002); "This request requires an authenticated account">

如何设置徽章或获得一些新的权限?

4

5 回答 5

39

除了 Daij-Djan 的回答:可以堆叠枚举,这样您就可以一次请求它们。如下:

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

调试输出提到我应该请求应用程序徽章权限

于 2014-06-05T00:49:18.857 回答
36

要在 ios8 下修改徽章,您必须请求权限

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

或在 objC

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
于 2014-06-04T09:52:02.467 回答
22

以前帖子的附加信息(完整registerUserNotificationSettings):

Apple 为注册通知和使用徽章制作了新的 API。

请参阅 WWDC 2014 会议视频文本版本文档

用户可以在“设置”中更改每个UIUserNotificationType( UIUserNotificationTypeBadge, UIUserNotificationTypeSound, UIUserNotificationTypeAlert) 的权限。

在更改徽章之前,您必须检查权限。

我的 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)

CurrentUserNotificationSettings方法在 UI 应用程序实例中可用,并将为您提供最新的用户通知首选项。

使用徽章编号:

[self setApplicationBadgeNumber:0];

代替

application.applicationIconBadgeNumber = 0;

PS:#ifdef __IPHONE_8_0由于需要在 Xcode5 和 Xcode6 中构建,因此在编译时检查( )。如果你没有这个需求,可以简化代码。

于 2014-09-24T09:13:39.020 回答
1

当我使用 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
    }
}
于 2015-05-24T03:02:46.953 回答
0

更新到 8.3,ObjC:我们应该添加 Daij-Djan 脚本来替换 NSLog(@"access denied for UIUserNotificationTypeBadge"); 在上面的 Spidy & KepPM 解决方案中。希望这会有所帮助

于 2015-08-10T14:40:18.493 回答