我遇到了Firebase" registration_ids"的问题。当我从Rest Client发送请求时,我得到了成功的响应。
{"multicast_id":4650719213012935695,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1468837777484579%214918aff9fd7ecd"},{"message_id":"0:1468837777484484%214918aff9fd7ecd"}]}
但是当我从我的Android应用程序调用相同的php脚本时,它会在响应中给出错误。 (我在Charles Proxy中得到了答复)
"registration_ids" field is not a JSON array
这是我的php脚本
function fetchFirebaseTokenUsers($message) {
$query = "SELECT token FROM firebase_tokens";
$fcmRegIds = array();
if($query_run = mysqli_query($this->con, $query)) {
while($query_row = mysqli_fetch_assoc($query_run)) {
array_push($fcmRegIds, $query_row['token']);
}
}
if(isset($fcmRegIds)) {
$pushStatus = $this->sendPushNotification($fcmRegIds, $message);
}
}
function sendPushNotification($registration_ids, $message) {
ignore_user_abort();
ob_start();
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $registration_ids,
'data' => $message,
);
define('GOOGLE_API_KEY', 'AIzaSyC.......VdYCoD8A');
$headers = array(
'Authorization:key='.GOOGLE_API_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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if($result === false)
die('Curl failed ' . curl_error());
curl_close($ch);
return $result;
ob_flush();
}
答案 0 :(得分:5)
我使用to
而不是将registration_ids
的Firebase令牌数组发送出去。导致registration_ids
仅限于1000个火焰基地令牌数量https://firebase.google.com/docs/cloud-messaging/http-server-ref。
function fetchFirebaseTokenUsers($message) {
$query = "SELECT token FROM firebase_tokens";
$fcmRegIds = array();
if($query_run = mysqli_query($this->con, $query)) {
while($query_row = mysqli_fetch_assoc($query_run)) {
array_push($fcmRegIds, $query_row['token']);
}
}
if(isset($fcmRegIds)) {
foreach ($fcmRegIds as $key => $token) {
$pushStatus = $this->sendPushNotification($token, $message);
}
}
}
function sendPushNotification($registration_ids, $message) {
ignore_user_abort();
ob_start();
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'to' => $registration_ids,
'data' => $message,
);
define('GOOGLE_API_KEY', 'AIzaSyC.......VdYCoD8A');
$headers = array(
'Authorization:key='.GOOGLE_API_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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if($result === false)
die('Curl failed ' . curl_error());
curl_close($ch);
return $result;
ob_flush();
}
答案 1 :(得分:1)
如果您使用registration_ids
,则需要按如下方式传递数组中的数据:
<强> PHP 强>:
$fields = array(
'registration_ids' => array($registration_ids),
'data' => $message,
);
答案 2 :(得分:1)
如果registration_ids只是一个令牌,如下所述 https://firebase.google.com/docs/cloud-messaging/http-server-ref 你应该使用字段&#39;来&#39;。
FCM要求registered_ids是一个索引从0开始的JSON数组。 我有相同的错误,因为索引不在这些。我使用PHP函数array_values解决了它。
function sendPushNotification($registration_ids, $message) {
ignore_user_abort();
ob_start();
$url = 'https://fcm.googleapis.com/fcm/send';
//FCM requires registration_ids array to have correct indexes, starting from 0
$registration_ids = array_values($registration_ids);
$numTokens = count($registration_ids);
if($numTokens == 1){
$fields = array(
'to' => $registration_ids[0],
'data' => $message,
);
}elseif($numTokens > 1){
$fields = array(
'registration_ids' => $registration_ids,
'data' => $message,
);
}
define('GOOGLE_API_KEY', 'AIzaSyC.......VdYCoD8A');
$headers = array(
'Authorization:key='.GOOGLE_API_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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if($result === false)
die('Curl failed ' . curl_error());
curl_close($ch);
return $result;
ob_flush();
}
答案 3 :(得分:0)
我有类似的情况,我必须为用户的注册令牌创建一个GCM组&amp;发生了错误。我后来发现我没有遵循f ire-base
中提到的正确的JSON格式<强>解决方案:强>
创建模型
public class tempo {
public string operation {get;set; }
public string notification_key_name { get; set; }
public List<string> registration_ids { get; set; }
}
然后序列化
var hmm = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
并打电话给谷歌。