我有一个 PHP 页面,我用它来向我开发的移动应用程序的用户发送通知,该页面直到上个月都工作正常,然后它给了我这个错误
{"multicast_id":5174063503598899354,"成功":0,"失败":1,"canonical_ids":0,"结果":[{"错误":"无效注册"}]}
我尝试使用此链接中的文档生成 OAUTH 令牌https://firebase.google.com/docs/cloud-messaging/auth-server#node.js但它需要 NODE.JS 服务器,而我的服务器不支持 Node.Js ,我尝试使用 Firebase Admin SDK 但找不到任何内容。
这是页面的PHP代码
<?php
//Includes the file that contains your project's unique server key from the Firebase Console.
require_once("serverKeyInfo.php");
//Sets the serverKey variable to the googleServerKey variable in the serverKeyInfo.php script.
$serverKey = $googleServerKey;
//URL that we will send our message to for it to be processed by Firebase.
$url = "https://fcm.googleapis.com/fcm/send";
//Recipient of the message. This can be a device token (to send to an individual device)
//or a topic (to be sent to all devices subscribed to the specified topic).
$recipient = $_POST['rec'];
//Structure of our notification that will be displayed on the user's screen if the app is in the background.
$notification =array(
'title' => $_POST['title'],
'body' => $_POST['body'],
'sound' => 'default'
);
//Structure of the data that will be sent with the message but not visible to the user.
//We can however use Unity to access this data.
$dataPayload =array(
"powerLevel" => "9001",
"dataString" => "This is some string data"
);
//Full structure of message inculding target device(s), notification, and data.
$fields =array(
'to' => $recipient,
'notification' => $notification,
'data' => $dataPayload
);
//Set the appropriate headers
$headers = array(
'Authorization: key=' . $serverKey,
'Content-Type: application/json'
);
//Send the message using cURL.
$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_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
//Result is printed to screen.
echo $result;
?>
任何人都可以给我发一个例子吗我该怎么做(我是 PHP 初学者)
提前致谢
*更新:我还尝试将代码中的 $url 更改为
$url = "https://fcm.googleapis.com/v1/projects/notifications-9ccdd/messages:send";
但它给了我这个错误
“错误”: {
“代码”:401,
"message": "请求的身份验证凭据无效。需要 OAuth 2 访问令牌、登录 cookie 或其他有效的
身份验证凭证。看https://developers.google.com/identity/sign-in/web/devconsole-project。”,
“状态”:“未经身份验证”
}
块引用