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

标签 ios swift push-notification

有没有办法通过系统警报检查应用程序是否已获得推送通知权限?

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 
    }

如果需要,您也可以保存一个 bool 值并使用“boolForKey”访问它,如果没有找到 key ,它会返回 false。

这个也忘了。

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

关于ios - 有没有办法检查推送通知设置是否被请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39495585/

相关文章:

ios - 带有相机 View 的程序化屏幕截图

ios - 在后台快速更新用户位置

ios - 快速创建 Sprite 套件游戏。将角色添加到场景中

ios - CoreML 模型 : Convert imageType model input to multiArray

c# - pushChannel 在 Windows Phone 8.1 设备中始终为空(推送通知)

ios - UILabel sizeToFit 不适用于自动布局 ios6

ios - WL.Client.login() 然后 nsurlerrordomain 错误 1012

ios - 在 iOS 中,如何根据环境(dev、hom、prod)更改启动屏幕图像?

php - 向多个设备发送推送消息

push-notification - 推送通知服务器