Nodejs Firebase cloud messaging delayed delivery on android

Viewed 107

I'm trying to push some high-priority data messages from a nodejs backend to the android client. Here is my code,

var payload = {
        data
      };

var options = {
        priority: "high",
        timeToLive: 0,
        ttl: 0,
        android: {
          priority: "high",
          timeToLive: 0,
          ttl: 0,
        },
      };

   admin.messaging()
        .sendToDevice(deviceFcmId, payload, options)
        .then(function (response) {})
        .catch(function (response) {
          console.error("error sendNotification", response);
        });

As you can see, I'm using priority "high" and timeToLive: 0. But this does not wake up the device if it is in Doze mode, or sometimes even if the device is in an unlocked state, the message is delayed up to a few minutes. This is a voice-calling app, so the delivery delay should be minimal. I've searched similar posts on StackOverflow but nothing works for me. What am I missing here? Thanks in advance.

UPDATE: In my app, there are two types of notifications, Chat notifications and Call notifications. When the user receives a chat notification, the app will write a Realm database entry and fetch the sender's profile picture from API, and shows the notification. I know it's a network call, but the thing is, the image fetching is managed by Glide so 90% of the time it will be a disk cache read rather than a network call (even the network call has a 1-second timeout, after this timeout, it will return a local image).

When the user receives a call notification, the app will start a service and ping the caller using socket.io. If the ping is a success it will show the call screen using a fullscreen intent by calling startForeground from the service. If the ping fails it will show a miscall notification and stops the service.

So, no matter whichever the case is there will always be a user-facing notification.

1 Answers

From the official docs

High priority messages on Android are meant for time sensitive, user visible content, and should result in user-facing notifications. If FCM detects a pattern in which messages do not result in user-facing notifications, your messages may be deprioritized to normal priority. FCM uses 7 days of message behavior when determining whether to deprioritize messages; it makes this determination independently for every instance of your application. If, in response to high priority messages, notifications are displayed in a way that is visible to the user, then your future high-priority messages will not be deprioritized.

This means your android app should pretty much just show a notification (make sure the app has notifications enabled). If you abuse this and instead try to directly do other things when receiving the push (such as open connections to the backend or do any other computationally-expensive task) then your messages might be downgraded to normal priority.

On a single device, you can verify if a message has been deprioritized by checking if getPriority() returns a lower value than getOriginalPriority(). And for all messages, you can query the Data API.

Related