19

I have developed a website using php and mysql.I want to send notification to Android device for every post which is uploaded to my website.

Is it possible to send notification using GCM Server? If yes, then how can I send notification to all devices which have the Android app installed?

2

3 Answers 3

23

I have decided to post answer for my own question

Now Google introduced FCM

<?php
define('API_ACCESS_KEY','Api key from Fcm add here');
 $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
 $token='235zgagasd634sdgds46436';

    $notification = [
            'title' =>'title',
            'body' => 'body of message.',
            'icon' =>'myIcon', 
            'sound' => 'mySound'
        ];
        $extraNotificationData = ["message" => $notification,"moredata" =>'dd'];

        $fcmNotification = [
            //'registration_ids' => $tokenList, //multple token array
            'to'        => $token, //single token
            'notification' => $notification,
            'data' => $extraNotificationData
        ];

        $headers = [
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        ];


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$fcmUrl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
        $result = curl_exec($ch);
        curl_close($ch);


        echo $result;
3
  • Where to get the token value?
    – natsumiyu
    Sep 26, 2019 at 3:12
  • @natsumiyu.from android device.you need implement fcm in android side
    – scott
    Sep 26, 2019 at 13:49
  • There is a new FCM API implementation v1, will this still work or it needs some revisions?
    – Ajowi
    Jan 2 at 6:36
15

First of all follow Implementing GCM Client and implement the gcm client in your android app.

For GCM Server in php you can implement it in the following way. Edit your code according to your need like code it for when publishing the post. For more about implementing the GCM Server go to Implementing GCM Server

<?php
    // Replace with the real server API key from Google APIs
    $apiKey = "your api key";

    // Replace with the real client registration IDs
    $registrationIDs = array( "reg id1","reg id2");

    // Message to be sent
    $message = "Your message e.g. the title of post";

    // Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';

    $fields = array(
        'registration_ids' => $registrationIDs,
        'data' => array( "message" => $message ),
    );
    $headers = array(
        'Authorization: key=' . $apiKey,
        'Content-Type: application/json'
    );

    // Open connection
    $ch = curl_init();

    // Set the URL, number of POST vars, POST data
    curl_setopt( $ch, CURLOPT_URL, $url);
    curl_setopt( $ch, CURLOPT_POST, true);
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields));

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // curl_setopt($ch, CURLOPT_POST, true);
    // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));

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

    // Close connection
    curl_close($ch);
    // print the result if you really need to print else neglate thi
    echo $result;
    //print_r($result);
    //var_dump($result);
?>

There is also a good post Android Push Notifications for beginners.

2
  • This worked, great! However, i wonder what happens if Google changes this url: https://android.googleapis.com/gcm/send ? How likely is that ? If likely, are there any PHP libraries that will keep up with the changes, i don't want to manually change that URL in my app in case Google changes it.
    – doubleOrt
    Jul 30, 2017 at 12:08
  • how can i guet the api key? are to much in console google. Nov 15, 2018 at 18:45
1

There is some changes into the code in comment the define(api key line) and put / paste it as my code in php. its working fine comment this :

define('API_ACCESS_KEY','Api key from Fcm add here');

comment or replace this code line :

'Authorization: key=' . API_ACCESS_KEY

//define('API_ACCESS_KEY','Api key from Fcm add here');

$apiKey = 'Api key from Fcm add here';

$fcmUrl = 'https://fcm.googleapis.com/fcm/send';

$token='235zgagasd634sdgds46436';

 $notification = [
        'title' =>'title',
        'body' => 'body of message.',
        'icon' =>'myIcon', 
        'sound' => 'mySound'
    ];
    $extraNotificationData = ["message" => $notification,"moredata" =>'dd'];

    $fcmNotification = [
        //'registration_ids' => $tokenList, //multple token array
        'to'        => $token, //single token
        'notification' => $notification,
        'data' => $extraNotificationData
    ];

    $headers = [
        'Authorization: key=' . $apiKey,
        'Content-Type: application/json'
    ];


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$fcmUrl);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
    $result = curl_exec($ch);
    curl_close($ch);

    echo $result;

Not the answer you're looking for? Browse other questions tagged or ask your own question.