Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Cloud Messaging last collapse_key not received (rate limited?)

I have a service that uses Firebase Cloud Messaging to communicate with its Android clients using FCM Data messages with the collapse_key parameter set. From the documentation about collapsable keys:

When there is a newer message that renders an older thread, related message becomes irrelevant to the client app and FCM replaces the older message. For example send-to-sync, or outdated notification messages.

This is what I'm looking for. I don't need all updates, only the last one is needed. But, I need it ASAP if the user is online.


However, I get a weird rate limiting that doesn't result in any HTTP error code. It is pretty easily reproducible just do 20 consecutive data messages and monitor the android FirebaseMessagingService.onMessageReceived:

for i in {1..20}; do    curl -v -X POST --header "Authorization: key=$SERVER_KEY" \        --Header "Content-Type: application/json" \        https://fcm.googleapis.com/fcm/send \        -d "{\"to\":\"$CLIENT_TOKEN\", \             \"data\":{\"counter\":\"$i\"}, \             \"priority\":\"high\", \             \"collapse_key\": \"test\" \            }" done 

The bash script above is a bit hard to read, but I have a counter variable that I'm interested in.

After a few received messages (counter=~10) it stops and you need to toggle network status to get the last message with counter=20. The last message also appears after a few minutes (normally ~10 minutes) when a firebase check-in is requested (?).

Removing collapse_key from the curl command above results in that all 20 messages are received (where counter={1..20}).


So, the question: Is this a bug? Or am I shutting down (/rate limited) because I "abuse" the interface (since all requests sends back a 200 response I thought I was ok).

like image 273
dacwe Avatar asked Jan 08 '17 00:01

dacwe


People also ask

Is there any limit for Firebase Cloud Messaging?

You can send up to 240 messages/minute and 5,000 messages/hour to a single device.

What is TTL in FCM?

They are used for the same purpose -- setting the lifespan of the payload. Difference is that ttl is the key for FCM v1 while time_to_live is for FCM Legacy. Follow this answer to receive notifications.

Does Firebase store push notifications?

Version. Firebase Cloud Messaging (FCM) is a set of tools that sends push notifications and small messages of up to 4 KB to different platforms: Android, iOS and web. This topic is useful because you use push notifications in a lot of mobile projects. Firebase is one of the simplest methods to get notifications working ...


1 Answers

<?php #API access key from Google API's Console     define( 'API_ACCESS_KEY', 'YOUR-SERVER-API-ACCESS-KEY-GOES-HERE' );     $registrationIds = $_GET['id']; #prep the bundle      $msg = array           (         'body'  => 'Body  Of Notification',         'title' => 'Title Of Notification',                 'icon'  => 'myicon',/*Default Icon*/                 'sound' => 'mySound'/*Default sound*/           );     $fields = array             (                 'to'        => $registrationIds,                 'notification'  => $msg             );       $headers = array             (                 'Authorization: key=' . API_ACCESS_KEY,                 'Content-Type: application/json'             ); #Send Reponse To FireBase Server             $ch = curl_init();         curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );         curl_setopt( $ch,CURLOPT_POST, true );         curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );         curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );         curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );         curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );         $result = curl_exec($ch );         curl_close( $ch ); #Echo Result Of FireBase Server echo $result; 

Put your data in body section.

like image 117
PRATEEK BHARDWAJ Avatar answered Oct 16 '22 00:10

PRATEEK BHARDWAJ