Aleem
Aleem

Reputation: 3291

Send message to Urban airship using PHP

I am iphone developer and i am working on application, in which i need to send notification to devices in which my application is installed. I done this with the help of Urban airship, now my requirement is to send message from CMS (PHP code) to urban airship(in which my application is registered) and that message will automatically send to my device as notification send earlier from urban airship. Some one guide me that how can i achieve this, or advise me any healthy way to achieve this target

Upvotes: 0

Views: 3190

Answers (2)

jtbandes
jtbandes

Reputation: 118691

Urban Airship's API documentation can be found here. They also have an article describing a simple use of the API, and an open-source PHP library.

Upvotes: 4

Waqar Alamgir
Waqar Alamgir

Reputation: 9968

<?php
 define('APPKEY','XXXXXXXXXXXXXXX'); // Your App Key
 define('PUSHSECRET', 'XXXXXXXXXXXXXXX'); // Your Master Secret
 define('PUSHURL', 'https://go.urbanairship.com/api/push/');

 $contents = array();
 $contents['badge'] = "+1";
 $contents['alert'] = "PHP script test";
 $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);
 echo $content; // just for testing what was sent

 // Check if any error occured
 $response = curl_getinfo($session);
 if($response['http_code'] != 202) {
     echo "Got negative response from server, http code: ".
     $response['http_code'] . "\n";
 } else {

     echo "Wow, it worked!\n";
 }

 curl_close($session);
?>

Upvotes: 2

Related Questions