php - 如何获取要在 php curl 请求的授权 header 中解析的 FCM 服务器 key ?

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

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

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

最佳答案

点击 Overview 导航选项右侧的 Setting 图标,然后从弹出窗口中选择 Project Setting,然后选择 cloud Messaging 选项卡,您将看到 Server KeyLegacy Server Key

关于php - 如何获取要在 php curl 请求的授权 header 中解析的 FCM 服务器 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47053115/

相关文章:

javascript - 为什么我的 jquery new Date() 函数会覆盖传入的参数?

php - CakePHP 在创建新用户失败时填写密码字段

android - 为什么当我输入确切名称时,我的应用程序在 Play 商店中的排名如此之低?

Android 支持库 RecyclerView 禁用动画

php - 比较另一个表中是否存在记录

php - PHP中 "or"的奇怪做法

android - 为什么 Activity 正在重启?如何避免?

node.js - 如何从 Cloud Firestore 数据库获取 node.js 中的特定字段值?

ios - 我们可以在 iOS 中为一个 App 设置多个 Apple 推送通知设置作为日历 App

iphone - 推送通知未出现在通知托盘中