【发布时间】:2019-08-27 13:11:56
【问题描述】:
我正在尝试通过 PHP 脚本通过 Firebase Cloud Messaging 向 iOS 应用发送推送通知。
目前我正在使用以下测试PHP 脚本。
$url = "https://fcm.googleapis.com/fcm/send";
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$serverKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$title = "Title";
$body = "Body of the message";
$notification = array('title' =>$title , 'text' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
但是,当我尝试通过浏览器运行脚本时收到以下错误消息。
{"multicast_id":5417898622260949101,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
但是当我从 FCM 控制台使用相同的设备令牌时,它可以正常工作。
我在互联网上尝试了许多 PHP 脚本,但最终得到相同的结果,这会是什么问题?
任何帮助将不胜感激。
【问题讨论】:
-
根据文档 (firebase.google.com/docs/cloud-messaging/…),“InvalidRegistration”指的是格式错误的值。您能否确保注册令牌的值中没有其他字符或空格?
-
还请注意,您当前使用的是旧版 HTTP 协议 - 如果可以,请考虑使用 HTTP v1 协议 (firebase.google.com/docs/reference/fcm/rest/v1/…) 来实现它,这将确保您的脚本保持未来的证明.
-
@jeromegamez 我正在使用字符串传递令牌,肯定没有任何其他字符。但是我使用 trim 来确保在创建数组的最后一步没有附加额外的空格。关于第二点,我会尝试并告诉你。
标签: php firebase push-notification firebase-cloud-messaging apple-push-notifications