Send Android Push Notifications using GCM & PHP

Discussion in 'Android' started by pradeep, Nov 7, 2013.

  1. pradeep

    pradeep Team Leader

    Apr 4, 2005
    Likes Received:
    Trophy Points:
    Kolkata, India
    Home Page:
    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:

    // 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 '';

    // 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_handleCURLOPT_URL$gcm_url );

    curl_setopt$curl_handleCURLOPT_POSTtrue );
    curl_setopt$curl_handleCURLOPT_RETURNTRANSFERtrue );

    curl_setopt$curl_handleCURLOPT_POSTFIELDSjson_encode$fields ) );

    // send
    $response curl_exec($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.
  2. haktuts

    haktuts New Member

    Jul 2, 2015
    Likes Received:
    Trophy Points:
    I want to get open the url via push notification. please provide the details steps for similer qwery.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice