【问题标题】:Field "to" must be a JSON string [Firebase]字段“to”必须是 JSON 字符串 [Firebase]
【发布时间】:2016-06-01 06:14:02
【问题描述】:

我正在尝试使用 cron 作业发送通知。我从 GCM 迁移到 FCM。在我的服务器端,我更改了https://android.googleapis.com/gcm/send to https://fcm.googleapis.com/fcm/send,并更新了如何请求将registration_ids 更改为to 的数据。检查通过的 json 是一个有效的 json,但我遇到错误 Field "to" must be a JSON string。有没有办法解决这个问题?

这是我的代码

function sendNotificationFCM($apiKey, $registrationIDs, $messageText,$id) {


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

    $message = array(
            'to' => $registrationIDs,
            'data' => array(
                    "message" => $messageText,
                    "id" => $id,
            ),
    );


    $ch = curl_init();

    curl_setopt_array($ch, array(
            CURLOPT_URL => 'https://fcm.googleapis.com/fcm/send',
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => json_encode($message)
    ));

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

【问题讨论】:

    标签: php android json firebase firebase-cloud-messaging


    【解决方案1】:

    尝试将$registrationIDs 显式转换为string

    $message = array(
            'to' => (string)$registrationIDs,
            'data' => array(
                    "message" => $messageText,
                    "id" => $id,
            ),
    );
    

    已编辑答案

    'to' 参数需要string - 这是消息的接收者。

    $registrationIDs 可以通过单独的参数(作为字符串数组)传递给'registration_ids'

    将您的代码编辑成如下内容:

    $recipient = "YOUR_MESSAGE_RECIPIENT";
    
    $message = array(
            'to' => $recipient,
            'registration_ids' => $registrationIDs,
            'data' => array(
                    "message" => $messageText,
                    "id" => $id,
            ),
    );
    

    $recipient 在哪里

    注册令牌、通知键或主题。

    请参考: Firebase Cloud Messaging HTTP Protocol

    【讨论】:

    • 会将(string) 转换为像“bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvD, 1234asd:CI2k_HHwgIpoDKCIZvvD, bk3RNwTe3H0:asds_HwgIpoDKCIZvvD”这样的$registrationIDs?>
    • 不,不会。我猜你的 $registrationIDs 是一个字符串数组。 'to' 键需要一个字符串,我假设 $registrationIDs 必须是 int,它不是,因此我建议将它转换为字符串。
    • 我的$registrationIDs 有注册令牌
    • @mori 然后'to' 是可选的,请参阅答案中的链接
    • 我应该只使用registration_ids 吗?但是当我使用它时,我的设备没有收到任何通知。
    【解决方案2】:

    尝试将 to 设为字符串:

    Content-Type:application/json
    Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
    
    {
      "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
      "data" : {
       ...
     },
    }
    

    【讨论】:

      【解决方案3】:
      // if you are using an array like
      $fcm_ids = array();
      $fcm_ids[] = "id_1";
      $fcm_ids[] = "id_2";
      $fcm_ids[] = "id_3";
      //.
      //.
      //.
      $fcm_ids[] = "id_n";
      // note: limit is 1000 to send notifications to multiple ids at once.
      // in your messsage send notification function use ids like this
      $json_data = [
      "to" => implode($fcm_ids),
      "notification" => [
          "title" => $title,
          "body" => $body,
          ],
      "data" => [
          "str_1" => "something",
          "str_2" => "something",
          .
          .
          .
          "str_n" => "something"
          ]
      ];
      

      【讨论】:

        【解决方案4】:

        我遇到了类似的问题。事实证明,当我从我的 Firebase 数据库中检索注册令牌(使用此 Firebase PHP Client)时,它在令牌的开头和结尾都带有双引号。所以我必须先从令牌中删除第一个和最后一个字符,然后才能使用它。下面的代码解决了我的问题

        substr($registrationToken, 1, -1)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-03
          • 2022-01-16
          • 2022-07-15
          • 2015-03-06
          • 2020-06-24
          • 2021-06-08
          • 1970-01-01
          • 2021-12-04
          相关资源
          最近更新 更多