Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should I specify for Authorization key in Firebase Cloud Messaging

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.

like image 607
Tamás Polgár Avatar asked Jul 01 '16 21:07

Tamás Polgár


1 Answers

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);
like image 128
Tamás Polgár Avatar answered Oct 06 '22 13:10

Tamás Polgár