首页 > 解决方案 > FCM Push for iOS 错误 InvalidRegistration

问题描述

我正在尝试通过 PHP 脚本通过Firebase Cloud MessagingiOS 应用程序发送推送通知。

目前我正在使用以下测试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 脚本,但最终得到相同的结果,这会是什么问题?

任何帮助将不胜感激。

标签: phpfirebasepush-notificationfirebase-cloud-messagingapple-push-notifications

解决方案


$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);
}

推荐阅读