Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Android OnNewIntent Not Called by Closed App

What is Working

I have implemented push notification for the Android side of our Xamarin Forms app using the following Xamarin guide:

https://developer.xamarin.com/guides/android/application_fundamentals/notifications/remote-notifications-with-gcm/

This has worked great and we correctly and OnNewIntent is called when the application is running and in the background. We then call our method to process the push notification's intent and all is well.

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    GoogleListenerService.ReceivedRemoteNotification(intent);
}

What is the Problem

If we close the application, trigger a push notification, and click on the push notification, the application does not appear to call OnNewIntent and instead appears to start the application exactly as it would if we just clicked the app icon (calling OnCreate).

What has been tried

The main workarounds I have tried implementing are outlined by:

  1. Android OnNewIntent not called

Xamarin only has a GetIntent method that requires an string URL input, and using the following results in null data.

Intent intent = new Intent(this, typeof(MainActivity));
Bundle data = intent.Extras;
  1. onNewIntent is not called

In the second post, all changes to LaunchMode = SingleTop in our intent service, our MainActivity.cs file, and in our manifest file have resulted in no changes in behavior.

EDIT

Push notifications are always received and OnMessageReceived is always getting called, whether the application be in the background, or closed altogether. Moreover, OnNewIntent receives the pending intent when the application is in the background, just not when it is closed.

this.Intent.Extras;

Always appears to evaluate to null (currently located in the OnResume method).

like image 823
Jarrod L Avatar asked Jan 03 '17 16:01

Jarrod L


1 Answers

When receiving notifications on Android there is a difference, whether you are receiving Notifactions or Data Notifications and how the App behaves.

Looking at the table in the cloud messaging docs here: https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages

App State  |    Notification     |        Data        |    Both
-------------------------------------------------------------------------------------
Foreground |  onMessageReceived  |  onMessageReceived | onMessageReceived
Background |  System tray        |  onMessageReceived | Notification: system tray
                                                        Data: in extras of the intent

The cases where your notification arrives in onMessageReceived in your service, you need do your own work with the incoming notification and decide yourself whether you want to present a notification or do something else.

From this it sounds like you are sending regular notifications and preparing a PendingIntent, which is what triggers the OnNewIntent when you press it.

When the App is in the background an you receive a regular notification, the app should just launch regularly. If you have attached data to the notifcation you should be able to grab it from the Intent Extras property.

So what you are seeing here is the correct behavior.

like image 181
Cheesebaron Avatar answered Oct 01 '22 00:10

Cheesebaron