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.