Search code examples
huawei-mobile-serviceshuawei-developershuawei-push-notification

Huawei PushKit Notifications doesn't appear when app is killed or in background


I have integrated the Huawei Push kit to my app and when I trigger a notification from Huawei Push Kit Console, I can receive the notification when the app is in background. However, when our system backend triggers Huawei API to push notifications, it doesn't appear when the app is in the background.

Following code is getting executed despite the app is in foreground or background but Notification parameters such as Title etc, coming as null. Notification object itself is not null.

Contents of the JSON message can be received as a single String from remoteMessage.getData() but values does not mapped to respective fields.

 public class HuaweiNotificationHandler extends HmsMessageService{
    
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            Log.i(TAG, "getData: " + remoteMessage.getData()
                    
            RemoteMessage.Notification notification = remoteMessage.getNotification();
            if (notification != null) {
                Log.i(TAG, "getTitle: " + notification.getTitle()
            
            }
        }
    
   }

Our backend executes this API provided by Huawei to send data messages.

This is the format of our JSON

  {
   "collapseKey":"dummykey",
   "priority":"high",
   "delayWhileIdle":false,
   "dryRun":false,
   "sound":"",
   "contentAvailable":true,
   "data":{
      "data":{
         "type":"A",
         "id":"1111111",
         "entity":"0",
         "url":""
      },
      "restrictedPackageName":"com.aa.bb.cc" // this package name is exactly same as the huawei app package registered
   },
   "notification":{
      "title":"Notification Title",
      "icon":"ic_launcher",
      "body":"Message"
   }
}

Solution

  • UPDATE

    Sample code of a typical data message:

    {
        "validate_only": false,
        "message": {
            "data": "{'param1':'value1','param2':'value2'}",
            "token": [
                "pushtoken1",
                "pushtoken2"
            ]
        }
    }
    

    For Details,see Docs.


    Push Kit supports two types of messages: notification messages and data messages.

    After a device receives a data message, the device transfers it to your app instead of directly displaying the message. Your app then parses the message and triggers the corresponding action. Push Kit only functions as a channel, and the delivery of data messages depends on the resident status of your app. However, notification messages can still be delivered even if your app is not launched.

    For the sake of saving power and not disturbing users, your app will not be launched by Push Kit after being stopped, and no data messages can be delivered to your app. In this case, you can determine whether to use notification messages based on your services.

    From: https://stackoverflow.com/a/64100678/14006527

    Alternatively, you can set High-priority data messages to forcibly launch your stopped app to receive and process the messages.