Sample Code For Sending an Android Push Notification from PHP via Urban Airship


Update:
Sorry there is some confusion about what variables need to be changed in order for this script to work.
The only values that need to be changed are:

  • $dictionary[‘apids’] = array(…)
  • $username
  • $password

Obtaining an apid is explained on step 9 of this page. The $username is the application key from the details page for the urban airship apps page. The password is the Application Master Secret from the same page. See below for details.

Screenshot showing where to find $username and $password values

Where to find the $username and $password values

<?
if(isset($_POST['name'])){

        // grab the contents from the form
        $alert = $_POST['alert'];
        $message = $_POST['message'];
        $name = $_POST['name'];
        $email = $_POST['email'];


        // we are going to build up a dictionary that will be converted
        // to a json object that urban airship understands

        // create the extra field
        $extra = array();
        $extra['name'] = $name;
        $extra['message'] = $message;
        $extra['email'] = $email;

        // create the contents of the android field
        $android = array();
        $android['alert'] = $alert;
        $android['extra'] = $extra;

        // create the contents of the main json object
        $dictionary = array();
        $dictionary['android'] = $android;
        $dictionary['apids'] = array("1aur99c-5406-4e69-abd6-7mod91us1c"); // The specific android urban airship phone id

        // convert the dictionary to a json string
        $data = json_encode($android);

        // open connection
        $ch = curl_init();

        // the url and credentials for posting to urban airship
        $url = "https://go.urbanairship.com/api/push/";
        $username = "UFRMLySlgEv67xcEcWuqUY";
        $password = "wnfIUBWlPEE7uGNFjk0z";

        // set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json",));
        curl_setopt($ch, CURLOPT_USERPWD,"$username:$password");
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // execute post
        $result = curl_exec($ch);

        // close connection
        $res = curl_close($ch);
        if($res == 1){
                print "Success";
        } else {
                print "Error";
        }

} else {
?>
        <h2>Send Jim a push notification
                <form method="post" action="thisfile.php" style="margin: 5px; padding: 5px;">
                        <table width="100%" border="0" cellspacing="5px" cellpadding="3px">
                        <tr>
                                <td>Alert</td>
                                <td><input name="alert" type="text" id="alert" size="50" style="width: 400px;" /></td>
                        </tr>
                        <tr>
                                <td>Message</td>
                                <td><textarea name="message" rows="4" cols="90" id="detail" style="width: 400px; height: 120px;"></textarea></td>
                        </tr>
                        <tr>
                                <td>Your Name</td>
                                <td><input name="name" type="text" id="name" size="50" style="width: 400px;" /></td>
                        </tr>
                        <tr>
                                <td>Email
                                <td><input name="email" type="text" id="email" size="50" style="width: 400px;" /></td>
                        </tr>
                        <tr>
                                <td> 
                                <td><input type="submit" name="Submit" value="Submit"></input> <input type="reset" name="Submit2" value="Reset" /></td>
                        </tr>
                </table>
                </form>
<?

Comments 7

  1. Pingback: Up, up, and away with Urban Airship « DesertJim

  2. Hi! Nice tuto. I’m trying to send some notifications to my phone with your php code but it didnt work.
    I put my registration_id in the “$dictionary[‘apids’]”, for the $url i put “https://android.apis.google.com/c2dm/send” and i add my google account for $username and $password! Can you tell me if i did something wrong? cause it just print me “error”! Thanks

  3. Sure,
    Here are some things that I am noticing:

    • The url field should not need to be changed.
    • The $username and $password values are from the apps page on your urban airship account. I updated the post to explain that.

    Sorry for the confusion.

  4. I would like to use the same function to push both to iPhone and Android applications
    would the same code work for iPhone as well ?

  5. I haven’t tested this on iphone. But, I do know that the Json is different for sending on IOS. The IOS json looks something like the following:
    {"aps": {"alert": "Hello from Urban Airship!"}, "device_tokens": ["ios_token_here"]}
    where as the android looks like:

    {"android": {"alert": "Hello from Urban Airship!"}, "apids": ["apid_here"]}

    Outside of that I believe the rest would just work.

Leave a Reply

Your email address will not be published. Required fields are marked *