What should I specify for the authorization key in Firebase Cloud Messaging

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

That's what. I'm just trying to send a push message to users of my application in Cordoba, noticing them about an update or something like that. In the good old days (maybe a few weeks ago), this was simply called a push message. Now this thing is Firebase, they changed and renamed everything. I suppose I'm currently called “Notifications,” but there are also cloud messages, and I really don't see what the exact difference is.

Then there is a complete confusion of terminology. There seems to be an https API for sending notifications, but it is poorly documented. There are several sites that explain this, but they do not seem to reach consensus on terminology. Some mention the "API key", others - the "authentication key", Google calls it the "authorization key", but when I look at my project settings on the console, I see the "server key" and "application identifier", and google_services.json contains "private_key_id". Thus, there are a large number of WTFs.

Can someone clarify what I have to put here? Thus, the POST request should be formulated in the 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 happens to "Authorization"? As for 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 would be happy if it just appeared on every device.) Is there any good documentation for these parameters?

Also, it looks like Google assumes that everyone is working in Java and supplying only Java examples. At least I did not find anything useful for PHP, Node.js or regular JavaScript. There are many examples for all functions that I do not need.

UPDATE: I just discovered that “Authorization” should be “Server Key”, which is located on the Firebase console in my tab “Project Cloud Messages”. (It’s good that they did not hide it too much.) However, if I send POST with PHP using cURL, it returns an empty response with HTTP code 0.

+6
source share
1 answer

Well, I figured it out myself and now I feel a little less an idiot. Hope someone else makes this useful.

The value for "Authorization" should be "Server Key" found in the Firebase console on the Cloud Messages tab of the project.

The "to" parameter is required. If it is omitted, the server will respond with a simple "to." In any case, it responds in nice JSON.

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

 "to": "/topics/all" 

The server response is only if you are successful:

 {"message_id":4988221490411655075} 

Here's what 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 described here:

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

The Firebase console saves messages sent from the console itself.

For Cordoba, I use this simple plugin and it works great:

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

Here is a well-formed JSON request to send using 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); 
+16
source

All Articles