在iOS 8上启用远程通知类型

时间:2014-09-09 11:25:37

标签: ios push-notification

在iOS 7中,我可以检索用户以这种方式启用的通知类型:

NSUInteger rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

但是在iOS 8上我不能这样做,因为我在控制台中得到了这个:

enabledRemoteNotificationTypes is not supported in iOS 8.0 and later.

我找到了一种方法来检查是否在iOS 8上启用了通知:

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]

但我如何检索启用哪种类型的通知,就像在iOS 7中一样?

2 个答案:

答案 0 :(得分:2)

根据developer.apple.com,你可以使用这个UIApplication的方法:

- (UIUserNotificationSettings *)currentUserNotificationSettings

然后使用以下方法检查通知类型:

@property (nonatomic, readonly) UIUserNotificationType types;

所以它会是这样的:

UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
UIUserNotificationType types = settings.types;

答案 1 :(得分:0)

我遇到了同样的问题,我发现types属性基本上是一个位掩码。以下是如何提取信息的方法。 我的例子是在Swift中。

    let settings = UIApplication.sharedApplication().currentUserNotificationSettings()

    if settings.types.rawValue & UIUserNotificationType.Alert.rawValue == UIUserNotificationType.Alert.rawValue
    {
       // can receive alert!
    }
    else
    {
       // if the user is not even able to receive alerts, show him a hint on how to reenable notifications in system settings
    }

    if settings.types.rawValue & UIUserNotificationType.Badge.rawValue == UIUserNotificationType.Badge.rawValue
    {
        // can receive badge!
    }
    if settings.types.rawValue & UIUserNotificationType.Sound.rawValue == UIUserNotificationType.Sound.rawValue
    {
        // can receive sound!
    }