首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向notification对象添加字段

向notification对象添加字段
EN

Stack Overflow用户
提问于 2017-03-18 17:39:15
回答 1查看 515关注 0票数 0

我开始开发一个有推送通知功能的应用程序。我希望我的通知对象保存一条消息和其他我称之为"color“的额外字符串。

在我添加行数据color=‘>$color=’>颜色之前,一切都进行得很好.现在,我的消息字段为空(null),我只能读取颜色字符串。

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

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-18 17:41:29

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

代码语言:javascript
复制
$data = $message . "\n";
$data .= $color;

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42877687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档