"error":"InvalidRegistration" 从 PHP 发送通知

"error":"InvalidRegistration" while sending notification from PHP

使用 registeration_ids 和参数尝试上述代码,但两者均无效。帮我解决同样的问题。 每次它给出多播 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, true);
    curl_setopt($ch, 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;
}

无效注册通常有以下原因之一:

  • 注册token/device令牌无效
  • 客户端应用已主动注销 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