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.
For sending FCM notification payload you can use Firebase Cloud Messaging Tool in firebase console. And click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token.
Maximum message rate to a single device For Android, you can send up to 240 messages/minute and 5,000 messages/hour to a single device.
FCM is the new version of GCM under the Firebase brand. It inherits GCM's core infrastructure to make sure we continue to deliver messages reliably on Android, iOS and Chrome. Save this answer.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With