HMS-Core / hms-react-native-plugin

This repo contains all of React-Native HMS plugins.
https://developer.huawei.com/consumer/en/doc/overview/HMS-Core-Plugin?ha_source=hms1
Apache License 2.0
240 stars 68 forks source link

Background handler does not always work #183

Closed ngseba closed 1 year ago

ngseba commented 2 years ago

Description I've got 2 issues so far with background data messages.

  1. When the app is in KILLED state the service does not open whatsoever, I can see the logs in flipper that the notification is received, but that's pretty much it.
  2. When the app is in BACKGROUND state the service opens sometimes, and after a while, when the app is still in background it fails. I noticed that when the app is in background and it works, it takes about 5 minutes for it to stop working.

Expected behavior The background handler should work when app is in background and killed state, all the time, without failures.

Current behavior It just fails to launch and it logs an error if the app is in BACKGROUND, if it's in KILLED state it does not log an error, however it doesn't do anything.

Code This is my background handler which calls the method to display the data_messages as notifications.

static async showHuaweiNotifications(dataMessage: RemoteDataMessageObject['msg']) {
        return HmsLocalNotification.localNotification({
            [HmsLocalNotification.Attr.title]: dataMessage.title,
            [HmsLocalNotification.Attr.message]: new RNRemoteMessage(dataMessage).getData(),
        });
    }

 HmsPushMessaging.setBackgroundMessageHandler(async remoteMessage =>
        HmsProvider.showHuaweiNotifications(remoteMessage),
    );

The background handler is in the index.js of the application.

This is the body of the remote message that I'm sending through postman. I am sending an actual token there.

{
    "validate_only": false,
    "message": {
        "data": "{\"url\":\"https://www.google.com\"}",
        "token": [ "ACTUAL_TOKEN"]
    },
       "android": {
            "urgency": "HIGH"
    }
}

Logs

The following log shows the case for which the app is in BACKGROUND state and it actually works and shows the notification.

18:31:21.587 HmsPushMessageService ** onMessageReceived **
18:31:21.662 Notfiy_NotificationPushListener
1200103310|R|W|handleNotificationInDaemonService Notification is StatusBarNotification(pkg=com.app.my user=UserHandle{0} id=-2026250689 tag=null key=0|com.app.my|-2026250689|null|10243: Notification(channel=huawei-hms-rn-push-channel-id-4-default-250 pri=1 contentView=null vibrate=null sound=null defaults=0x4 flags=0x10 color=0x00000000 category=call vis=PUBLIC))

This one is 5 minutes later when it stops working.

18:35:23.462 HmsPushMessageService ** onMessageReceived **
18:35:23.467 ActivityManager
Background start not allowed: service Intent { cmp=com.app.my/com.huawei.hms.rn.push.services.MessagingHeadlessService (has extras) } to com.app.my/com.huawei.hms.rn.push.services.MessagingHeadlessService from pid=14912 uid=10243 pkg=com.app.my
18:35:23.470 HmsPushMessageService Change Priority to 'high'
java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.app.my/com.huawei.hms.rn.push.services.MessagingHeadlessService (has extras) }: app is in background uid UidRecord{af875b9 u0a243 SVC  bg:+3m36s938ms idle change:uncached procs:1 seq(0,0,0)}
    at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1753)
    at android.app.ContextImpl.startService(ContextImpl.java:1709)
    at android.content.ContextWrapper.startService(ContextWrapper.java:657)
    at com.huawei.hms.rn.push.remote.HmsPushMessageService.onMessageReceived(HmsPushMessageService.java:51)
    at com.huawei.hms.push.HmsMessageService.doMsgReceived(HmsMessageService.java:2)
    at com.huawei.hms.push.HmsMessageService.handleIntentMessage(HmsMessageService.java:11)
    at com.huawei.hms.push.HmsMessageService$a.handleMessage(HmsMessageService.java:7)
    at android.os.Handler.dispatchMessage(Handler.java:109)
    at android.os.Looper.loop(Looper.java:166)
    at android.app.ActivityThread.main(ActivityThread.java:7555)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)

I can see the error that is Change Priority to 'high' however I am unsure of where should I change this priority, I tried changing the body of the message by adding urgency:HIGH but no luck.

Environment

ozcanozgur commented 2 years ago

Hi @ngseba,

We advise you to update the plugin version to the latest. If the issue persists, you can try Huawei AppGallery connect to send data message.

You may also check the examples from the native documentation if you would like to send data message with Postman.