How to send notification on Android with php?

I developed a website using php and mysql. I want to send a notification to my Android device for every message that is uploaded to my site.

Can I send notifications using GCM Server? If so, how can I send a notification about all devices on which the Android application is installed?

+8
android php google-cloud-messaging
source share
3 answers

I decided to post the answer to my 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; 
+2
source share

First of all, follow the Deploy GCM Client and implement the gcm-client in your Android application.

For GCM Server in php you can implement it as follows. Edit your code according to your needs, for example, its code when publishing a publication. For more information about implementing the GCM Server, go to Deploying the 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 eg 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 publication of Android Push Notifications for beginners.

+12
source share

// There are some changes in the code in the comment define (api key line) and paste it as my code in php. his working fine // comment on this: - define ('API_ACCESS_KEY', 'Api key from Fcm add here'); // comment or replace this line of code: - "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; ?> 
0
source share

All Articles