简体   繁体   English

如何检查是否启用了特定类型的本地通知?

[英]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,我们可以使用下面的代码验证alertSettingsoundSettingbadgeSetting是否启用,

 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.

相关问题 如何检查是否启用了本地通知? - How to check if Local Notification is enabled/disabled? 如何在 userNotificationCenter 中检查哪个标识符用于触发本地通知? - How can I check in the userNotificationCenter which identifier was used to trigger local notification? 如何检查在ios9上启用的本地通知 - how to check local notifs enabled on ios9 如何从本地通知中打开特定的视图控制器? - How do I open a specific view controller from local notification? 如何检查 iOS/iPadOS 中是否启用了暗模式? - How can I check whether dark mode is enabled in iOS/iPadOS? 如何使用 Swift 4 检查是否启用了“请勿打扰模式” - How can I check if "Do not Disturb mode" is enabled using Swift 4 如何检查 iOS 设备上是否启用了 Motion & Fitness? - How can I check whether or not Motion & Fitness is enabled on an iOS device? 如何检查用户是否从设置中启用了推送通知? - How to check if the user has enabled push notification from the settings? 如何获得firemonkey在iOS上显示本地通知? - How can I get firemonkey to display a local notification on iOS? 如何在以下情况下安排本地通知? - How can I schedule local notification for the following scenario?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM