[英]How can I check if a specific type of local notification is enabled?
If I request authorization for local notifications for alerts and sounds using the code below, the user could go into settings after granting authorization turn off banners and sounds.如果我使用下面的代码请求对警报和声音的本地通知进行授权,则用户可以在授予授权关闭横幅和声音后将 go 设置为设置。 Authorization would still exist, but there would be no methods for sending notifications.授权仍然存在,但不会有发送通知的方法。 How can I check if specific types of notifications (eg alert) is enabled?如何检查是否启用了特定类型的通知(例如警报)?
@objc func registerLocal() {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
if granted {
self.notificationsAuth = true
} else {
self.notificationsAuth = false
}
self.notificationAuthUndertermined = false
}
}
For iOS 10.0 and later:对于 iOS 10.0 及更高版本:
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
// also available for badgeSetting, soundSetting, authorization status and ...
switch settings.alertSetting {
case .notSupported:
// Do stuff
break
case .disabled:
// Do stuff
break
case .enabled:
// Do stuff
break
}
}
We can verify alertSetting
, soundSetting
, badgeSetting
is enabled or not using below code,我们可以使用下面的代码验证alertSetting
、 soundSetting
、 badgeSetting
是否启用,
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
print("Notification settings: \(settings)")
let isAlertEnabled = settings.alertSetting
let isSoundEnable = settings.soundSetting
let isbadgeEnabled = settings.badgeSetting
if(isAlertEnabled == .enabled && isSoundEnable == .enabled && isbadgeEnabled == .enabled){
print("Alert, Sound and Badges are enabled.")
}else{
//Different action
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.