My Ionic capacitor is set as per these instructions. It is able to receive data messages just fine when the app is in the background or foreground. But the background.js script is not being called when the app is killed.
this.hmsPush.setBackgroundFile("public/assets/background.js")
background.js
var data = {
"key":"value"
};
onGetItemResponse((result)=>{
console.log(JSON.stringify(result));
});
onBackgroundRemoteMessageReceived((remoteMessage)=>{
const jsonData = JSON.parse(remoteMessage.data);
const headlessNotification = {
"title": "[Headless] " jsonData.title,
"message": jsonData.message.replace("{{name}}","YourName")
};
const notification = JSON.stringify(headlessNotification);
HmsLocalNotification.backgroundLocalNotification(notification);
});
HmsPush.setItem("test",JSON.stringify(data));
HmsPush.getItem("test");
HmsPush.removeItem("test");
Update 1 Here is the log when I try to send and data message and the app is already killed.
This line is suspicious
04-25 10:20:01.809 W/ActivityManager( 1461): Service starting has been prevented by iaware or trustsbase sInfo ServiceInfo{8882f89 com.huawei.hms.cordova.push.remote.HmsPushMessageService}
04-25 10:20:01.776 I/PushLog110105309( 5138): [SocketRead_09:20:20-233]enter cancelAlarm(Action=com.huawei.android.push.intent.RESPONSE_FAIL
04-25 10:20:01.778 I/PushLog110105309( 5138): [SocketRead_09:20:20-233]process cmdid to receive from pushSrv:44, subCmdId:FF
04-25 10:20:01.783 I/PushLog110105309( 5138): [SocketRead_09:20:20-233]dispatchIntent over
04-25 10:20:01.785 I/PushLog110105309( 5138): [ReceiverDispatcher-209]process push message cmdid from pushSrv:44, subCmdId:FF
04-25 10:20:01.796 I/PushLog110105309( 5138): [ReceiverDispatcher-209]msgType:3 [0:PassBy msg, 1:System notification, 2:normal notification, 3:HCM PassBy msg],userId:0
04-25 10:20:01.799 I/PushLog110105309( 5138): [ReceiverDispatcher-209] closeTokenCheck: false
04-25 10:20:01.802 I/PushLog110105309( 5138): [ReceiverDispatcher-209]app declare HmsMessageService num is: 1
04-25 10:20:01.802 I/PushLog110105309( 5138): [ReceiverDispatcher-209]judge process is running start
04-25 10:20:01.805 I/PushLog110105309( 5138): [ReceiverDispatcher-209]no running process.
04-25 10:20:01.805 W/PushLog110105309( 5138): [ReceiverDispatcher-209]process com.**** not exist and control 0
04-25 10:20:01.805 I/PushLog110105309( 5138): [ReceiverDispatcher-209]tempDozeWhileListTime = 10000
04-25 10:20:01.805 I/PushLog110105309( 5138): [ReceiverDispatcher-209]add com.**** to doze temp white list
04-25 10:20:01.807 I/PushLog110105309( 5138): [ReceiverDispatcher-209]do not need to apply net.
04-25 10:20:01.809 W/ActivityManager( 1461): Service starting has been prevented by iaware or trustsbase sInfo ServiceInfo{8882f89 com.huawei.hms.cordova.push.remote.HmsPushMessageService}
04-25 10:20:01.809 W/PushLog110105309( 5138): [ReceiverDispatcher-209]start remote message service failed
04-25 10:20:01.813 I/PushLog110105309( 5138): [ReceiverDispatcher-209]message 0A3680A0490F8108 is cached success: true
04-25 10:20:01.814 I/PushLog110105309( 5138): [ReceiverDispatcher-209]send passby message by start service for: com.****,msgID:0A3680A0490F8108
04-25 10:20:01.814 I/PushLog110105309( 5138): [ReceiverDispatcher-209]process cmdid to send to pushSrv:45, subCmdId:FF
04-25 10:20:01.815 I/PushLog110105309( 5138): [ReceiverDispatcher-209]send msg to remote srv success
04-25 10:20:01.815 I/PushLog110105309( 5138): [ReceiverDispatcher-209]handleMessageResponse the response msg is :45,msgId:0A3680A0490F8108,displayPkgName:com.****,flag:1B
04-25 10:20:01.815 I/PushLog110105309( 5138): [ReceiverDispatcher-209]enter AlarmTools:setInexactAlarm(intent:Intent { act=com.huawei.action.push.intent.CHECK_CHANNEL_CYCLE pkg=android } interval:1200000ms
CodePudding user response:
In killing state, can you find this information in log? " ** onMessageReceived ** "
You can use this command to catch log: adb logcat -c && adb logcat -vtime > logcat1.txt
CodePudding user response:
the log tells the root cause. 04-25 10:20:01.809 W/ActivityManager( 1461): Service starting has been prevented by iaware or trustsbase sInfo ServiceInfo{8882f89 com.huawei.hms.cordova.push.remote.HmsPushMessageService}
Solution is below. Just add a delay when starting
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getContext() ,ServiceName.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
}
},1000);
please refer to this url for more info: https://blog.katastros.com/a?ID=01750-e3e353a0-ceec-4138-b2aa-0c221be37c13