如何使用 OneSignal API 和 PHP 在推送通知中发送 "additional data"

How to send "additional data" in push notification with OneSignal API and PHP

从 OneSignals 控制面板发送推送通知时,我可以在推送消息中包含 "additional data"(键和值)。

我正在尝试创建一个 PHP 脚本,允许我通过 OneSignal API 从 PHP 发送推送,但在 OneSignal 的 API 中找不到任何关于附加数据的信息] 文档。

URL 至 OneSignal 文档:https://documentation.onesignal.com/reference

脚本按原样运行,但我需要包含其他数据。

有人能指出我正确的方向吗?

function sendPush($oneSignalConfig) {
if (sizeof($oneSignalConfig)) {  
  $notifTitle = html_entity_decode($oneSignalConfig['title'],ENT_QUOTES, 'UTF-8');
  $notifContent = html_entity_decode($oneSignalConfig['brief'],ENT_QUOTES, 'UTF-8');

  $includedSegments = array('All');      

  $fields = array(
    'app_id' => $oneSignalConfig['app_id'],
    'headings' => array("en" => $notifTitle),
    'included_segments' => $includedSegments,
    'isAnyWeb' => true,
    'url' => $oneSignalConfig['url'],
    'contents' => array("en" => $notifContent)
  );

  $thumbnailUrl = $oneSignalConfig['image_url'];

  if (!empty($thumbnailUrl)) {
      $fields['chrome_web_image'] = $thumbnailUrl;
  }

  $logoUrl = $oneSignalConfig['logo_url'];

  if (!empty($logoUrl)) {
      $fields['chrome_web_icon'] = $logoUrl;
  }

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
                         'Authorization: Basic ' . $oneSignalConfig['app_rest_api_key']));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  curl_setopt($ch, CURLOPT_POST, TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

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

  return $response;
}

return null;
} // EO_Fn 


if(isset($_POST['send'])) {

$oneSignalConfig = array(
'app_id' => 'xxxxx', //APP ID (ONESIGNAL)
'app_rest_api_key' => 'xxxxx', //REST API KEY
'title' => $_POST['tittel'], //TITLE
'brief' => $_POST['beskjed'], //BESKJED
'url' => $_POST['side'], //CONTENT URL
'image_url' => 'xxxx', //BILDE URL
'logo_url' => 'xxxx', // LOGO URL
);

$behandle = sendPush($oneSignalConfig);
if($behandle) {
    $array = json_decode($behandle, true);
    $melding = $array['recipients']." personer har mottatt push varslet.";
}

}

找到了。 :)

在 $oneSignalConfig 数组(代码底部)中,我添加了以下内容:

'key' => 'some value here',
'value' => 'something here',

在 $fields 数组的函数中,我添加了以下内容:

'data' => array($oneSignalConfig['key'] => $oneSignalConfig['value'])

这成功了。 Key 和 Value 现在通过推送通知发送。