Android FCM推送通知的关键在哪里?
我安装了Firebase,如果我通过Firebase Console发送手动推动通知,我的应用程序会收到它。
我正在尝试使用PHP脚本将通知发送到FCM。
问题是我不知道我应该使用什么密钥?
如果我在firebase控制台中使用一个,我会遇到此401错误:“无效键”
如果我在API&服务,我遇到了这个401错误“ project_not_permitting”
这是我正在使用的PHP脚本:
$server_key = ""
$token = ""
$title = ""
$body = ""
$curl = curl_init();
$authKey = "key=" . $server_key;
$registration_ids = $token;
curl_setopt_array($curl, array(
CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{
"registration_ids": ' . $registration_ids . ',
"notification": {
"title": ' . $title . ',
"body": ' . $body . '
}
}',
CURLOPT_HTTPHEADER => array(
"Authorization: " . $authKey,
"Content-Type: application/json",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo "no error: " . $response;
}
当然,它在Else语句中打印我的回声:
“ no Orror:401 ...
”可以帮我吗?
I installed firebase, and if I send manually push notification through firebase console, my app receives it.
I'm trying to send a notification to FCM using a php script.
The problem is I don't know what key I'm supposed to use ?
If I use the one in firebase console, I got this 401 error: "Invalid Key"
If I use the one in Api & Services, I got this 401 error "PROJECT_NOT_PERMITTED"
This is the php script I'm using:
$server_key = ""
$token = ""
$title = ""
$body = ""
$curl = curl_init();
$authKey = "key=" . $server_key;
$registration_ids = $token;
curl_setopt_array($curl, array(
CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{
"registration_ids": ' . $registration_ids . ',
"notification": {
"title": ' . $title . ',
"body": ' . $body . '
}
}',
CURLOPT_HTTPHEADER => array(
"Authorization: " . $authKey,
"Content-Type: application/json",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo "no error: " . $response;
}
Of course, it's printing my echo in else statement:
"no error: 401 ..."
Does someone can help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在Firebase Console项目设置中启用云消息传递API(旧)。
Try to enable Cloud Messaging API (Legacy) in Firebase Console project settings.