1

I'm trying to display a data message on my Android tray when the app is closed by users, i.e. when the user drags app to the side in recent app list. Althought, data messages are just being received like this:

  1. App is closed. Data message sent.
  2. Message is not received when app is closed.
  3. App is opened.
  4. After 4~5 minutes data message is received and displayed in Android tray.

To achieve that, I'm using react-native-firebase lib, following its docs.

import firebase from 'react-native-firebase';
import type { RemoteMessage } from 'react-native-firebase';

export default async (message: RemoteMessage) => {

    const notifPromise = new Promise((resolve, reject) => {

         let notification = new firebase.notifications.Notification();
              notification.android.setPriority(firebase.notifications.Android.Priority.High);
              notification.android.setChannelId("test-channel");

        resolve(firebase.notifications().displayNotification(notification));
    }); 

    console.log("MESSAGE IN BACKGROUND OR APP CLOSED");

    return notifPromise.resolve();
}

The code above works fine in the background, I mean when the app is just "minimized" to the secondary plan.

AndroidManifest.xml, HeadlessTask and MainApllication.java are theoretically in accordance with docs. I'm just showing blank UI in Android tray to test.

Message being sent from Postman:

{
"to": "erVxmCT6rgA:APA91bGn6q9...",

"data": {
    "custom1": "custom1",
    "custom2": "custom2"
   }
}

Questions: What could be wrong once it works on background? Why is this behavior happening?

Taylon Assis
  • 332
  • 1
  • 6
  • 16

1 Answers1

1

After deep searching, I could figure it out.

I was running app in a ASUS device. Accord to this stackoverflow answer, ASUS smartphones have a perfomance trick gain more battery life, so when you swipe away a app from the recent apps list, you are forcing app to stop. Thus, the HeadlessJSTask responsible for receiving the messages is taken from the list of android processes.

I have also made some edits in my codes:

Fixing Promise:

export default async (message: RemoteMessage) => {

    const notification = new firebase.notifications.Notification();
              notification.android.setPriority(firebase.notifications.Android.Priority.High);
              notification.android.setChannelId("test-channel");
              notification.setTitle(message.data.custom1);

        firebase.notifications().displayNotification(notification);

    return Promise.resolve(message);
}

Setting high priority in our data message (it is necessary for background and app closed messages):

{

"to": "cKUNOaOnvaY:APA91bFVAPLSuHogmZfxc1GlhqOhEkxcscoRvZxRrzno0XjyDkqYZVmNqJVL4v6mcQgH4p9zt9Zxz5aDugCjNy7CBg_pbXb8u8X6336K0x6WffdXoGOl50lCtHt46oS78Yyc9XM3gPJQ",

"data": {
    "custom1": "custom1",
    "custom2": "custom2"
},

"priority": "high"

}
Taylon Assis
  • 332
  • 1
  • 6
  • 16