来自PHP的FCM始终是InvalidRegistration

时间:2018-01-15 01:11:37

标签: php android firebase firebase-cloud-messaging

我有问题从PHP发送推送通知,我总是收到错误“InvalidRegistration”,其中设备ID和服务器密钥是正确的。

但是,如果我测试从控制台firebase发送消息是成功的,我可以在我的设备上看到notif。

这是我的代码:

   function sendNotification($dataArr)
    {

        define('API_ACCESS_KEY', 'AAAAL25_W68:APA91b............Bwp');
        $url = 'https://fcm.googleapis.com/fcm/send';

        $registrationIds = $dataArr['device_id'];

        $message = $dataArr['message'];
        $title = $dataArr['message'];

        // prepare the bundle
        $msg = array('message' => $message, 'title' => $title);
        $fields = array('registration_ids' => $registrationIds, 'data' => $msg);

        $headers = array(
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        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, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
        // Close connection
        curl_close($ch);

        return $result;
    }

并出现此错误:

"fcm":"{\"multicast_id\":7294907474895567505,\"success\":0,\"failure\":1,\"canonical_ids\":0,\"results\":[{\"error\":\"InvalidRegistration\"}]}

所以如何修复它,谢谢

1 个答案:

答案 0 :(得分:0)

试试这个。

<?php 

function send_notification ($tokens, $message = "", $n)
{
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array(
         /*'to'             => $tokens,*/
         'registration_ids' => $tokens,
         'priority'     => "high",
         'notification' => $n,
         'data'         => $message
    );

    //var_dump($fields);

    $headers = array(
        'Authorization:key = AAAAwLh71-s:APA91bHp4feyq76q7....RdhZrI',
        'Content-Type: application/json'
        );

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);  
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
   $result = curl_exec($ch);           
   if ($result === FALSE) {
       die('Curl failed: ' . curl_error($ch));
   }
   curl_close($ch);
   return $result;
}


$conn = mysqli_connect("localhost","root","","mydb");

$sql = " Select Token From t_user_detail";

$result = mysqli_query($conn,$sql);
$tokens = array();

if(mysqli_num_rows($result) > 0 ){

    while ($row = mysqli_fetch_assoc($result)) {
        $tokens[] = $row["Token"];
    }
}

//mysql_close($conn);

$msg = array
(
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'
);

$n = array(
    "body"  => "great match!",
    "title" => "NEW NOTIFICATION!",
    "text"  => "Click me to open an Activity!",
    "sound" => "warning"
);
$message_status = send_notification($tokens, $msg, $n);
echo $message_status;



?>