asked    Matthew     2018-10-22       ios       15 view        1 Answer

[SOLVED] 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.

  1 Answer  

        answered    Abraham     2018-10-22      

I solve this iusse in this way: in the method checkPremissionStatus I add a a 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.





Your Answer





 2018-10-22         Chad

How to retrieve specific information from a list in python?

I have a file from which I would like to retrieve specific information.First, I extracted the lines of interest and I put them in a list:array = []file_in = open("Traj.pdb", "r") for line in file_in: if line.startswith('TITLE'): array.append(line)I end up with a list like this: ['TITLE Protein in INSANE! Membrane UpperLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 LowerLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 t= 1500000.00000\n', 'TITLE Protein in INSANE! Membrane UpperLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 LowerLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 t= 1500020.00...
 python                     2 answers                     92 view
 2018-10-22         Jocelyn

How do I split a string with '$' delimiter?

I have this FINAL PAYMENT $25 string on a MVC c# application.I want to split into FINAL PAYMENT and 25I tried doing this string s = "FINAL PAYMENT $25";string[] str1 = s.Split('$');//result: 25How can I get the rest. Can anyone help? Split method returns a string array, if you need both elements of this array, Try:string s = "FINAL PAYMENT $25";string[] resArray = s.Split('$');var FPayment = resArray[0];var second25= resArray[1]; [XXX]You can use indexOf instead of a Splitstring s = "FINAL PAYMENT $25";int index = s.IndexOf("$");String final_pay = s.Substring(index...
 c#                     2 answers                     92 view
 2018-10-22         Rory

UseWebpackDevMiddleware AggregateException

I try to create a ASP.NET Core MVC application using NPM/Webpack/Vue.js. I am following this tutorial:https://marczak.io/posts/netcore-vuejs/When starting the project I get this exception:An exception of type 'System.AggregateException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'One or more errors occurred.' Inner exceptions found, see $exception in variables window for more details. Innermost exception Newtonsoft.Json.JsonReaderException : Unexpected character encountered while parsing value: <. Path '', line 0, position 0. at Newton...
 c#                     1 answers                     92 view