11
votes

I've been trying to send data through Google's FCM which will expire after 10 seconds. What I mean is that if the user wasn't connected to the internet during those 10 seconds, he won't receive it.

This is my Javascript code for sending the request to the FCM:

var http = new XMLHttpRequest();
http.open("POST", "https://fcm.googleapis.com/fcm/send", true);
http.onreadystatechange = function() {};
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Authorization", "key=*****");
http.send(JSON.stringify({
    "to": "/topics/all",
    "data": {
        "Title": "Test",
        "areas": "Dan, Ayalon",
    },
    "android": {
      "ttl": "10s" //I've tried also removing the s, and changing the ttl to TTL and time_to_leave
    }
}));

The problem is that my android app still receives the data even 2 minutes later, after those 10 seconds passed. (What I did here is switching off the network ay my phone and turning it back on 2 minutes later)

I tried to send the request through the Firebase Console, and there it worked... I didn't receive the message a minute later. What am I doing wrong here?

Thank you!

Update: I thought about another solution. Maybe I'd send the current time manually, and then I'll make the client check how much time has passed? Does it sound like a good solution?

Update 2: I tried each and every example Google provided in their documentation and none of them worked. I also tried a million other ways to write the request and yet nothing worked. I'm starting to feel like it might be a bug. I reported Google and now I'm waiting for their response. I will update here their response.

2
Where are you executing that javascript? Not from within your app, are you?kasoban
@kasoban from just a random html page on my pc. The request is being sent just fine the only thing that doesnt work is the ttl.morha13
Yeah I was thinking maybe your request sending might be suppressed in some way, as the TTL should only start to be considered when the FCM server received your message, not when you tried to send it...kasoban
@kasoban I'm sending the message instantly so I think it's not the case.. Also I tried to wait for 2 minutes and I set a delay of only 10 seconds...morha13

2 Answers

26
votes

So this is the answer I received from Google's Firebase Support, they found out what I did wrong here:

Looking at your code, it seems that you have some irregularities with your request format. The headers are for the Legacy FCM API while the format of the payload is for the HTTP v1 FCM API.

If you want to use the Legacy API, you'll have to omit the Android specific payload and use time_to_live instead of ttl. But if you prefer using the HTTP v1 API, you'll have to build the request as mentioned here and use the token parameter instead of to.

The problem is that I combined two methods of requests. The headers of the requested match the Legacy API's format, while the request itself was in HTTP v1 FCM API format.

This part of the request belongs to the HTTP v1 API fomat:

"android": {
  "ttl": "10s" 
}

While the correct way to do it is without the "android" tag and using just a time_to_live tag:

"time_to_live": 10 //The time in seconds

This is how my full request looks like now:

var http = new XMLHttpRequest();
http.open("POST", "https://fcm.googleapis.com/fcm/send", true);
http.onreadystatechange = function() {};
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Authorization", "key=*****");
http.send(JSON.stringify({
    "to": "/topics/all",
    "data": {
        "Title": "Test",
        "areas": "Dan, Ayalon",
    },
    "time_to_live": 10
}));       

If you have any questions please comment

0
votes

I give my credit to @morha13. you can also check this as a complete example.