来自PHP的FCM总是无效注册



我在从PHP发送推送通知时遇到问题,我总是收到错误"无效注册">,其中设备ID和服务器密钥正确。

但是,如果我测试从控制台发送消息 火力基地 成功,我可以在我的设备上看到 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"}]}

那么如何解决它,谢谢

试试这个。

<?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;

?>

相关内容

  • 没有找到相关文章