如何使用php变量使用onesignal发送通知?

时间:2018-12-05 21:18:38

标签: javascript php onesignal

function sendMessage(){
  $content = array(
      "en" => 'Testing Message'
      );

  $fields = array(
      'app_id' => "XXXXXXXXX",
      'include_player_ids' => $usertomemberonesignalid,
      'data' => array("foo" => "bar"),
      'large_icon' =>"ic_launcher_round.png",
      'contents' => $content
  );

  $fields = json_encode($fields);

问题在于,当在include_player_ids内键入玩家ID值时,该代码当前正在运行,但是我想使用PHP变量动态地执行此操作。

1 个答案:

答案 0 :(得分:0)

这是我现有项目之一的示例代码。

// pass your single or multiple device ids here to send notification

// for single
$device_ids = array($PLAYERIDFROMDB);

// for multiple from array
/*$device_ids = array(
'2c5d007-5985-4ad9-a1d7-f4ec7eb84f00', // device id 1 (register_id from onceSignal)
'2c5d007-5985-4ad9-a1d7-f4ec7eb84f00'  // device id 1 (register_id from onceSignal)
); */
$content = array(
    "en" => 'Notification Title'
);
$data = array(
        "image"          => 'your_image_url', 
        "title"          => 'your Title',
        "message"        => 'your Message'
    );
$fields = array(
    'app_id' => 'ONESIGNAL_API_KEY', // your api key
    'include_player_ids' => $device_ids,
    'data' => $data,
    'contents' => $content
);

$sendContent = json_encode($fields);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, 
                    array('Content-Type: application/json; 
                            charset=utf-8',
                            'Authorization: Basic ONESIGNAL_REST_API_KEY' // your 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, $sendContent);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

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