3

Im trying obtain data from notification, when my app is closed and when app is just in background. I get notification, tap on it, and in MainActivity(from android project) i want to obtain data. I can do it when my app is open, by HmsMessageService and OnMessageReceived, there is no problem. But i cant find examples, how to do it when app is closed. Any help, pls. There is my notification in Json:

        var jObject = new
        {
            message = new
            {
                notification = new
                {
                    title = titleNot,
                    body = bodyNot
                },
                android = new
                {
                    notification = new
                    {
                        foreground_show = false,
                        click_action = new
                        {
                            type = 3
                        }
                    }
                },
                token = new[] { token }
            }
        };

3 Answers3

2

When the application is closed, you can obtain parameters by customizing click_action.

Set intent in the message body on your app server.

{
    "message": {
        "notification": {
            "title": "message title",
            "body": "message body"
        },
        "android": {
            "notification": {
                "click_action": {
                    "type": 1,
                    "intent": "intent://com.huawei.xahmspushdemo/deeplink?#Intent;scheme=pushscheme;launchFlags=0x4000000;i.age=180;S.name=abc;end"
                }
            }
        },
        "token": [
            "pushtoken1"
        ]
    }
}

This is the document.

Np0v0
  • 33
  • 5
  • 1
    i hoped that should be a simplier way to do it. without changing message Type. Google Firebase allow to do it, Unfortunately Hms is not. – Jack The Healer Dec 16 '21 at 05:20
  • In my opinion, In this mode, the message type is not changed. just added the intent parameter and the parameter is transferred through the intent . – Np0v0 Dec 16 '21 at 11:45
2

Just as what Np0v0 mentioned, you can use click_action to get the data. There is another way to implement the goal.

  1. Register an Activity class to be started in the AndroidManifest.xml file of the app.

enter image description here

enter image description here

3.Receive data using the customized activity class.

For more detail information, pls refer to https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/andorid-basic-clickaction-0000001087554076#EN-US_TOPIC_0000001087554076__li7205195217309

Zinna
  • 1,947
  • 2
  • 5
  • 20
1

Solved problem without changing "click_action type". Just override OnNewIntent() method in main activity, and add "GetIntentData" with some logic.

    private void GetIntentData(Bundle bNotification)
    {
        if (bNotification != null)
        {
            try
            {
                if (bNotification.ContainsKey(TITLE_KEY) && bNotification.ContainsKey(BODY_KEY))
                {
                    \\...
                }
            }
            catch
            {
            }
        }
    }
    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        GetIntentData(intent?.Extras);
    }

in OnCreate() I created bundle and if its not null I load app:

        Bundle bNotification = Intent.Extras;
        if (bNotification == null || bNotification.IsEmpty)
        {
            LoadApplication(new App(...));
        }
        else
        {
            LoadApplication(new App(true, ...));
            GetIntentData(bNotification);
        }

https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/xamarin-customizingactions-0000001055648851

Sender
  • 6,660
  • 12
  • 47
  • 66