php - FCM Push for iOS 错误 InvalidRegistration
问题描述
我正在尝试通过 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 脚本,但最终得到相同的结果,这会是什么问题?
任何帮助将不胜感激。
解决方案
$apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$title = 'Title';
$message = 'hello message';
$notification =[
"text" => "Test",
'badge' => "1",
'sound' => 'shutter.mp3',
"android_channel_id"=>"test01",
'body'=>$message,
'icon'=>'notif_icn',
'title'=>$title,
'priority'=>'high',
'vibrate'=> 1,
'alert'=> $message
];
$msg = [
'message'=> $message,
'title'=> $title
];
$fields =[
"content_available" => true,
"priority" => "high",
'registration_ids'=> $deviceToken,
'data'=> $msg
];
$headers = array('Authorization: key=' . $apiKey,'Content-Type: application/json');
if ($headers){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$response = curl_exec($ch);
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
}
推荐阅读
- r - R中时间序列缺失数据的插补模型
- python - 从单词搜索游戏中找到解决方案
- java - AWS Api Gateway 缓存的响应
- javascript - 菜单中的链接不可点击
- c# - CreateInstance InvalidCastException
- angularjs - 具有名称属性的AngularJS单选按钮组未正确初始化
- php - 查询状态以获取未来时间的记录,查询也获取过去的记录
- gridview - 即使在应用程序关闭后保存和恢复 GridView 的 textview 数据及其属性
- linux - 如何在 Unix 脚本中自动化并结束更多命令?
- esper - 如何获得“仅限重大事件”?