以编程方式导航以在电话上推送应用程序的通知设置

时间:2017-12-15 14:36:39

标签: ios swift3 swift4

我有一个带按钮的示例应用程序。点击按钮应该是用户推送我的应用程序的通知服务,以便能够禁用它们或启用。 我知道如何使用此示例代码进入常规设置,但我认为通知可能需要一些其他参数,如bundleId。

我的问题更多是关于我的应用推送通知的网址,不仅可以进行常规设置,因为这会在下面的代码示例中显示

示例代码:

@IBAction func pushNotificationSettings(button: UIButton) {
    guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
        return
    }

    if UIApplication.shared.canOpenURL(settingsUrl) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                print("Settings opened: \(success)") // Prints true
            })
        } else {
            // Fallback on earlier versions
        }
    }
}

1 个答案:

答案 0 :(得分:0)

对于IOS 10或更高版本

 UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        if settings.authorizationStatus == .authorized {
            // Notifications are allowed
        }
        else {
            // Either denied or notDetermined

            let alertController = UIAlertController(title: nil, message: "Do you want to change notifications settings?", preferredStyle: .alert)

            let action1 = UIAlertAction(title: "Settings", style: .default) { (action:UIAlertAction) in
                if let appSettings = NSURL(string: UIApplication.openSettingsURLString) {
                    UIApplication.shared.open(appSettings as URL, options: [:], completionHandler: nil)
                }
            }

            let action2 = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction) in
            }

            alertController.addAction(action1)
            alertController.addAction(action2)
            self.present(alertController, animated: true, completion: nil)
        }
    }