php - 注册 ID 无效 FCM

标签 php android firebase-cloud-messaging

当我执行下面的代码时,我得到:

Invalid registration token

我从 MYSQL 数据库中获取 token 。我检查了返回的数据是否与数据库匹配。一切看起来都很完美,但我找不到我的错误。

如何修复或调试此代码:

function send_android_notification($registration_ids, $message) {
    //print_r($registration_ids);
    //print_r($message);
    //exit;
    define("GOOGLE_API_KEY", "AAAABoqI4ac:APA91bEMNO81wwRARcQftyBhIBU1U4Bq6rLKeRZDLOPAQu-9fk8y_6bOsZWnw2JEq-uwDJXDij1SjGPJtnwG6QO_IRZ54Gbbjfp9-izJ_a7DnLoTHD9Ot6lod7C-wLaYkH2Xl6l8iR8z");
    $fields = array(
        'registration_ids' => array($registration_ids),
        'data' => $message,
    );
    $headers = array(
        'Authorization:key=' . GOOGLE_API_KEY, // FIREBASE_API_KEY_FOR_ANDROID_NOTIFICATION
        'Content-Type: application/json'
    );
    // Open connection
    $ch = curl_init();
    // Set the url, number of POST vars, POST data
    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_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    //echo json_encode($fields);
    //exit;
    // Execute post
    $result = curl_exec($ch);
    if ($result === false) {
        die('Curl failed:' . curl_errno($ch));
    }
    // Close connection
    curl_close($ch);
    return $result; 
}

最佳答案

解决了!

检查您的移动注册 token 存储在数据库表中的字段的长度。设置在200以上,因为token数的长度超过了200。API server key字段必须是firebase cloud messaging提供的legacy server key。

下面是我的工作代码!

function send_android_notification($registration_ids, $message) {
    //print_r($registration_ids);
    //print_r($message);
    //exit;
    define("GOOGLE_API_KEY", "AIzaSyDfwSXWRD5tf*******************"); //legacy server key
    $fields = array(
        'registration_ids' => $registration_ids,
        'notification' => $message, //note: body & title fileds must be specified for the message or your only get just the vibration but the notification
    );
    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY, //  FIREBASE_API_KEY_FOR_ANDROID_NOTIFICATION
        'Content-Type: application/json'
    );
    //Open connection
    $ch = curl_init();
    //Set the url, number of POST vars, POST data
    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_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    //echo json_encode($fields);
    //print_r($headers);
    //exit;
    //Execute post
    $result = curl_exec($ch);
    if ($result === false) {
        die('Curl failed:' . curl_errno($ch));
    }
    // Close connection
    curl_close($ch);
    return $result;
}

关于php - 注册 ID 无效 FCM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47618633/

相关文章:

android - 如何在关闭/停止android中的 Activity 时创建Bottom-Top动画

android - 在一个函数上发送多个 firebase 云消息

php - file_get_contents 默认使用哪个 HTTP 版本?

php - 如何在MPDF中设置只有底部边框的文本框

php - Foreach 视频创建模态

node.js - firebase 函数通知

android - Unresolved 类 'MyFirebaseMessagingService'

php - Laravel:第 342 行的非法字符串偏移量 'file',文件 Illuminate/Mail/Mailable.php

android - 使用动画旋转图像

android - 多个主题更改选项卡图标颜色