Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Engineering Concepts (http://www.go4expert.com/articles/engineering-concepts-tutorials/)
-   -   Send Android Push Notifications using GCM & PHP (http://www.go4expert.com/articles/send-android-push-notifications-using-t29896/)

pradeep 7Nov2013 19:36

Send Android Push Notifications using GCM & PHP
 
GCM or Google Cloud Messaging is a service for developers to send notifications from remote servers to their Android apps or Chrome extensions, the notification may be sent anytime irrespective of the Android device being online or not, the notification will be delivered when the device comes online. It helps Android applications to go into sleep mode when idle and they will be brought back when the Android system receives a notification. GCM is analogous to Apple's APNS.

The whole process works as follows:

Android application sends sender ID & application ID to GCM server for registering the device.
On successful registration, the GCM server responds with a registration ID.
  • The app then sends the registration ID to the application developer's server
  • The application developer uses the registration ID & API key to send notifications using GCM.
In your Google API Console you'll need to enable Google Cloud Messaging for Android, and get the API key from the API access section. When you have all these you can start coding for sending notifications through GCM.

Sending Notifications to GCM with PHP



For accessing the GCM you'll need CURL & JSON module (or PHP based JSON encoder/decoder), now let's get down to coding:

Code: PHP

<?
// Your API key
$api_key = "123456";

// Client IDs from your application
$registration_ids = array( "YPA91bEMm0IIwQib8iikAaxLQMLEaz6c3qmvvo2GSVvcrR3dR-CQTk9rWG3B-diVxJ02sMoZi0GGLMvREjFjg2SwbUx0yLNj1ZcMhQiX3l6c5653Pg9S_uggGD0wxpa8xyBnojFv0qg3", "XPA91bEorfbAG8is8AS6GZoEWgphsVDhA6QRHHJPrkZnsKGlKn8rd2UAxVLjjzjWb4ntyb5X10hVHZq31Ar2SGSlcXw41E-75XbUWVzTtTChb85fJErVMrFe8WflQPmrXLl5hlbWg_Al" );

$message = "hello, test!!";

// URL to POST to
$gcm_url = 'https://android.googleapis.com/gcm/send';

// data to be posted
$fields = array(
                'registration_ids'  => $registration_ids,
                'data'              => array( "message" => $message ),
                );

// headers for the request
$headers = array(
                    'Authorization: key=' . $api_key,
                    'Content-Type: application/json'
                );

$curl_handle = curl_init();

// set CURL options
curl_setopt( $curl_handle, CURLOPT_URL, $gcm_url );

curl_setopt( $curl_handle, CURLOPT_POST, true );
curl_setopt( $curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, true );

curl_setopt( $curl_handle, CURLOPT_POSTFIELDS, json_encode( $fields ) );

// send
$response = curl_exec($curl_handle);

curl_close($curl_handle);

// let's check the response
$data = json_decode($response);

foreach ($data['results'] as $key => $value) {
    if ($value['registration_id']) {
        printf("%s has a new registration id: %s\r\n", $key, $value['registration_id']);
    }
    if ($value['error']) {
        printf("%s encountered error: %s\r\n", $key, $value['error']);
    }
    if ($value['message_id']) {
        printf("%s was successfully sent, message id: %s", $key, $value['message_id']);
    }
}
?>


That looks pretty easy, but in case you run into non-JSON/non-2xx response, please check the Google documentation for GCM.


All times are GMT +5.5. The time now is 00:37.