Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin iOS - Push Notifications - Distinguish push notification clicked from arrived

I'm creating a Xamarin.iOS application that includes push notifications.

I've noticed that if the app is not running (not just at the background), when a push notification arrived (with the correct payload), the OS will take over handling the notification and will present it to the user and if the user clicks on the notification while it closed, I will be able to get this information from the NSDictionary options parameter of the FinishedLaunching method (finding if it contains the UIApplication.LaunchOptionsRemoteNotificationKey key)

so far, everything is great.

but I have two scenarios,

1) the push notification arrives when the application is running. 2) the user clicked on a push notification that arrived earlier (while the app was closed) after he launches the app

for both scenarios, the method ReceivedRemoteNotification is invoked.

but for each scenario I want to do a different business logic, my question is: How do I know if the ReceivedRemoteNotification was invoked by scenario 1 or scenario 2?

I've new to Xamarin and I've searched the web for it, but couldn't find a solution.

can anyone help me with this?

Thanks.

like image 738
Suave Avatar asked Dec 08 '22 23:12

Suave


1 Answers

check the application state using the following condition, inside didReceiveRemoteNotification method,

UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
{//write here...Application opened by tapping notification}

FYI- The above code is in Objective-c

like image 126
RJV Kumar Avatar answered Apr 27 '23 01:04

RJV Kumar