Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access payload of HMS push notifications?

I have published an Android app on Huawei AppGallery and am able to send push notifications 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=MY_APP_ID&
client_secret=MY_APP_SECRET

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":"MY_APP_ID"}) -

https://api.push.hicloud.com/pushsend.do?nsp_ctx=
    %7B%22ver%22%3A%221%22%2C+%22appId%22%3A%22101130655%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.

like image 444
Alexander Farber Avatar asked Sep 09 '19 20:09

Alexander Farber


1 Answers

Thanks to the nice help provided by Huawei developers (after sending them an adb shell setprop log.tag.hwpush VERBOSE log) everything is resolved now -

In AndroidManifest.xml I have added a custom scheme app (could be any string) as explained in the Google doc Create Deep Links to App Content:

<activity android:name="de.slova.MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="app" android:host="slova.de" />
    </intent-filter>
</activity>

In MainActivity.java I have added a code for parsing intents:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // getIntent() should always return the most recent intent
    handleIntent(intent);
}

private boolean handleIntent(Intent intent) {
    try {
        String gidStr = intent.hasExtra("gid") ?
                intent.getStringExtra("gid") :             // FCM notification
                intent.getData().getQueryParameter("gid"); // HMS notification
        Log.d(TAG, "handleIntent gidStr=" + gidStr);
        int gid = Integer.parseInt(gidStr);
        // show the game when user has tapped a push notification
        showGame(gid);
        return true;
    } catch (Exception ex) {
        Log.w(TAG, "handleIntent", ex);
    }

    return false;
}

By the way I clear push notifications when the app is started:

@Override
public void onResume() {
    super.onResume();

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.cancelAll();
}

Finally in my game backend I first acquire token by POSTing to https://login.cloud.huawei.com/oauth2/v2/token

grant_type=client_credentials&
client_secret=MY_APP_SECRET&
client_id=MY_APP_ID

And then POST the notification https://push-api.cloud.huawei.com/v1/MY_APP_ID/messages:send using the headers Authorization: Bearer MY_TOKEN and Content-Type: application/json; charset=UTF-8. The JSON format is described at HMS PushKit doc.

{ 
   "message": { 
      "android": { 
         "notification": { 
            "image": "https://slova.de/ws/board3?gid=108250",
            "title": "Game 108250",
            "body": "Alexander: Test chat msg",
            "click_action": { 
               "type": 1,
               "intent": "app://slova.de/?gid=108250"
            }
         }
      },
      "token": [ 
         "1234567890123456789000000000DE01"
      ]
   }
}

The HMS SDKs I currently use in my build.gradle currently are:

implementation "com.huawei.hms:base:3.0.0.301"
implementation "com.huawei.hms:hwid:3.0.0.301"
implementation "com.huawei.hms:push:3.0.0.301"
implementation "com.huawei.hms:iap:3.0.0.301"

This works well for my word game: a push notification arrives, with the title, body and small image. Then the game #108250 is opened by my app, when the user taps it:

app screenshot

like image 123
Alexander Farber Avatar answered Sep 18 '22 16:09

Alexander Farber