Handling external notifications

Hello,



I have implemented in my app the possibility to receive push notifications via the FCM service. I have managed to do all required configurations and I can receive the notifications on all the devices that have the app installed on them.


In the messages sent, I am using a special key that should indicate the app what window should it open, to help the user access the functionality that the notification is referring to. Until now, I have implemented in the AppDelegate.m file, using the method didReceiveNotificationResponse, a line where I am posting the received notification inside the app, with the following line of code:


[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object: nil userInfo:userInfo];


After doing so, I went to the main view controller, that is accessed every time the app opens, and implemented an observer that should get the posted notification from above:


[[NSNotificationCenter defaultCenter] addObserver: self selector:@selector(receiveNotification:) name:@"Notification" object:nil];


The receiveNotification method contains code that should performSegueWithIdentifier, based on the value of the key inside the notification object.


My question is, am I doing it right? If not, how can I handle this issue?



Thank you in advance!

Replies

am I doing it right? If not, how can I handle this issue?


As you ask the question, does that mean it does not work as expected ?

If so, what do you get and how is it different from what you expect ?

Thank you for answering!


It does work, as I am getting the notification data in the view controller holding the observer (and from there, I can manage to direct the user to the right window), but I don't know if that's how it should be done.

Great.


I do not see a problem in what you do.


Just think of removing the observer when no more needed.

What I usually, to avoid creating the same observer several times, before adding the observer, I remove it ; if it does not exist, nothing occurs and it is created after.

If it already exist, then I avoid duplication.


You could also test if observer exists:

https://stackoverflow.com/questions/52276059/swift-observer-kvo-checking-for-existence

Understood. Thank you for the help!

Good continuation.


Don't forget to close the thread.

What you are proposing is a two step notification process. It has certain advantages and certain disadvantages over a one step process. Consider instead adding code in the app delegate's didReceiveNotificationResponse that interpretes the push notification and does just what is appropriate for the specific push notification. It can often do that simply from within the app delegate by, for example, issuing alerts to the presented viewController:

   UIViewController* parentController =self.window.rootViewController;
    while( parentController.presentedViewController &&
          parentController != parentController.presentedViewController ){
        parentController = parentController.presentedViewController;
    }


or by referencing any one of the other viewControllers from the app delegate itself if the app dleegate retains a reference to those controllers. You can also issue a single notification from within this method that performs the specific task just as you are doing now from the "main viewController". In that case, each viewController could add an observer that was specific for its task and could be added in viewDidLoad once and never removed.

Thank you for the answer!


I shall try this solution and see how it goes. Also, from AppDelegate, if I were to always get through the main ViewController, is there a way for me to send, while opening it, a variable that could help me along the way? Say, if I had a property called ActionType in ViewController, would I be able to set a value for it directly from the AppDelegate, in didReceiveNotificationResponse?

You can simply use a field value in use [NSUserDefaults standardDefaults] as the variable.

Otherwise you would need a pointer to the class and then set up the variable as a property of the class to be able to change it from the app delegate