1

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.

SnooZe21
  • 31
  • 7
  • 1
    Hi @SnooZe21 and welcome to SO. Could this be a case of throttling https://stackoverflow.com/a/39658639/944070 ? – madlymad Dec 27 '18 at 15:12
  • Hi @madlymad, after googling a little bit about throttling in FCM, I have no idea how to check it out. How would you check if this is a case of throttling ? – SnooZe21 Dec 27 '18 at 15:37
  • I don't know if you can check that in any way. But if this happen when you are exchanging muliple messages then its most probable the throttling. – madlymad Dec 27 '18 at 15:43
  • Can throttling happen if it only involves 2 messages ? (A requests B location, B location gets the requests and answers by sending its location to A, all through our back end REST application using Admin SDK). Also, it does happen when sending a single message through the console.. – SnooZe21 Dec 27 '18 at 15:48
  • I would say that 2 messages are not enough to be throtteled I've been thinking something like 10messages/second or that something is sending too many on a loop. I've also found this on github there are some user's claiming the same problem as the one you mentioned. Maybe getting through that steps help you understand better what is going on https://github.com/firebase/quickstart-android/issues/83 – madlymad Dec 27 '18 at 15:54
  • After looking into the link you have given, we added a notification with the message, and set the TTL to 25 seconds. First time it was OK so we thought it was fixed, but now it seems like the delay just came back.. And now that the TTL is set to 25 seconds in our backend server, the messages can't be received since the delay is much more important than the time to live. – SnooZe21 Dec 27 '18 at 16:32
  • Have you tried with 0 TTL? Check here the documentation https://firebase.google.com/docs/cloud-messaging/concept-options#ttl I suspect that as long as you are using a simulator there maybe something different there. But we may reject the possiblity of that behavior due to doze or bad network or suspension on the application due to masive battery consumption, I don't think that any of these applies on emulators. – madlymad Dec 27 '18 at 22:45
  • Yes, I did try with 0 TTL and unfortunately it didn't seem to go any better.. That's why we set it to 25000 milliseconds, so that the location is still relevant while the message has the time to be "safely" received.. – SnooZe21 Dec 27 '18 at 23:50
  • "*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.*" -- FCM is primarily used for Push Notifications. For a feature like that, I think you're better off using Firebase Realtime Database. – AL. Dec 28 '18 at 04:58
  • Hi @AL, first of all thank you for answering. I think that using Firebase Realtime Database would be more appropriate if localization is used frequently (almost in real time). It is true that I forgot to mention that in our case, the user must ask for a friend's location, and once he gets it he asks again every 60 seconds (or even more), which means that we will not (in my opinion, I might be wrong) consume too much battery and make too frequent use of push notifications. – SnooZe21 Dec 28 '18 at 11:12
  • In addition, our school did not require us to use FCM, but it implicitly expects us to use it, because the teachers know only that and the other students have all used it without any problems. That's why I would like to try to solve the problem using Cloud Messaging. I honestly have no idea why not all our messages are received instantly... – SnooZe21 Dec 28 '18 at 11:12
  • From how you mentioned it, the data you're sending is *app critical*. FCM sends the message(s) as soon as feasible, however, it is not advised to hold time sensitive info/app critical data, since there are factors that may or may not contribute to the delay that a message is received -- in some cases/devices, the message is not received at all ([see this post](https://stackoverflow.com/q/39504805/4625829)). -- from my [answer here](https://stackoverflow.com/a/48655342/4625829) – AL. Dec 28 '18 at 11:28
  • So what you're suggesting is that the service stops prematurely and is therefore not able to receive the messages intended for it? Because, in the tests we perform, we specifically leave the applications open, so there is no reason for the service to shut down, since we do not kill/swipe off the application. Am I wrong ? – SnooZe21 Dec 28 '18 at 12:12

1 Answers1

2

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.

SnooZe21
  • 31
  • 7
  • Generally, testing on emulator with real systems is not the suggested way to go. Additionally, profecionally I rarely use emulators so I hardly know about such problems ;-) – madlymad Dec 29 '18 at 15:45
  • 1
    Yes I guess you're right, and I learnt my lesson the hard way :p – SnooZe21 Dec 30 '18 at 11:50
  • yes , the android studio emulator with push notification takes his time in very annoying way. i thinking about trying another emulator .. – Guy Jan 09 '19 at 12:01