Search code examples
phpandroidpush-notificationhttp-status-code-401firebase-notifications

How to get FCM server key which is to be parsed in authorization header in php curl request?


I tried with web api key which i got from > firebase console> My Project> project setting>General Tab. NOTE : Currently I am not using paid developer account. I just create a new google account and created a project.

$url = 'https://fcm.googleapis.com/fcm/send';
$server_key = '??????WHAT TO PUT HERE ????';
$fields = [];
$data = [];
$data['type'] = $type;
$data['title'] = $title;
$data['message'] = $message;
$data['sound'] = 'default';
$fields['data'] = $data;
if (is_array($id)) {
    $fields['registration_ids'] = $id; // array of registration ids
} else {
    $fields['to'] = $id; single registration id
}
//$fields['content_available'] = "true";
//header with content_type api key
$headers = array(
    'Content-Type:application/json',
    'Authorization:key=' . $server_key
);
$notification = array();
$notification['title'] = $title;
$notification['body'] = $message;
$notification['sound'] = 'default';
$notification['click_action'] = $type;
$fields['notification'] = $notification;

$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
curl_close($ch);
return $result;

Solution

  • Click on Setting Icon on Right of Overview navigation option and select Project Setting from popup and select cloud Messaging tab you will see the Server Key and Legacy Server Key.