iOS 8 检查远程通知类型的权限

Posted

技术标签:

【中文标题】iOS 8 检查远程通知类型的权限【英文标题】:iOS8 check permission of remotenotificationtype 【发布时间】:2014-08-29 14:08:21 【问题描述】:

我可以像这样检查用户是否在 ios8 之前授予了通知(警报)权限:

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types & UIRemoteNotificationTypeAlert)

    //user granted

它不适用于 iOS8,它说:

iOS (3.0 and later) Deprecated:Register for user notification settings using the  registerUserNotificationSettings: method instead.

控制台说:

enabledRemoteNotificationTypes is not supported in iOS 8.0 and later.

那么如何在 iOS 8 上检查它?

【问题讨论】:

【参考方案1】:

好的,我这样解决了我的问题:

BOOL isgranted = false;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
    
        isgranted =  [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
    
#else
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types & UIRemoteNotificationTypeAlert)
    
        isgranted = true;
    
#endif

【讨论】:

您好,您的 IOS8 解决方案并不能完全说明您的“允许通知”是打开还是关闭。如果允许通知设置关闭,“isRegisteredForRemoteNotifications”将返回 false,但如果禁用声音通知并且警报类型设置为“none”,也会返回 false。有没有办法单独确定是否启用了允许通知? @MichalShatz 我找不到办法,但这个建议很糟糕,因为它不能在 ios7 和 ios 8 上运行,请参阅我添加到这个问题的额外答案 @DanielBaughman types & UIRemoteNotificationTypeAlert 展示了相同的行为【参考方案2】:

我对 woheras 的回答进行了扩展,使其更具活力

 - (BOOL)pushNotificationsEnabled


    BOOL isgranted = false;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
        
            isgranted =  [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
        else

        
    else
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (types & UIRemoteNotificationTypeAlert)
        

           isgranted = true;
        else

        
    
    return isgranted;


【讨论】:

你如何将它转换为 swift,在过去的半小时里一直试图弄清楚:(【参考方案3】:

我在使用isRegisteredForNotifications 时运气不佳。我最终改用currentUserNotificationSettings

+ (BOOL)notificationServicesEnabled 
    BOOL isEnabled = NO;

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)])
        UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

        if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) 
            isEnabled = NO;
         else 
            isEnabled = YES;
        
     else 
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (types & UIRemoteNotificationTypeAlert) 
            isEnabled = YES;
         else
            isEnabled = NO;
        
    

    return isEnabled;

【讨论】:

【参考方案4】:

这里想用一块石头打两只鸟。首先,您应该检查 currentUserNotificationSettings(),而不是检查 isRegisteredForRemoteNotifications()。这样,您可以查看是否允许使用警报、声音等。其次,这里的 iOS 版本检查似乎是多余的。简单地检查对象是否响应选择器就足够了。最后,代码示例在 Swift 中,供一位用户使用:P

func hasPushNotificationsEnabled() -> Bool 
    if UIApplication.sharedApplication().respondsToSelector("currentUserNotificationSettings") 
        let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
        return (settings.types & UIUserNotificationType.Alert) != UIUserNotificationType.None
    

    let types = UIApplication.sharedApplication().enabledRemoteNotificationTypes()
    return (types & UIRemoteNotificationType.Alert) != UIRemoteNotificationType.None

【讨论】:

【参考方案5】:

这是 Daniels 在 Swift 2.0 中的回答方法

func hasPushEnabled() -> Bool 
    //ios 8+
    if UIApplication.sharedApplication().respondsToSelector("currentUserNotificationSettings") == true 
        let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
        if (settings?.types.contains(.Alert) == true)
            return true
         else 
            return false
        
    
    else 
        let types = UIApplication.sharedApplication().enabledRemoteNotificationTypes()
        if types.contains(.Alert) == true 
            return true
         else 
            return false
        
    

【讨论】:

以上是关于iOS 8 检查远程通知类型的权限的主要内容,如果未能解决你的问题,请参考以下文章

IOS 8 推送通知检查返回错误的 UIRemoteNotificationType

IOS 远程通知在 IOS 8 的后台不起作用

IOS 8 注册远程通知只成功了一半

iOS8 - 请求远程推送通知

iOS 在收到静默远程通知后发送本地通知

在 ios 中处理远程通知接收