Swift ios check if remote push notifications are enabled in ios9 and ios10

后端 未结 11 1320
悲哀的现实
悲哀的现实 2020-12-01 02:26

How can I check if the user has enabled remote notifications on ios 9 or ios 10?

If the user has not allowed or clicked No I want to toggle a message asking if they

11条回答
  •  甜味超标
    2020-12-01 03:13

    I tried Rajat's solution, but it didn't work for me on iOS 10 (Swift 3). It always said that push notifications were enabled. Below is how I solved the problem. This says "not enabled" if the user has tapped "Don't Allow" or if you have not asked the user yet.

    let notificationType = UIApplication.shared.currentUserNotificationSettings!.types
        if notificationType == [] {
            print("notifications are NOT enabled")
        } else {
            print("notifications are enabled")
        }
    

    PS: The method currentUserNotificationSettings was deprecated in iOS 10.0 but it's still working.

提交回复
热议问题