问题描述
使用 registeration_ids 和 to 参数尝试上述代码,但两者都不起作用。帮我解决同样的问题。
每次它都会给出多播ID和InvalidRegistration的错误。
收到此错误:{"multicast_id":8367359766XXXXXXXXX,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
function send_notification () {
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$args = func_get_args();
$response = ["status" => 0,"message" => "Notification Couldn't be sent"];
$title = "hey test notification";
$body = "";
$apiKey = "QWERTYHHVCFVBVBN";//Server key under the Project settings
$tokenArr = ["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"];
$refId = 123;
$msgNotification = [
"title" => $title,"body" => $body
];
$extraNotificationData = [
"refId" => $refId,"title" => $title
];
$fcmNotification = [
"registration_ids" => $tokenArr,"notification" => $msgNotification,"data" => $extraNotificationData
];
$headers = [
"Authorization: key=" . $apiKey,"Content-Type: application/json"
];
$encodedData = json_encode($fcmNotification);
// echo "<pre>";print_r($fcmNotification);exit;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$fcmUrl);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_POSTFIELDS,$encodedData);
$result = curl_exec($ch);
if ($result === FALSE) {
die("Curl Failed: " . curl_error($ch));
}
print_r($result);exit;
curl_close($ch);
$response = ["status" => 1,"message" => "Notification sent to users","payload" => $result];
return $response;
}
解决方法
无效注册通常有以下原因之一:
- 注册令牌/设备令牌无效
- 客户端应用已主动注销 FCM
- 客户端应用已自动取消注册到 FCM,因为该应用已被卸载。
- 设备令牌已过期 - 当 Google 刷新设备令牌时,或者在 iOS 设备的情况下,当 APNs 令牌已过期时,可能会发生这种情况
- API 密钥与您要将消息发送到的设备属于不同的 Firebase 项目。造成这种情况的一个常见原因是,当您使用多个环境时,例如,令牌已注册到 prod 环境中,但您尝试从暂存环境发送消息。
在所有情况下,除了最后一种情况,您都可以从服务器中删除注册令牌并停止向其发送通知。
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
表明您正在使用旧版 FCM API 发送消息。请注意,除非您依赖设备组通知,否则 Google 建议使用 migrating to the newer HTTP v1 API。