MarJano
MarJano

Reputation: 1327

Get deviceToken when user enable push notification manually when app is running in background

Is there a way to get Push notification token when user enabled it manually from settings while the application was in the background. Is there a function available in Swift which will get push notification token when user will come back to the app.

Upvotes: 1

Views: 818

Answers (2)

Kakoo
Kakoo

Reputation: 11

When user turing push notification allowed in the general setting of ios, the app can call the

UIApplication.shared.registerForRemoteNotifications()

and device token is able to be obtained from

public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)

as I understand there is no any direct method to determine the user setting push notification manually, so I came up two alternative ways

  1. using NSNotification center observer to determine any setting changes as explained already here
  2. or in the Appdelegate applicationWillEnterForeground

to call bellow function (for example)

    func registerRemoteNotification() {
     let notificationCenter = UNUserNotificationCenter.current()
     notificationCenter.getNotificationSettings { (settings) in
        if settings.authorizationStatus == .authorized {
            //"authorized"
            DispatchQueue.main.async {
              UIApplication.shared.registerForRemoteNotifications()
             }
        } else {
          //"not authorized")
        }
      } 
    }

Upvotes: 1

ahmed
ahmed

Reputation: 568

You can use observer for system wise settings change. See more info on this thread

Upvotes: 0

Related Questions