Check user settings for push notification in Swift

前端 未结 6 804
滥情空心
滥情空心 2021-02-12 12:54

I have push notifications in my app. Whenever the app is launched, I would like to check whether the user has enabled push notification for my application.

I do it this

6条回答
  •  野的像风
    2021-02-12 13:30

    For iOS 10 or higher

    Checking if push notifications have been enabled for your app has changed dramatically for Swift 3. Use this instead of the above examples if you are using Swift 3.

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

    Here is Apple's documentation on how to further refine your check: https://developer.apple.com/reference/usernotifications/unnotificationsettings/1648391-authorizationstatus

提交回复
热议问题