如何在php curl请求中获取要在授权头中解析的FCM服务器密钥?

时间:2017-11-01 10:47:27

标签: php android push-notification http-status-code-401 firebase-notifications

我尝试使用web api密钥来自> firebase控制台>我的项目>项目设置>常规选项卡。 注意:目前我没有使用付费开发者帐户。我只是创建一个新的谷歌帐户并创建了一个项目。

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

1 个答案:

答案 0 :(得分:0)

点击Setting导航选项右侧的Overview图标,然后从弹出式窗口中选择Project Setting并选择cloud Messaging标签,您会看到Server Key和{{ 1}}。