在Swift中检查用户设置以获取推送通知

20

我在我的应用程序中有推送通知。每次启动应用程序时,我想检查用户是否已为我的应用程序启用了推送通知。

我是这样做的:

let notificationType = UIApplication.sharedApplication().currentUserNotificationSettings()!.types
if notificationType == UIUserNotificationType.None {
    print("OFF")
} else {
    print("ON")
}

如果用户禁用了推送通知,是否有办法在我的应用程序中激活它?

或者是否有其他方法将用户发送到推送通知设置(设置-通知-应用程序名称)?

6个回答

22

该应用程序无法更改设置。但是,您可以使用此代码将用户引导到特定于应用程序的系统设置。

extension UIApplication {
    class func openAppSettings() {
        UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
    }
}

更新至Swift 3.0版本

extension UIApplication {
    class func openAppSettings() {
        UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
    }
}

已更新至 iOS 10+ 和 Swift 5+

extension UIApplication {
    @objc class func openAppSettings() {
        shared.open(URL(string: openSettingsURLString)!,
                    options: [:],
                    completionHandler: nil)
    }
}

2
在 Swift 3 中的扩展 UIApplication { class func openAppSettings() { let url = URL(string: UIApplicationOpenSettingsURLString) UIApplication.shared.openURL(url!) } } - Zaldy Bughaw

15

iOS 10或更高版本

检查应用程序的推送通知是否已启用对于Swift 3有了巨大变化。如果您正在使用Swift 3,请改用此方法而不是上面的示例。

    let center = UNUserNotificationCenter.current()
    center.getNotificationSettings { (settings) in
        if(settings.authorizationStatus == .authorized)
        {
            print("Push authorized")
        }
        else
        {
            print("Push not authorized")
        }
    }

这是苹果文档中关于如何进一步优化您的检查的说明:https://developer.apple.com/reference/usernotifications/unnotificationsettings/1648391-authorizationstatus


5
此代码仅适用于 iOS 10 及以上版本,使用其他 SDK 运行此代码会导致崩溃,请注意。 - Andrei Neag
如果你没有在@available(iOS 10.0, *)中包装函数,XCcode会产生编译错误,但它不会崩溃。 - Hayden Holligan

6
这是 Swift 3 版本,您可以检查通知是否已启用或禁用。
let notificationType = UIApplication.shared.currentUserNotificationSettings?.types
    if notificationType?.rawValue == 0 {
        print("Disabled")
    } else {
        print("Enabled")
    }

2

salabaha 的回答的 Swift 4 版本

extension UIApplication {
    class func openAppSettings() {
        UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: {enabled in
            // ... handle if enabled
        })
}

1

简短回答: 如果用户拒绝了你的推送通知请求,你就不能再次请求,而应该将他们引导到设置页面,在其中使用一个警报来告知他们步骤和两个选项“确定”和“前往设置”,这就是您可以使用Roman的代码的地方。

详细回答: 只有在绝对需要时,您才应向用户请求推送通知,如果您要请求,最好先解释一下为什么您想/需要发送推送通知。这是一篇很棒的TechCrunch文章,将帮助您从用户那里获得更好的保留/接受率 - http://techcrunch.com/2014/04/04/the-right-way-to-ask-users-for-ios-permissions/


1
以下是最详细的方法,适用于iOS12和Swift 5:
 _ = UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        switch settings.authorizationStatus {
        case .notDetermined:
            <#code#>
        case .denied:
            <#code#>
        case .authorized:
            <#code#>
        case .provisional:
            <#code#>
        @unknown default:
            <#fatalError()#>
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接