Sample php file for send push notification to Android devices

3:49 PM

I've write a php file for send push notification to Android devices :

$registration_ids = array(
"fZWd8yT7uLQ:APA91bFywoAAWmXDRLkovcrR9cnIlk_ezTFSlu2nWVMv6LAffE6OC0Ox-gI_n-zPIYeLEzQ4DSKe2bowruSqNgxU3srzcMoyNAwgqmbT2IxjbgNsk7kNRm_ULrebJkcjHUGAfvIhQ3Ze"
);
$api_key = "AIzaSyCdsdFPkRPgkOL8ramcn4J2O1UEr56hMnY";

$data =  array(
            "message" => 'This is test',
            'title' => 'this is title',
            'notId'=>mt_rand(1, 115),  
            //"style" => "inbox", // if you want to show notification in inbox style
    "summaryText" => "There are %n% notifications", // if you want to show summary text in inbox style            
        );

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

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

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

$curl_handle = curl_init();

echo 'Notification PhP file';

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

echo $response;


curl_close($curl_handle);

You Might Also Like

0 comments