简体   繁体   中英

Push notifications and token device in Swift

I am working on an App that need to know the device token to send notification to the users when they give their authorization. The first time the system ask the authorization for notification. If user say "allow" the system calls for me the method didRegisterForRemoteNotificationsWithDeviceToken and in the body of this method I write in UserDefaults the device token. This is the flow: 1) System ask for permission 2) in didFinishLaunchingWithOptions (inside AppDelegate) I call this method in my class that manage the Notification Framework:

   func registerForPushNotifications() {
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
            (granted, error) in
            print("NFM permission granted: \(granted)")
            // 1. Check if permission granted
            guard granted else { return }
            // 2. Attempt registration for remote notifications on the main thread
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }

3) the system calls for me the method didRegisterForRemoteNotificationsWithDeviceToken (in AppDelegate) and everything works fine. In the body of the method I write the token UserDefaults settings. The second case happen when the user denied the permission the first time. In this second case in another ViewController of my app I check if the user has given permission for push notification using the device setting (so out of the app). In a specific section of my app I use this method to verify if the user has given the authorization (the is always in my class that manage the Notification Framework).

    func checkPremissionStatus() -> Bool{
    var isAuth = false
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        print("NFM checkPremissionStatus -> notification status")
        switch settings.authorizationStatus {
        case .authorized:
            print("NFM checkPremissionStatus -> authorized status")
            self.autorizzato = true
            isAuth = true
        //TODO check pending notification...
        case .denied:
            print("NFM checkPremissionStatus -> denied status")
            self.autorizzato = false
            isAuth = false
        case .notDetermined:
            print("NFM notDetermined never see this print")
        }
    }
    return isAuth
}

In this specific case the method didRegisterForRemoteNotificationsWithDeviceToken it is not called and so I cannot store the token. The alert for ask to the user the authorization is showed only the first time that the user use open the app. The next times the user open the app the system does not show the alert anymore. How can I solve this problem?

Thanks.

I solve this iusse in this way: in the method checkPremissionStatus I add aa call to my method registerForPushNotifications and in this way the system calls the method didRegisterForRemoteNotificationsWithDeviceToken so I can use the device token in the business logic of my app. This is the code modified in the class that manage the Notification Framework.

    func checkPremissionStatus() -> Bool{
    var isAuth = false
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        print("NFM checkPremissionStatus -> notification status")
        switch settings.authorizationStatus {
        case .authorized:
            print("NFM checkPremissionStatus -> authorized status")
            self.autorizzato = true
            isAuth = true
            print("NFM checkPremissionStatus -> call registerForPushNotification")
            self.registerForPushNotifications()
        //TODO check pending notification...
        case .denied:
            print("NFM checkPremissionStatus -> denied status")
            self.autorizzato = false
            isAuth = false
        case .notDetermined:
            print("NFM notDetermined never see this print")
        }
    }
    return isAuth
}

So when I check for the user permission and I found that the user has enabled (from the device settings) the push notification case .authotrized the system call the right method and I can store the device token in the User Preferences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM