-
-
Save anonymous/c617fb627da3a47ffe91 to your computer and use it in GitHub Desktop.
Example for sending announcement push notification using SeattleCloud API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function curl_post($url, array $post = NULL, array $headers = NULL, array $options = array()) { | |
$hasHeader = 0; | |
if ($headers != NULL) | |
$hasHeader = 1; | |
$defaults = array( | |
CURLOPT_POST => 1, | |
CURLOPT_HEADER => $hasHeader, | |
CURLOPT_URL => $url, | |
CURLOPT_FRESH_CONNECT => 1, | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_FORBID_REUSE => 1, | |
CURLOPT_TIMEOUT => 4, | |
CURLOPT_POSTFIELDS => http_build_query($post) | |
); | |
$ch = curl_init(); | |
curl_setopt_array($ch, ($options + $defaults)); | |
if ($headers != NULL) { | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
} | |
if (!$result = curl_exec($ch)) { | |
trigger_error(curl_error($ch)); | |
} | |
curl_close($ch); | |
return $result; | |
} | |
function curl_get($url, array $get = NULL, array $options = array()) { | |
$defaults = array( | |
CURLOPT_URL => $url . (strpos($url, '?') === FALSE ? '?' : '') . http_build_query($get), | |
CURLOPT_HEADER => 0, | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_TIMEOUT => 4 | |
); | |
$ch = curl_init(); | |
curl_setopt_array($ch, ($options + $defaults)); | |
if (!$result = curl_exec($ch)) { | |
trigger_error(curl_error($ch)); | |
} | |
curl_close($ch); | |
return $result; | |
} | |
$username = ""; | |
$password = ""; | |
$publisherId = ""; | |
$appId = ""; | |
$message = "Message"; | |
// Get SC API authorization token by submitting your SC account username and password. | |
// You should save it somewhere and reuse for subsequent requests. | |
// Re-request a new token if you get a 401 response code and "invalidAuthToken" or "authTokenExpired" error reasons. | |
$base_url = "http://seattleclouds.com/api/v1/"; | |
$url = $base_url . "auth/tokens"; | |
$post_params = array( | |
'username' => $username, | |
'password' => $password, | |
'publisherId' => $publisherId | |
); | |
$response = curl_post($url, $post_params); | |
$json = json_decode($response, true); | |
// Provide authentication token header | |
$auth_header = 'Authorization: SCAuth authToken=' . $json['authToken']; | |
$headers = array( | |
$auth_header | |
); | |
// Send Push Notification to the app uniquely identified by its publisher ID, username and app ID. | |
// Note that you must specify your type "announcement" and "data.message" is your custom message. | |
$url = $base_url . "users/" . $username . "/apps/" . $appId . "/cloudMessages?publisherId=" . $publisherId; | |
$post_params = array( | |
"type" => "announcement", | |
"data.message" => $message | |
); | |
$push_response = curl_post($url, $post_params, $headers); | |
print $push_response; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment