有没有办法检查推送通知设置是否被请求?

问题描述:

有没有办法通过系统警报检查应用程序是否获得推送通知许可?有没有办法检查推送通知设置是否被请求?

UIApplication.sharedApplication().currentUserNotificationSettings()?.types.rawValue

它返回0既当应用程序从未要求推送通知允许当用户拒绝的权限。

我想在应用程序从不请求权限时显示系统警报弹出窗口。如果用户拒绝了权限,我还想让用户去设置权限设置。

例如:

let pushNotificationPermissionNeverAcquired: Bool = ??? 
if (pushNotificationPermissionNeverAcquired) { 
    // show system popup to acquire push notification permission 
    UIApplication.sharedApplication.registerForRemoteNotificationTypes([ * some types ]) 

} else if (UIApplication.sharedApplication().currentUserNotificationSettings()?.types.rawValue == 0) { 
    // user declined push notification permission, so let user go to setting and change the permission 
    UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)) 

} else { 
    // push notification is allowed 

} 

我如何检查pushNotificationPermissionNeverAcquired

这就是我现在正在做的事情。

if NSUserDefaults.standardUserDefaults().stringForKey(NOTIFICATION_PERMISSIONS_ASKED) == nil 
    { 
     NSUserDefaults.standardUserDefaults().setObject("true", forKey: NOTIFICATION_PERMISSIONS_ASKED) 
     // Request permissions 
    } 
    else 
    { 
     // Show your own alert 
    } 

你可以保存一个布尔太多,如果你想与“boolForKey”访问它,如果没有密钥发现返回false。

忘记了这一点。

if let enabledTypes = UIApplication.sharedApplication().currentUserNotificationSettings()?.types { 
    switch enabledTypes 
    { 
    case UIUserNotificationType.None: 
     // Denied permissions or never given 
    default: 
     // Accepted 
    } 
} 
+0

这是可能的想法,但我需要采用它到已经发布的应用程序。将有用户允许/不允许推送通知,但没有保存的值。 – Ryan

+0

如何请求权限,然后检查您当前的视图控制器是否呈现警报控制器? (系统不允许/允许)。我不确定它是否会成为视图层次结构的一部分。 – ohr