I am making an Android location based app. For the location alert I want to send push notification in Android. But I am new to Urban airship. Can anyone tell me how can I implement it in PHP web API. It will be thankful to me
This is a good article that should get you started. It has both Android and iOS code as well.
Here is the code specifically for Android:
<?php
define('APPKEY','XXXXXXXXXXXXXXX'); // Your App Key
define('PUSHSECRET', 'XXXXXXXXXXXXXXX'); // Your Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/push/');
$contents = array();
$contents['alert'] = "PHP script test";
$notification = array();
$notification['android'] = $contents;
$platform = array();
array_push($platform, "android");
$push = array("audience"=>"all", "notification"=>$notification, "device_types"=>$platform);
$json = json_encode($push);
echo "Payload: " . $json . "\n"; //show the payload
$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);
echo "Response: " . $content . "\n";
// Check if any error occured
$response = curl_getinfo($session);
if($response['http_code'] != 202) {
echo "Got negative response from server: " . $response['http_code'] . "\n";
} else {
echo "Wow, it worked!\n";
}
curl_close($session);
?>
Related
hopefully you can help me.
I created a simple Messenger Chatbot yesterday and it works fine. Then I tried to program it more complex and the result was that I don't get any responses anymore.
I deleted and created a new Facebook App and I used a simple script again. But this script won't work either.
$hubVerifyToken = 'YES-THIS-IS-MY-BOT';
$accessToken = "MY-HOPEFULLY-RIGHT-ACCESS-TOKEN-I-RENEWED-IT-SEVERAL-TIMES";
if ($_REQUEST['hub_verify_token'] === $hubVerifyToken) {
echo $_REQUEST['hub_challenge'];
exit;
}
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$answer = "Error";
if($message == "hi") {
$answer = "Hello";
}
$jsonData = '{
"recipient":{
"id":"' . $sender .'"
},
"message":{
"text":"' . $answer . '"
}
}';
$ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token='.$accessToken);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$resp = curl_exec($ch);
I checked the $resp and I found out that it return 'false'?
But I have absolutely no idea why ...
Can you please help me?
Comment if you need more infos.
Thank you.
I have created development app in urbanairship and trying to send push notification from server using PHP code.
My code:
define('APPKEY','xxx'); // Your App Key
define('PUSHSECRET', 'ytyy'); // Your Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/push/');
$notification = array();
$notification['alert'] = "alert";
$platform = array();
array_push($platform, "android"); //comment out if you don't want Android
$richpush = array();
$richpush['title'] = "title";
$richpush['body'] = $message;
if(strlen($deviceToken)>50){
$deviceToken=str_replace(" ","",$deviceToken);
$push = array("audience"=>array("device_token"=>$deviceToken), "notification"=>$notification, "device_types"=>$platform, "message"=>$richpush);
}else{
$deviceToken=str_replace(" ","-",$deviceToken);
$push = array("audience"=>array("apid"=>$deviceToken), "notification"=>$notification, "device_types"=>$platform, "message"=>$richpush);
}
$json = json_encode($push);
$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);
echo "Response: " . $content . "\n";
But I'm getting :
Response: {"ok":false,"error":"The specified appkey [xxx] is not entitled to use the Message Center.","error_code":403}
Any help would be appreciated.
Message Center is not included in the Start or Basic accounts. Contact support and they will work with you to add Message Center entitlements.
I am trying to create the proper signature for the Ascentis API. There documentation is http://www.ascentis.com/API/Ascentis_API_Documentation.pdf. Page 4 describes the Signature format.
Here is my PHP code. Am I doing something wrong? I get a "not authorized error".
$url='https://selfservice2.ascentis.com/mycompany/api/v1.1/employees';
$timestamp=gmdate('Y-m-d\TH:i:s\Z');
$path=strtolower(str_replace('https://selfservice2.ascentis.com','',$url));
$signature_string="GET {$path} {$timestamp}";
$signature=hash_hmac("sha1",$signature_string,$secret_key);
$authorization=encodeUrl($client_key).':'.encodeUrl($signature);
Here's a more complete example. Props to #Steve Lloyd for getting me in the right direction.
$codes['secret_key'] = 'my_secret';
$client_key = 'my_key';
$url = 'https://selfservice.ascentis.com/my_company/api/v1.1/employees?lastname=%s';
$timestamp = gmdate('Y-m-d\TH:i:s\Z');
$path = strtolower(str_replace('https://selfservice.ascentis.com', '', $url));
$signature_string = "GET {$path} {$timestamp}";
$signature = base64_encode(hash_hmac("sha1", $signature_string, $codes['secret_key'], TRUE));
$authorization = urlencode($client_key) . ':' . urlencode($signature);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: " . $authorization,
"Accept: application/xml",
"Timestamp: " . $timestamp,
]);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data = curl_exec($ch);
$info = curl_getinfo($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "Received $httpcode from $url\n";
echo "Key: $client_key\n";
echo "Signature: $signature\n";
echo "authorization: $authorization\n";
echo "request info:" . var_export($info, TRUE) . "\n";
echo "data:\n";
var_export($data, TRUE)
After playing the trial and error game I was able to get this working. It turns out that you have to set hash_hmac to raw_output. Here is the working code:
$url='https://selfservice2.ascentis.com/mycompany/api/v1.1/employees';
$timestamp=gmdate('Y-m-d\TH:i:s\Z');
$path=strtolower(str_replace('https://selfservice2.ascentis.com','',$url));
$signature_string="GET {$path} {$timestamp}";
$signature=base64_encode(hash_hmac("sha1",$signature_string,$codes['secret_key'],true));
$authorization=encodeUrl($client_key).':'.encodeUrl($signature);
I am trying to send some custom data with the payload to iOS devices. I use the following code to successfully send pushes to all devices but I need to send some additional data. I couldn't find any documentation for urban airship and everything I tried just gives me errors.
Code:
$contents = array();
$contents['badge'] = "+1";
$contents['alert'] = $message;
$contents['sound'] = "cat.caf";
$notification = array();
$notification['ios'] = $contents;
$platform = array();
array_push($platform, "ios");
$push = array("audience"=>"all", "notification"=>$notification, "device_types"=>$platform);
$json = json_encode($push);
$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);
After some research I came to this. In the 'contents' section of your code add this line:
$contents['extra'] = array("message"=>"another message");
This should enable you to send additional custom data along with your push.
After reading documentation I have tried to write the following source:
$contents = array();
//$contents['badge'] = "+1";
$contents['alert'] = "Touchdown!";
//$contents['sound'] = "cat.caf";
$notification = array();
$notification= $contents;
$audience['tag'] = "49ers";
$scheduled['scheduled_time'] = "2013-04-01T18:45:30";
$push['push'] = array("audience"=> $audience, "notification"=>$notification, "device_types"=>"all");
$response = array();
array_push($response, array("name" => "My schedule", "schedule"=> $scheduled));
array_push($response, $push);
$json = json_encode($response);
//echo "Payload: " . $json . "\n"; //show the payload
$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);
// echo "Response: " . $content . "\n";
print_r($content);
// Check if any error occured
$response = curl_getinfo($session);
if($response['http_code'] != 202) {
$error = "Got negative response from server: " . $response['http_code'] . "\n";
} else {
$success = "Message has been sent successfully";
}
I am getting error = 0.
Can anyone help me please?
Thanks.
I have figured out the problem with the help of #dperconti (thank you very much).
I am posting a solution to 'how to schedule push for urbanairship' so others may find it helpful.
Code is:
define('APPKEY','xxxx'); // Your App Key
define('PUSHSECRET', 'xxxx'); // Your Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/schedules/');
$contents = array();
//$contents['badge'] = "+1";
$contents['alert'] = "Touchdown!";
//$contents['sound'] = "cat.caf";
$notification = array();
$notification= $contents;
$audience = "all";
$scheduled['scheduled_time'] = "2016-04-01T18:45:30";
// $push['push'] = array("audience"=> $audience, "notification"=>$notification, "device_types"=>"all");
$response = array();
array_push($response, array("name" => "My schedule", "schedule"=> $scheduled, "push" => array("audience"=> $audience, "notification"=>$notification, "device_types"=>"all")));
// array_push($response, $push);
$json = json_encode($response);
echo "Payload: " . $json . "<br><br><br>"; //show the payload
$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);
// echo "Response: " . $content . "\n";
print_r($content);
// Check if any error occured
$response = curl_getinfo($session);
if($response['http_code'] != 202) {
$error = "Got negative response from server: " . $response['http_code'] . "\n";
} else {
$success = "Message has been sent successfully";
}
curl_close($session);