среда, 26 ноября 2014 г.

How to send push notification to Android phone (PHP)

Run this code on http://phpfiddle.org
<?php
define( 'API_ACCESS_KEY', 'AIzaSyCTcXRRgm7g3YuKxZU9w88KwtY2QIU4zSU' );
$registrationIds = array("APA91bHno1hY5_dBroaqhN0PKb5VEXvmF8hZ1Hzm8qsPL1zMnn3pzJbLDZXNj6qfXYjK3VGhU8Wo6saVqHc3PszWcKLlJB_CwxrCcB0t3rsPNz5JIZgu_HXh_T24SaF7tYI5q9dltVBSMGAFWaFb-QQdWPpvNgzLF2EWV48vb3Pc7mpmEtjAVrU" );

$msg = array
(
    'message'       => 'here is a message. message',
    'title'         => 'This is a title. title',
    'subtitle'      => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'sound'     => 1
);

$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'              => $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
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( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;
?>