Search code examples
androidreact-nativehuawei-mobile-serviceshuawei-developershuawei-push-notification

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


In foreground i can read data message, but in background message was lost!

I have read this tutorial : Receiving Data Messages When the App Is in Background or Killed State

In the tutorial the function for run in background the data listener is

HmsPushMessaging.setBackgroundMessageHandler

But not seem working

My test device is 'Mediapad M5 lite 10' EMUI 8.0.0

This is my my app:

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

The body of my data push notification is:

  {
    "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....."
        ]
    }
}

when app is in foreground i receive this response:

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 LOG

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

Thanks for any help!


Solution

  • test device is 'Mediapad M5 lite 10' EMUI 8.0.0

    Due to system limitations, Apps on devices whose EMUI version is earlier than 10.0 cannot be enabled in the background, and the data Message depends on the EMUI version, so you are advised to use whose emui is greater than 10.0 phone to test.