Reputation: 1937
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.
Upvotes: 12
Views: 11530
Reputation: 4227
I give my credit to @morha13. you can also check this as a complete example.
Upvotes: 0
Reputation: 1937
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
Upvotes: 30