Few months back I wrote an article on implementing Push Notification in awesome Xamarin Forms, So I just implemented Push Notifications in awesome Xamarin.Forms … 😉 after my personal experience at work. And YES ! it works like a charm without any doubt.
But after a while I noticed something in Xamarin iOS app, whereas when we send out the Push Notification from Azure Hub, the notification gets received by iPhone, and when I click on the Notification alert, it gets opened up in the app and displays the redirected page. But sometime, when I click on the received push notification alert, the app opens up, but the push notification message box doesn’t get displayed or gets redirected to the message display page. In short, sometimes the push notification alert click only opens up the app, nothing else.
Okay What is happening…
After careful observation I noticed that, when the app is running or if its in the background, and I click on the push notification alert, it gets opened up in the app and everything works fine. But when the app is closed or not running in the background, the push notification alert only opens up the app, and it doesn’t get redirected to the message display page or anything.
But Why is this happening…
I realized this after going through the push notification alert click event, which is ReceivedRemoteNotification()
This method gets fired when the User clicks on the received push notification alert on the phone. And from there it calls the ProcessNotification method where it displays the Push Notification alter Message Box and redirects the user to the give page in the app.
But it gets fired only when the app is already running or if its running in the background stack. If you have restarted the phone or closed the app or by any chance if the app is not running in the background, this method will not get fired upon above push notification action. And thereby it will only open up the app. 😦
So What is the Solution…
So what we need to do here is to detect at the FinishedLaunching() method whether the app was opened up by a Push Notification alert click or manual app icon click by the User.
This is a common scenario most of the developers has faced, not only in Xamarin Forms related iOS even in native iOS development. But most of us don’t realize this as it’s very hard to notice this scenario, because we usually don’t manually close apps in our iPhone devices. Even I got curious about this when my boss complained about this once, and yes took me a while to figure it out too… 😉
Alright ! Let’s patch it up !
Even though it took me a while to figure out the solution, it was quite simple to implement. The reason I’m sharing this is because there’s not much resources regarding this issue, probably because we don’t easily notice this issue.
This solution can be implemented easily for anyone even without following my previous Xamarin Forms push notifications implementation article, just do the changes as shown below, then you should be good to go. I’m not going to repost the whole code here, but only the necessary changes, but if you are looking for the full code, you may check up my article, So I just implemented Push Notifications in awesome Xamarin.Forms !
Did you know, when our iOS app is closed and it gets opens up by the click on a Push Notification Alert, the system passes through a parameter in the NSDictionary options parameter in the FinishedLaunching() method which is called UIApplicationLaunchOptionsRemoteNotificationKey !
All we have to do is capture this parameter value in our FinishedLaunching() method and process the notification. Inside this key object contains the whole push notification object which we could easily extract. So add the following code to the end of your FinishedLaunching() method in AppDelegate class.
As you can see above we are checking for the existence of the UIApplicationLaunchOptionsRemoteNotificationKey object key value in the options parameter of FinishedLaunching() method. After extracting the object we are passing it to our usual ProcessNotification() method in order to do the usual processing of the push notification message.
After that tiny trick your FinishedLaunching() method should look like this… 🙂
Well there it goes folks, hope this post was helpful for you and prolly saved you a lot of time… 🙂 Please share this among other developers as there aren’t much resources on Handling Push Notifications in Xamarin iOS when App is Closed, and you may save another developer’s time as well… 😉
Stay awesome fellow Geeks ! ^_^