question

WeiWen-3421 avatar image
0 Votes"
WeiWen-3421 asked LeonLu-MSFT commented

How to display a specific page on Android when the user clicks push notification and the app is not started

This question has been asked before. But I don't think the answer is helpful. Here is the link that is relevant to my question:
https://social.msdn.microsoft.com/Forums/en-US/fd9832ae-dc19-4b8e-b93c-7d0ffebddb46/redirect-to-specific-page-when-firebase-notification-tapped?forum=xamarinforms

The answer requires the push notification payload to include certain information so that a second Activity can open. But in my case, payload will always be the same whether the app is in foreground, background or not started yet. My interest is in the Not Started case, since in the other two cases, I already get the right page to display.

A related question is: how can I test which part of the code is reached when the user clicks a push notification while the app is not even started yet. If in Visual Studio, I click Run button, the app will already start and does not satisfy the test condition, i.e. the user clicks push notification when the app is not started.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

how can I test which part of the code is reached when the user clicks a push notification while the app is not even started yet. If in Visual Studio, I click Run button, the >app will already start and does not satisfy the test condition, i.e. the user clicks push notification when the app is not started.

If you want to test the Data messages you need to the create your server environment and FCM, then you can send the FCM payload like following code.

{
  "to":"some_device_token",
  "content_available": true,
  "notification": {
      "title": "hello",
      "body": "test message",
      "click_action": "OPEN_ACTIVITY_1"
  },
  "data": {
      "extra":"juice"
  }
}


Then, after debug your application with VS, your application has been installed the emulator or device, you can click the application icon start this application. then click white background square.

123555-image.png

choose your application, swipe up, to remove your application from running task, then you can set it(the user clicks push notification when the app is not started.)
123528-image.png




===================Update=======================



If the app is not yet started, and the user clicks push notification, how do I know what code gets executed so that I can modify code to let the desired page to appear? In this case, >when the user clicks push notification, the payload data should be sent somewhere in the code. Where is it?

First of all, I want to confirm with your nugetpackage, If you use CrossGeeks/FirebasePushNotificationPlugin, onMessageReceived() callback even if your app is in foreground/background/killed. So you can receive the Data messages, If the app is not yet started.

However, if you use Xamarin.Firebase.Messaging plugin (it comes from the native FCM lib by binding-library), Google official article do not mentioned that app is not yet started could receive the data messages, it just has two situation (Foreground and Background). See this Handling messages topic,

123821-image.png

And you want to know "how do I know what code gets executed so that I can modify code to let the desired page to appear?". the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity., When app is in background, Intent must be delivered at your launcher activity . So it opens your Launcher activity. Now you check if there is data in Intent in launcher activity then start your required Activity like following code.

`
 [Activity(Label = "FCMNotifications", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
 protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
           

            if (Intent.Extras != null)
            {
                foreach (var key in Intent.Extras.KeySet())
                {
                    var value = Intent.Extras.GetString(key);

                    if (string.Equals(value, "com.xamarin.fcmexample.OPEN_ACTIVITY_1"))
                    {
                        StartActivity(new Android.Content.Intent(this, typeof(SepcificalActivity)));
                    }
                    Log.Debug(TAG, "Key: {0} Value: {1}", key, value);
                }
            }
}

`








Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.






image.png (98.4 KiB)
image.png (116.8 KiB)
image.png (128.0 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LeonLu-MSFT Your answer is helpful. But I really do not want to make any changes to payload. What I am interest in is: If the app is not yet started, and the user clicks push notification, how do I know what code gets executed so that I can modify code to let the desired page to appear? In this case, when the user clicks push notification, the payload data should be sent somewhere in the code. Where is it?

If I mark your answer as Accepted Answer, the first question I asked won't be answered. If no one else knows the answer to the first question, as a last resort, I guess I will have to try your answer.

0 Votes 0 ·

Please see my updated answer.

0 Votes 0 ·