Skip to content

Instantly share code, notes, and snippets.

@bactisme
Created May 19, 2016 14:41
Show Gist options
  • Save bactisme/8ed1f8d4fc1587d09a3edf1a0708ad15 to your computer and use it in GitHub Desktop.
Save bactisme/8ed1f8d4fc1587d09a3edf1a0708ad15 to your computer and use it in GitHub Desktop.
Send a web notification with OneSignal
function sendMessage($title, $url){
$content = array(
"en" => $title,
"fr" => $title
);
$fields = array(
'app_id' => ONESIGNAL_APP_ID,
'included_segments' => array('All'),
'url' => $url,
//'data' => array("foo" => "bar"),
'contents' => $content,
'headings' => array(
"en" => ONESIGNAL_SITENAME,
"fr" => ONESIGNAL_SITENAME
),
'ttl' => 3600
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($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',
'Authorization: Basic '.ONESIGNAL_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, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment