Friday, 13 September 2019

How to access payload of non-transparent HMS push notifications?

I am preparing an Android app for publishing on Huawei AppGallery and am able to send push notifications (non-transparent) from my app backend server via HMS Push Service to the mobile phone as described below.

However, I wonder how to access the push notification payload in the app:

screenshot

Here is how I currently send push notifications -

First, my backend POSTs to https://login.vmall.com/oauth2/token the following:

grant_type=client_credentials&client_id=101076759&client_secret=XXXXX

and successfully gets an access token from HMS backend:

{
"access_token":"CF1/tI97Ncjts68jeXaUmxjmu8BARYGCzd2UjckO5SphMcFN/EESRlfPqhi37EL7hI2YQgPibPpE7xeCI5ej/A==",
"expires_in":604800
}

Then my backend POSTs to (that is URL-encoded for {"ver":"1", "appId":"101076759"}) -

https://api.push.hicloud.com/pushsend.do?nsp_ctx=
    %7B%22ver%22%3A%221%22%2C+%22appId%22%3A%22101076759%22%7D

the following URL-encoded body:

access_token=CF1/tI97Ncjts68jeXaUmxjmu8BARYGCzd2UjckO5SphMcFN/EESRlfPqhi37EL7hI2YQgPibPpE7xeCI5ej/A==
&nsp_svc=openpush.message.api.send
&nsp_ts=1568056994
&device_token_list=%5B%220869268048295821300004507000DE01%22%5D
&payload=%7B%22hps%22%3A%7B%22msg%22%3A%7B%22action%22%3A%7B%22param%22%3A%7B%22appPkgName%22%3A%22de%2Eslova%2Ehuawei%22%7D%2C%22type%22%3A3%7D%2C%22type%22%3A3%2C%22body%22%3A%7B%22title%22%3A%22Alexander%3A+How+to+access+payload%3F%22%2C%22content%22%3A%22Alexander%3A+How+to+access+payload%3F%22%7D%7D%2C%22ext%22%3A%7B%22gid%22%3A86932%7D%7D%7D

where the payload value is (and I am not sure, if it has a correct JSON structure and what does "type" 3 really mean):

{
  "hps": {
    "msg": {
      "action": {
        "param": {
          "appPkgName": "de.slova.huawei"
        },
        "type": 3
      },
      "type": 3,
      "body": {
        "title": "Alexander:+How+to+access+payload?",
        "content": "Alexander:+How+to+access+payload?"
      }
    },
    "ext": {
      "gid": 86932
    }
  }
}

I need to extract the custom integer "gid" value (the "game id" in my app).

In the custom receiver class I have the following methods defined, but they are not called (except the onToken method - when my app launches and async requests the "push token" from HMS by calling HuaweiPush.HuaweiPushApi.getToken method):

public class MyReceiver extends PushReceiver {
    private final static String BELONG_ID =  "belongId";

    @Override
    public void onToken(Context context, String token, Bundle extras) {
        String belongId = extras.getString(BELONG_ID);
        Log.d(TAG, "onToken belongId=" + belongId + ", token=" + token);
    }

    // this method is called for transparent push messages only NOT CALLED
    @Override
    public boolean onPushMsg(Context context, byte[] msg, Bundle bundle) {
        String content = new String(msg, "UTF-8");
        Log.d(TAG, "onPushMsg content=" + content);
        return true;
    }

    // this method is when a notification bar message is clicked NOT CALLED
    @Override
    public void onEvent(Context context, Event event, Bundle extras) {
        if (Event.NOTIFICATION_OPENED.equals(event) || Event.NOTIFICATION_CLICK_BTN.equals(event)) {
            int notifyId = extras.getInt(BOUND_KEY.pushNotifyId, 0);
            Log.d(TAG, "onEvent notifyId=" + notifyId);
            if (notifyId != 0) {
                NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                manager.cancel(notifyId);
            }
        }

        String msg = extras.getString(BOUND_KEY.pushMsgKey);
        Log.d(TAG, "onEvent msg=" + msg);
        super.onEvent(context, event, extras);
    }

    // this method is called when push messages state changes
    @Override
    public void onPushState(Context context, boolean pushState) {
        Log.d(TAG, "onPushState pushState=" + pushState);
    }
}

Please help me to pass a custom integer value from my backend to the app via HMS push notifications.

UPDATE:

I have tried printing getIntent().toUri(0) in onCreate of my app, when tapping a notification, but I only see the following:

#Intent;action=android.intent.action.MAIN;
category=android.intent.category.LAUNCHER;
launchFlags=0x30020000;
package=de.slova.huawei;
component=de.slova.huawei/de.slova.MainActivity;end



from How to access payload of non-transparent HMS push notifications?

No comments:

Post a Comment