6

Can you guys help me (and others who are just as frustrated as I am) to get how Google Firebase works straight? The documentation is so confusing that it makes me feel outright an idiot.

Here is the thing. I am simply trying to send a push message to the users of my Cordova app, noticing them about an update or something. In the good old times (maybe a few weeks ago) it was simply called a push message. Now there is this Firebase thing, they changed and renamed everything. I guess what I need is currently called Notifications, yet there is also Cloud Messaging and I don't really see what is the exact difference.

Then there is a total confusion of terminology. Apparently there is a https API to send notifications, but it's poorly documented. There are a couple of sites explaining it, but they don't seem to reach a consensus on terminology. Some mention an "API key", others an "authentication key", Google itself calls it an "authorization key", but when I look into my project settings on the console I see a "server key" and an "app ID" while the google_services.json file contains a "private_key_id". So there is a good amount of WTF.

Can someone clarify what the heck should I supply here? This is how a POST request should be formulated to Firebase API:

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA  <-- WHAT IS THIS?

{ "data": {
    "score": "5x1",
    "time": "15:10"
 },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..." <-- AND THIS?
}

What goes to "Authorization"? What goes to the "to" value if I want to send my notification to all users, and what if I want to target a specific group? (For starters I'd be happy if it simply arrived to every device.) Is there a good documentation for these parameters?

Furthermore seemingly Google assumes everybody works in Java and supplies Java examples only. At least I haven't found anything usable for PHP, Node.js or plain JavaScript. There are plenty of examples for all the functions I don't need.

UPDATE: I just found that the "Authorization" should be the "Server Key" found on the Firebase console under my project's Cloud Messaging tab. (Good thing they didn't hide it too much.) Still, if I send a POST from PHP with cURL, it returns an empty response with HTTP code 0.

1 Answer 1

18

OK, I figured it out myself and feel a little less an idiot now. Hope some who still do will find it useful.

The "Authorization" value should be the "Server Key" found on the Firebase console under my project's Cloud Messaging tab.

The "to" parameter is mandatory. If it's omitted, the server will respond with a simple "to". Anyway it responds in nice JSON.

If you want to send a message to all devices, you should specify:

"to": "/topics/all"

Server response is only this if you're successful:

{"message_id":4988221490411655075}

Here is how it looks like in case of any error:

{"multicast_id":5691752204334485119,
 "success":0,
 "failure":1,
 "canonical_ids":0,
 "results":[{
               "error":"MissingRegistration"
            }]
}

Parameters and return values are explained here:

https://firebase.google.com/docs/cloud-messaging/http-server-ref

The Firebase console only retains messages sent from the console itself.

For Cordova I am using this simple plugin and it works fine:

https://www.npmjs.com/package/cordova-plugin-fcm

Here is a well-formed JSON request to be sent with cURL in PHP (also included):

$json_data = '{ "data": { 
                  "price": "1000",
                  "currency": "USD" 
                },
                "notification": {
                  "title": "Hey you got a message",
                  "body": "Your mother stil loves you",
                  "sound": "default",
                  "click_action": "FCM_PLUGIN_ACTIVITY",
                  "icon": "icon_name"
                },
                "to": "/topics/all",
                "priority": "high"
              }';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                                            'Content-Type: application/json',                                                                                
                                            'Content-Length: '.strlen($json_data),
                                            'Authorization:key=AIzxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'  
                                          ));           
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec($ch);
curl_close($ch);
3
  • you sir are a life saver :)
    – Kushan
    Nov 9, 2016 at 13:39
  • This is a great help. Thank you so much. I have some doubts about how I should fetch the Server Key. So is it a good idea to hardcode the key in the app or we can get the value programmatically somehow? Oct 13, 2017 at 19:10
  • It's just a string. You can get it from wherever you wish. Obviously this here is just an example, I actually put it into a global variable. Oct 28, 2017 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.