0

I am building a messageing applicaion in xamarion IOS. My Problem is following

Application is in Background and a Remote Notification comes. Now User can open application by clicking both app icon and Notification Icon. Application Calls didReceivedRemoteNotification when Notification received and when User Taps the Notification. In both cases Application state is Background. when user taps the notification I need to open screen related to notification and When User taps App Icon on home screen I need to open default screen.

I am not able to identify that application is opened whether by taping Remote notification or by App Icon.

Edit: Application is still running in background.

2) Application is in foreground and user locks the phone. In this case App's state become Background because WillEnterBackground is called. Now Application is in background and a Remote Notification is received and DidReceiveRemoteNotification event fired with App state is Background. User taps the Notification and again DidReceiveRemoteNotification is called and state is background. so i can not open the screen related to notification because I can not identify that event is fired due to notification received or due to taping the notification received.

I have tried the solutions given in the following stackoverflow link iOS push notification: how to detect if the user tapped on notification when the app is in background?

But still facing the same issue.

1 Answers1

1

When user locks the screen, the app will enter the background state. So when remote notification arrives, the system will alert user on the notification center(on the top of screen) and DidReceiveRemoteNotification will not be fired automatically as the app is on the foreground state. There's only one state to fire the DidReceiveRemoteNotification event when the app is on the background state(tap the notification on the top to open the app). This background state includes the situation what you mentioned above:

Application is in foreground and user locks the phone.

So I think and I have tested that: when user locks the screen and notification comes(before this manipulation, the app is on foreground), DidReceiveRemoteNotification will not be called automatically.

Another way to achieve your effect is implementing the IUNUserNotificationCenterDelegate interface if you deployed your apps on iOS 10+. Implement the two events below in your delegate:

[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
    Console.WriteLine("Handling iOS 10 foreground notification");
    completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
}

[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
    completionHandler();
}

WillPresentNotification called to deliver a notification to an application that is running in the foreground. And DidReceiveNotificationResponse will only be fired when user taps the notification even though app is on the foreground. So you can move your code from DidReceiveRemoteNotification to DidReceiveNotificationResponse. This event will ensure the code is only run after user taps the notification.

Ax1le
  • 6,563
  • 2
  • 14
  • 61