Home » Php » Send informations via Firebase Cloud Messaging

Send informations via Firebase Cloud Messaging

Posted by: admin June 30, 2018 Leave a comment

Questions:

I am currently trying to make use of cloud messaging for my Android app. The server is sending messages via PHP using php-curl.

The problem is that the server’s response is always:

401: Unauthorized

I use the WebAPI Access key from the Firebase Console for my App, so this is definitively the right one. Below is the code I use to send the data:

<?php

$fields = array(
    'to' => "<MY-RECIPIENT-TOKEN>",
    'notification' => array(
        'body' => 'Test message :)',
        'title' => 'Test',
        'icon' => 'myicon',
        'sound' => 'mySound'
    )
);

$headers = array(
    'Authorization:key=<MY AUTH KEY IS HERE>',
    'Content-Type:application/json'
);

$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;

The MY-RECIPIENT-TOKEN is received from the App and the MY AUTH KEY IS HERE is the key from the Firebase Console.

What am I missing here?

Answers: