是否可以在应用程序中提供设置以允许用户打开或关闭Braze通知?

时间:2018-06-26 07:36:15

标签: ios swift

我正在使用Braze将通知推送到我们的应用。是否可以在我的应用程序中添加设置以打开或关闭推送通知?如果是,该怎么做?

1 个答案:

答案 0 :(得分:0)

您无法在应用程序中添加通知设置。但是您可以检查通知权限状态。通过使用此代码

let current = UNUserNotificationCenter.current()

current.getNotificationSettings(completionHandler: { (settings) in
    if settings.authorizationStatus == .notDetermined {
        // Notification permission has not been asked yet, go for it!
    }

    if settings.authorizationStatus == .denied {
        // Notification permission was previously denied, go to settings & privacy to re-enable
    }

    if settings.authorizationStatus == .authorized {
        // Notification permission was already granted
    }
})

如果身份验证状态被拒绝或不确定,则可以使用任何控件(按钮,开关)将用户重定向到应用程序设置,以便在通知时使用。使用此代码:

    guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
            return
        }

        if UIApplication.shared.canOpenURL(settingsUrl) {
            UIApplication.shared.open(settingsUrl, completionHandler: { (success)
 in
                print("Settings opened: \(success)
") // Prints true
            })
        }