在 iOS 中,可以使用以下代码来判断通知权限:
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.getNotificationSettings { (settings) in
switch settings.authorizationStatus {
case .notDetermined:
// Request authorization
break
case .denied:
// The user has denied the notification permission
break
case .authorized:
// The user has granted the notification permission
break
case .provisional:
// The user has granted the notification permission, but it may be used only under certain conditions
break
如果权限未确定,可以请求授权:
notificationCenter.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
if granted {
// The user has granted the notification permission
} else {
// The user has denied the notification permission