【问题标题】:Is there a way to check push notification setting is ever requested?有没有办法检查是否请求过推送通知设置?
【发布时间】:2016-09-14 16:35:43
【问题描述】:

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

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

【问题讨论】:

    标签: ios swift push-notification


    【解决方案1】:

    这就是我现在的做法。

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

    【讨论】:

    • 这是一个可能的想法,但我需要将它应用于已经发布的应用程序。会有用户允许/禁止推送通知,但没有保存的值。
    • 请求权限,然后检查当前视图控制器是否呈现警报控制器怎么样? (不允许/允许的系统一)。我不确定它是否会成为您的视图层次结构的一部分。
    猜你喜欢
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    相关资源
    最近更新 更多