当应用程序被杀死或在后台时,华为 PushKit 通知不会出现

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

我已经在我的应用中集成了华为推送,当我从华为推送控制台触发通知时,我可以在应用处于后台时收到通知。但是,当我们的系统后台触发华为API推送通知时,应用程序在后台时不会出现。

尽管应用程序处于前台或后台,但以下代码仍在执行,但通知参数(如标题等)为空。通知 object 本身不为空。

JSON 消息的内容可以作为单个字符串从 remoteMessage.getData() 接收,但值未映射到相应的字段。

 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()
            
            }
        }
    
   }

我们后台执行华为提供的this API发送数据报文

这是我们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"
   }
}

更新

典型数据信息示例代码:

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

详情见Docs


Push Kit支持两种类型的消息:通知消息和数据消息。

设备收到数据消息后,设备会将其传输到您的应用程序,而不是直接显示消息。然后,您的应用会解析消息并触发相应的操作。 Push Kit 只是一个通道,数据消息的发送取决于你应用的常驻状态。但是,即使您的应用未启动,通知消息仍然可以发送。

为了省电和不打扰用户,您的应用在停止后不会被Push Kit启动,也不会向您的应用下发数据消息。在这种情况下,您可以根据您的服务决定是否使用通知消息

发件人:

或者,您可以设置High-priority data messages强制启动已停止的应用程序来接收和处理消息。