简体   繁体   中英

Firebase Cloud messaging : random delay between sending and reception of message?

I am currently working on an Android application for school. Basically, one of its purposes is for you and your friends to be able to locate each other on a map. To make this happen, we use Firebase Cloud Messaging. I followed the tutorial provided by Google , and everything seems to work fine.

I set up the FirebaseMessagingService as shown in the tutorial, overridden onNewToken, onMessageReceived and onMessageDeleted. The token is saved in our school DB, so I can check in real time which accounts is linked to which Firebase token, allowing me to test a device with the FCM console.

This is where the problem occurs: I receive the messages, but there's some kind of random delay. Sometimes the messages come instantly, and some other times we have to wait for more than 20 minutes, without even being sure whether or not the problem comes from our implementation or from Firebase. We have absolutely no idea why this delay appears. Sometimes we just think our application is broken, and do something else, but then we come back to Android Studio and see the app received all the pending messages at once.

Here is my service declaration in the manifest:

<service android:name=".domains.FirebaseService" android:enabled="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
</service>

And here's my implementation of FirebaseMessagingService

public class FirebaseService extends FirebaseMessagingService {

    [...]        

    @Override
    public void onNewToken(String token) {
        // Save on DB
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Map<String, String> data = remoteMessage.getData();
        if(data.size() > 0) {
            // Do stuff
        }
    }

    [...]
}

So this seems pretty logical, nothing fancy here. Still, the delay before receiving any message is fully random and doesn't seem to be linked with the service status.

Again, the problem occurs even when sending the message from the Firebase console, so I think we can safely exclude the possibility that the problem comes from sending the message, since in the end, the application receives 80% of the time the message.

Since I receive the messages, I don't think the problem actually comes from the service... Maybe I'm wrong ?

Also, I'm pretty sure that the Firebase token doesn't change before and after the message is sent, which would explain why the device doesn't receive the message.

Do you have any idea where this delay could come from ? We've searched all over the internet to look for a solution, without finding any answer.

Thank you in advance.

EDIT: For what it's worth, I'm sending every message using the Firebase token of the device, and in our case we are using an emulator, so maybe that could be a reason why it's not working fine.

EDIT 2: As I mentioned in the comments below, our school expects us to use FCM and nothing else, so we need to make it work with it.

Since my phone is a potato, it wouldn't let me install my application on it. Until now. And now that the code runs on my phone and not using the emulator, everything works perfectly fine. The delay is gone and all the messages are received instantly.

What surprises me is that I had configured my emulator as it should be (with Google Play services, etc.) and did what was necessary to make sure there were no problems. Having searched on the web, I couldn't find any topics talking about this without the answer being simply to install Google Play services on the emulator (which was already done).

So, in case your emulator doesn't receive any push notifications, or it happens that there's some kind of random delay before receiving them, you'd be better off trying your application on a real device.

Many thanks to @madlymad and @AL. for trying to help me solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM