【问题标题】:FCM from PHP always InvalidRegistration来自 PHP 的 FCM 总是 InvalidRegistration
【发布时间】:2018-01-15 01:11:36
【问题描述】:

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

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

这是我的代码:

   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 android firebase firebase-cloud-messaging


    【解决方案1】:

    试试这个。

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

    【讨论】:

      猜你喜欢
      • 2020-04-22
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      • 2018-02-06
      • 1970-01-01
      • 2022-11-18
      相关资源
      最近更新 更多