用户通知中心获取授权选项 Swift 3/4 ios10/11

User Notification Center get Authorization Options Swift 3/4 ios10/11

对于小于 10 的 iOS,我们曾经使用 属性 来获取用户允许的通知类型(.alert、.badge 等)。我们使用了这个代码:

UIApplication.shared.currentUserNotificationSettings?.types

但现在已弃用。

问题:

除了对 ios10/11 使用 UNUserNotificationCenter 之外,我们如何做同样的事情? 有没有等效的方法?

考虑一下:已弃用的方式仍然有效,但我们永远不知道是否有一天 Apple 会取消它。

提前致谢!

您仍然可以通过以下方式获取通知设置:

UNUserNotificationCenter.current().getNotificationSettings { settings in

    if settings.alertSetting == .enabled {
        //alert is enabled
    }

}

apple doc

中所述

When the value of this property is UNNotificationSetting.enabled, the app is authorized to display alerts.