Huawei Push Kit API - 使用react native读取后台推送数据消息

Huawei Push Kit API - Read in background push data message with react native

在前台我可以读取数据消息,但在后台消息丢失了!

我已阅读本教程:Receiving Data Messages When the App Is in Background or Killed State

在教程中,运行 后台数据侦听器的功能是

HmsPushMessaging.setBackgroundMessageHandler

但似乎不起作用

我的测试设备是'Mediapad M5 lite 10'EMUI 8.0.0

这是我的应用程序:

  const App = () => {

  HmsPushMessaging.setBackgroundMessageHandler(dataMessage => {
    console.log('onNotificationOpenedApp '+JSON.stringify(dataMessage));
  });

  HmsPushEvent.onRemoteMessageReceived(event => {
    const RNRemoteMessageObj = new RNRemoteMessage(event.msg);
    const msg = RNRemoteMessageObj.parseMsgAllAttribute(event.msg);
    console.log('RNRemote Message received : ' + msg);
  });

  HmsPushEvent.onNotificationOpenedApp((result) => {
    console.log("[onNotificationOpenedApp]: " + JSON.stringify(result));
  });

  return (
    <SafeAreaView>
      <Text>Testing Text</Text>
    </SafeAreaView>
  );
};

我的数据推送通知正文是:

  {
    "validate_only": false,
    "message": {
        "data": "{\"pushtype\":1,\"pushbody\":{\"messageId\":\"111110001\",\"data\":\"test pass-through msg\"}}",
        "android": {
            "collapse_key": -1,
            "urgency": "NORMAL",
            "category": "PLAY_VOICE",
            "ttl": "1448s",
            "fast_app_target": 1
        },
        "token": [
            "token....."
        ]
    }
}

当应用程序处于前台时,我收到此响应:

RNRemote Message received : isDefaultSound : false
visibility : null
Ticker : null
BadgeNumber : null
When : null
Link : null
isDefaultVibrate : false
ImageUrl : null
ChannelId : null
ClickAction : null
icon : null
isLocalOnly : false
Tag : null
Sound : null
urgency : 0
data : {"pushtype":1,"pushbody":{"messageId":"111110001","data":"test pass-through msg"}}
titleLocalizationKey : null
body : null
Color : null
titleLocalizationArgs : []
title : null
bodyLocalizationArgs : []
analyticInfo : null
NotifyId : 0
contents : 0
Importance : null
bodyLocalizationKey : null
LightSettings : []
ttl : 0
receiptMode : 0
vibrateConfig : []
originalUrgency : 0
isDefaultLight : false
intentUri : null
sentTime : 0
messageId : null
dataOfMap : {pushbody={"messageId":"111110001","data":"test pass-through msg"}, pushtype=1}
messageType : null
isAutoCancel : false
from : null
sendMode : 0
to : null
analyticInfoMap : {}
token : token_censored
collapseKey : null

HMSSDK日志

I/HMSSDK_AutoInit: Push init start
I/HMSSDK_HMSPackageManager: enter checkHmsIsSpoof
I/HMSSDK_c: The local secret is already in separate file mode.
I/HMSSDK_ReadApkFileUtil: verifyMDMSignatureV3 verify successful!
I/HMSSDK_HMSPackageManager: check hms state: 2
I/HMSSDK_HMSPackageManager: Succeed to find HMS apk: com.huawei.hwid version: 60100313
I/HMSSDK_HianalyticsExist: In isHianalyticsExist, Failed to find class HiAnalyticsConfig.
I/HMSSDK_HianalyticsExist: hianalytics exist: false
I/HMSSDK_HMSBIInitializer: Builder->biInitFlag :false
I/HMSSDK_AnalyticsSwitchHolder: not ChinaROM
I/HMSSDK_AnalyticsSwitchHolder: hw_app_analytics_state value is 0
I/HMSSDK_HuaweiApi: inner hms is empty,hms pkg name is com.huawei.hwid
I/HMSSDK_HuaweiApiManager: sendRequest
I/HMSSDK_BaseHmsClient: ====== HMSSDK version: 50300301 ======
I/HMSSDK_BaseHmsClient: Enter connect, Connection Status: 1
I/HMSSDK_BaseHmsClient: connect minVersion:30000000 packageName:com.huawei.hwid
I/HMSSDK_Util: available exist: true
I/HMSSDK_Util: available exist: true
I/HMSSDK_HMSPackageManager: current versionCode:60100313, minimum version requirements: 30000000
I/HMSSDK_HMSPackageManager: MinApkVersion is disabled.
I/HMSSDK_BaseHmsClient: check available result: 0
I/HMSSDK_BaseHmsClient: enter bindCoreService, packageName is com.huawei.hwid, serviceAction is com.huawei.hms.core.aidlservice
I/HMSSDK_BinderAdapter: Enter onServiceConnected.
I/HMSSDK_BaseHmsClient: Enter onServiceConnected.
I/HMSSDK_HmsClient: post msg api_name:push.gettoken, app_id:104800037|, pkg_name:com.luca_hms_push, sdk_version:50300301, session_id:*, transaction_id:104800037ttoken20211011092403413233089, kitSdkVersion:50101303, apiLevel:1
I/HMSSDK_BaseAdapter: in baseRequest + uri is :push.gettoken, transactionId is : 104800037ttoken20211011092403413233089
I/HMSSDK_PendingResultImpl: init uri:push.gettoken
I/HMSSDK_PendingResultImpl: setResultCallback
I/HMSSDK_PendingResultImpl: setResult:0
I/HMSSDK_BaseAdapter: baseCallBack.onComplete
I/HMSSDK_HmsClient: receive msg status_code:0, error_code0, api_name:push.gettoken, app_id:104800037|, pkg_name:com.luca_hms_push, session_id:*, transaction_id:104800037ttoken20211011092403413233089, resolution:null
I/HMSSDK_HmsInstanceId: GetTokenTask receive a empty token, please check HmsMessageService.onNewToken receive result.
I/HMSSDK_HmsInstanceId: GetTokenTask receive a empty token, please check HmsMessageService.onNewToken receive result.
I/HMSSDK_AutoInit: Push init succeed
I/HMSSDK_HMSBIInitializer: Builder->biInitFlag :false
I/HMSSDK_HiAnalyticsUtils: Enable Log
I/HMSSDK_HMSBIInitializer: Builder->biInitFlag :false
I/HMSSDK_PushReceiver: push receive broadcast message, Intent:com.huawei.android.push.intent.REGISTRATION pkgName:com.luca_hms_push
I/HMSSDK_PushReceiver: receive a push token: com.luca_hms_push
I/HMSSDK_RemoteService: remote service bind service start
I/HMSSDK_HmsMessageService: start to bind
I/HMSSDK_RemoteService: remote service onConnected
I/HMSSDK_RemoteService: remote service unbindservice
I/HMSSDK_HmsMessageService: handle message start 
I/HMSSDK_HmsMessageService: onNewToken
I/HMSSDK_HmsMessageService: doOnNewToken:transactionId = null , internalCode = 0,subjectId:null,proxyType:null
I/HMSSDK_HmsMessageService: Apply token OnNewToken, subId: null
I/HMSSDK_HmsMessageService: onNewToken to host app.
I/HMSSDK_HmsMessageService: onNewToken to host app with bundle.
I/HMSSDK_HmsMessageService: start to destroy

感谢您的帮助!

test device is 'Mediapad M5 lite 10' EMUI 8.0.0

由于系统限制,EMUI版本低于10.0的设备上的应用无法后台开启,数据Message取决于EMUI版本,所以您建议使用emui大于10.0phone测试