首页 > 解决方案 > 是否可以在应用程序中提供设置以让用户打开或关闭 Braze 通知?

问题描述

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

标签: iosswift

解决方案


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

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

推荐阅读