向通知php对象添加字段

向通知php对象添加字段,php,firebase-cloud-messaging,Php,Firebase Cloud Messaging,我开始开发一个包含推送通知的应用程序。我希望我的通知对象保存一条消息和其他我称之为“颜色”的额外字符串 在我添加'data'=>$color行之前,一切都很顺利。现在我的消息字段为空(null),我只能读取颜色字符串 如何向PHP字段数组添加更多数据字段 function send_notification ($tokens, $message, $color) { $url = 'https://fcm.googleapis.com/fcm/send'; $fields = a

我开始开发一个包含推送通知的应用程序。我希望我的通知对象保存一条消息和其他我称之为“颜色”的额外字符串

在我添加'data'=>$color行之前,一切都很顺利。现在我的消息字段为空(null),我只能读取颜色字符串

如何向PHP字段数组添加更多数据字段

function send_notification ($tokens, $message, $color)
{
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array(
         'registration_ids' => $tokens,
         'data' => $message,
         'data' => $color
        );
    $headers = array(
        'Authorization:key = Not telling you me real 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_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;
}

您可以更改字段名,使其类似于
'color'=>$color
,也可以将其附加到
数据
字段中。它将是这样的:

$data = $message . "\n";
$data .= $color;

编辑:我相信您的
$message
变量可能是一个json,因此您应该将
$color
附加到该json。

您不能用同一个键添加两个值。在这种情况下,键是“数据”。如果您将其中一个重命名为其他名称,它应该可以工作。