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

Viewed 179

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 Answers

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.

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

Related