PHP tutorials,a way to getting some thing new in web

Followers

Powered by Blogger.

Saturday 30 August 2014

Notification to android apps using PHP

No comments :
WE have created website but also created application for our website.here i have created small code for sending notification thru CURL to android apps using php.First try to create your google application on google developers and make android notification ON.

After completing above steps,you will get an application access key for your code.just paste that in your code.and get registration id from an android developer so that u will able to send notification to that device.




Code:

<?php

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );


$registrationIds = array( $_GET['id'] );

// prep the bundle
$msg = array
(
    'message' => 'first push notification message',
'title' => 'test title',
'subtitle' => 'subtitle',
'tickerText' => 'ticker',
'vibrate' => 1,
'sound' => 1
);

$message_array = 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( $message_array ) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;
?>

You will get notification as output of this code.This is known as Android GCM notification from PHP.



No comments :

Post a Comment