【问题标题】:Check authorisation status of local notifications检查本地通知的授权状态
【发布时间】:2017-07-10 06:04:09
【问题描述】:

我在我的应用程序中使用本地通知,在向用户展示新的通知屏幕之前,我想先检查授权状态。我使用的是shouldPerformSegue(identifier:, sender:) -> Bool 方法,这样如果通知未经用户授权,则不会出现用户配置和保存新通知的场景:

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    if identifier == "AddReminder" {
        // Check to see if notifications are authorised for this app by the user
        let isAuthorised = NotificationsManager.checkAuthorization()
        if isAuthorised {
            print(isAuthorised)
            return true
        }
        else {
            let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) {
                (action: UIAlertAction) in
            }

            let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
                guard let settingsURL = URL(string: UIApplicationOpenSettingsURLString) else {
                    return
                }
                if UIApplication.shared.canOpenURL(settingsURL) {
                    UIApplication.shared.open(settingsURL, options: [:], completionHandler: { (success) in
                        print("Settings opened: \(success)")
                    })
                }
            }
            alert.addAction(cancelAction)
            alert.addAction(settingsAction)

            present(alert, animated: true, completion: nil)

            print(isAuthorised)
            return false
        }
    }

    // By default, transition
    return true
}

这是我用于授权检查的方法:

static func checkAuthorization() -> Bool {
    // var isAuthorised: Bool
    var isAuthorised = true
    UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in
        switch notificationSettings.authorizationStatus {
        case .notDetermined:
            self.requestAuthorization(completionHandler: { (success) in
            guard success else { return }

        })
            print("Reached .notDetermined stage")
        case .authorized:
            isAuthorised = true
        case .denied:
            isAuthorised = false
        }

    }

    //print("Last statement reached before the check itself")
    return isAuthorised
}

我认为上述函数中的最后一条语句(返回 isAuthorized)在 UNUserNotificationCenter.current().getNotificationSettings{} 的主体执行之前返回,因此它总是返回 isAuthorized 在最开始时配置的任何内容方法。

问题: 您能否建议我如何使用更好的方式检查授权,因为我的方式甚至不起作用。

这只是我的第一个IOS应用,所以我对IOS开发比较陌生;任何帮助将不胜感激。

【问题讨论】:

  • 您忘记指定要定位的 iOS 版本,因为 API 发生了变化
  • 我的错,iOS 10,我以为 UserNotifications 是在 10 年才引入的

标签: ios swift uilocalnotification


【解决方案1】:

如果有人遇到类似问题,那么不要使用 getNotificationsSettings(){} 方法,该方法将在返回封闭方法后计算;我们可以使用不同的方法,即获取 currentUserNotificationSettings,这是我们应用程序的通知设置。然后检查当前设置是否包含 .aler、.sound 等。如果答案是肯定的,那么我们可以确定应用程序已启用其通知。

【讨论】:

    猜你喜欢
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多