-
If you are using phonegap then install phonegap push notification plugin via
ORcordova plugin add phonegap-plugin-push
If you are working on native coding, You've to install Android Push Notificaion SDK - Create a project on Google Developers Console and enable "Cloud Messaging for Android" API for this project
-
This code execute after device ready :
var push = PushNotification.init({ "android" : { "senderID" : "9XX5XXX346XX"// This is your project number which you've created on Google Developers Console }, "ios" : {}, "windows" : {} }); push.on('registration', function(data) { //data.registrationId //send this token on server }); push.on('notification', function(data) { if (data.sound) {// play specific sound on notification received(When app running) var snd = new Media('file:///android_asset/www/sound/' + data.sound); snd.play(); } if (!data.additionalData.foreground) { // app is NOT RUNNING and new notification is RECEIVED } else { // app is RUNNING and new notification is RECEIVED } }); push.on('error', function(e) { //alert(JSON.stringify(data)) // e.message });
-
PHP Code for send push notification
$api_key = "AXXaSyXXyKXXNZXXf8O27hXX2DcrXXAEeXXuXX0";// your Google Developers Console Project API key $data = array( "message" => $message, 'title' => $title, ); // 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); curl_close($curl_handle);
- 3:26 PM
- 15 Comments