Huawei. Obtain data(title, body, sentTime) from notification by using Push Kit. Xamarin Forms

376 views Asked by At

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

There are 3 answers

2
Np0v0 On

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.

0
Zinna On

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

0
Jack The Healer On

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