Testing push notifications using PHP and HTTP POST

In order for our server application to programmatically push notifications to a user or group of users, we will need to create a script that can push the notifications to the Urban Airship servers. This can be done in a variety of methods (via desktop app, .NET application, web application and so on), but for the purposes of this recipe we will use PHP, which is simple, fast, and freely available.

Note

The complete source code for this recipe can be found in the /Chapter 9/Recipe 7 folder.

How to do it…

First, we need to create the PHP script which will communicate with the Urban Airship servers to send a push notification. Create the following PHP script, save it as airship.php, and upload it to a server capable of running PHP and with CURL installed. There are plenty of free PHP/Apache hosting accounts available online if you don't already have one capable of doing this.

The following sample is taken from the Urban Airship website:

<?php
 define('APPKEY','xxxx'), 
 define('PUSHSECRET', 'xxx'), // Master Secret
 define('PUSHURL', 
 'https://go.urbanairship.com/api/push/broadcast/'), 

 $contents = array(); 
 $contents['badge'] = "+1"; 
 $contents['alert'] = "Hello there Titanium Developer!"; 
 $push = array("aps" => $contents); 

 $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')); 
 $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'] != 200) { 
     echo "Got negative response from server, http code: ". 
     $response['http_code'] . "
"; 
 } else { 

     echo "Wow, it worked!
"; 
 } 

 curl_close($session);
?>

All that is left to do now is run the PHP script in a browser, and when you do, you should see a success message echoed out to the browser page, and you should also be able to see a new push notification delivered to your device that was set up in the previous recipe, as seen in the following screenshot:.

How to do it…

How it works…

The PHP script in this recipe is doing much the same job as the actual Urban Airship website does when you can perform tests via their console. Here, we are using PHP to build a CURL request in JSON and post it to the Urban Airship server. That request is in turn received and then pushed out to your device or devices as a Push Notification by the Urban Airship system.

In a production environment, you would want to extend your PHP script to either receive the badge and message variables as POST variables, or perhaps hook up the script directly to a database with whatever business logic your app requires. You should also note that Urban Airship provides samples for languages other than PHP. So if your system is built in .NET or another platform, the same principles of sending out broadcasts still apply.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.182.62