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

Wei Wen 1,096 Reputation points
2021-08-16T02:52:30.033+00:00

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.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,302 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,541 Reputation points Microsoft Vendor
    2021-08-16T09:52:36.397+00:00

    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.


0 additional answers

Sort by: Most helpful