How to get notification authorization status in swift 3?

北慕城南 提交于 2019-12-08 15:07:14

问题


How can I check UNUserNotificationCenter for current authorization status in iOS 11? I've been looking for a while and found some code but it's not in swift 3 and some of functions were deprecated in iOS 10. Can anyone help?


回答1:


Okay I found it:

let center = UNUserNotificationCenter.current()
center.getNotificationSettings { (settings) in
    if(settings.authorizationStatus == .authorized)
    {
        print("Push authorized")
    }
    else
    {
        print("Push not authorized")
    }
}

code by: Kuba




回答2:


When getting the notification authorization status, there are actually three states it can be in, i.e.

  • authorized
  • denied
  • non-determined

A straightforward way to check these is with a switch-case where .authorized, .denied, and .nonDetermined are enums in UNAuthorizationStatus

UNUserNotificationCenter.current().getNotificationSettings { (settings) in
    print("Checking notification status")

    switch settings.authorizationStatus {
    case .authorized:
        print("authorized")

    case .denied:
        print("denied")

    case .notDetermined:
        print("notDetermined")

    }
}

Description of UNAuthorizationStatus can be found here in Apple's docs https://developer.apple.com/documentation/usernotifications/unauthorizationstatus



来源:https://stackoverflow.com/questions/46457733/how-to-get-notification-authorization-status-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!