简体   繁体   中英

Firebase notification PHP script

I'm testing firebase notification since my old project was integrated to GCM, but now I'm also using firebase database.

I'm trying to execute a php script in order to send notification to one of my device. I'm able to obtain the TokenID and also I'm able to send notifications to this token from the Firebase Console, but when I use the PHP script, I have the following error:

Invalid (legacy) Server-key delivered or Sender is not authorized to perform request.

Here is my script:

$message = $_POST['message'];
$title = $_POST['title'];
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
$server_key = "AAAXXXXXX";
$key =  "c43xxxxxxxxx";


$headers = array (
    'Authorization: key =' . server_key,
    'Content-Type:application/json'
);

$fields = array('to'=>$key,
            'notification'=>array('title'=>$title,'body'=>$message));

$payload = json_encode($fields);

$curl_session = curl_init();
curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, "POST"); 

$result = curl_exec($curl_session);
curl_close($curl_session);

echo $result;

I'm getting the api key from:

Firebase console > gear icon > proyect configuration > Cloud messaging
$headers = array (
'Authorization: key =' . server_key,
'Content-Type:application/json'

);

you should make the server_key a variable like $server_key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM