Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Delay in Receiving message in FCM(onMessageReceived)

In Test app, I have implemented FCM for sending notification messages using

https://github.com/firebase/quickstart-android/tree/master/messaging

For testing I sent the message(8:42 PM) from firebase console using the "New Message" under Notification.

Sent at 8:42 PM

But in my emulator, I have received the message at 9:06 PM

received at 9:06 PM

Please let me know if there is anything to do reduce the delay.

Thank you.

like image 471
karsas Avatar asked Aug 02 '16 16:08

karsas


People also ask

How do I test FCM push notifications Android?

Send a test notification messageOpen the Notifications composer and select New notification. Enter the message text. Select Send test message. In the field labeled Add an FCM registration token, enter the registration token you obtained in a previous section of this guide.

What is onMessageReceived?

onMessageReceived is provided for most message types, with the following exceptions: Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device's system tray. A user tap on a notification opens the app launcher by default.

What is Senderid in FCM?

The Sender ID is used in the client side application to register to FCM from the device. Copy the Server Key. You must provide the Server Key in the Engagement server while configuring an application to send push messages.

How do you handle notifications when an app is in foreground in firebase?

Notification messages When your app is in the foreground, the FCM notification message is delivered to the onMessageReceived() handler and you can handle it by posting a notification if needed or update the app content with the FCM payload data (Max 4KB) or fetch content from app server.


2 Answers

This could be caused by the unrealistic heartbeat intervals in Firebase Cloud Messaging.

FCM works by maintaining an idle socket connection from an Android device to Google’s servers. This is great because it barely consumes battery power (contrary to polling), and it allows the device to be woken up instantly when a message arrives.

To make sure that the connection remains active, Android will send a heartbeat every 28 minutes on mobile connection and every 15 minutes on WiFi. If the heartbeat failed, the connection has been terminated, and FCM will re-establish it and attempt to retrieve any pending push notifications. The higher the heartbeat interval, the less battery consumed and the less times the device has to be woken up from sleep.

However, this comes at a great price: the longer the heartbeat interval, the longer it takes to identify a broken socket connection. Google has not tested these intervals in real-life situations thoroughly enough before deploying FCM. The problem with these intervals is caused by network routers and mobile carriers, who disconnect idle socket connections after a few minutes of inactivity.

More info is available on my blog:

http://eladnava.com/google-cloud-messaging-extremely-unreliable/

As a workaround, please consider Pushy (https://pushy.me), a drop-in replacement for GCM/FCM which greatly improves notification speed & reliability (Full disclosure - I founded Pushy).

like image 53
Elad Nava Avatar answered Oct 13 '22 16:10

Elad Nava


My best guess is that this has to do with the message priority.

From the docs:

Setting the priority of a message

You have two options for assigning delivery priority to downstream messages on Android: normal and high priority. Delivery of normal and high priority messages works like this:

Normal priority. This is the default priority for data messages. Normal priority messages won't open network connections on a sleeping device, and their delivery may be delayed to conserve the battery. For less time-sensitive messages, such as notifications of new email or other data to sync, choose normal delivery priority.

High priority. This is the default priority for notification messages. FCM attempts to deliver high priority messages immediately, allowing the FCM service to wake a sleeping device when possible and open a network connection to your app server. Apps with instant messaging, chat, or voice call alerts, for example, generally need to open a network connection and make sure FCM delivers the message to the device without delay. Set high priority if the message is time-critical and requires the user's immediate interaction, but beware that setting your messages to high priority contributes more to battery drain compared with normal priority messages.

I am not certain, but I believe that normal priority is used when you send a message to "All Android devices", which it seems you are doing in the question above. It is also possible that it is being sent to a FCM topic, which is optimized for throughput rather than latency

So, setting the priority to high, or sending to one specific device rather than a topic, should reduce the delay you are seeing.

Also; Be aware that push messages are based on best effort. There are no guarantees that the message will be delivered by a given time, or at all.

like image 7
Automatico Avatar answered Oct 13 '22 17:10

Automatico