OGeek|极客世界-中国程序员成长平台

标题: ios - 以编程方式导航到手机上应用程序的推送通知设置 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:27
标题: ios - 以编程方式导航到手机上应用程序的推送通知设置

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

我的问题更多是关于我的应用的推送通知的 URL,而不仅仅是一般设置,如下面的代码示例所示

示例代码:

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



Best Answer-推荐答案


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

关于ios - 以编程方式导航到手机上应用程序的推送通知设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47834503/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4