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

在iOS 8上启用远程通知类型,ios,push-notification,Ios,Push Notification,在iOS 7中,我可以通过以下方式检索用户启用的通知类型: NSUInteger rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 但在iOS 8上,我无法做到这一点,因为我在控制台中得到: enabledRemoteNotificationTypes is not supported in iOS 8.0 and later. 我找到了一种检查iOS 8上是否启用通知的方法: [[UI

在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中启用的通知类型?

根据developer.apple.com,您可以使用此UIApplication的方法:

- (UIUserNotificationSettings *)currentUserNotificationSettings
然后使用以下命令检查通知类型:

@property (nonatomic, readonly) UIUserNotificationType types;
所以会是这样的:

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

我也有同样的问题,我发现types属性基本上是一个位掩码。以下是如何提取信息。 我的例子是斯威夫特

    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!
    }