What is GCM?
Google Cloud Messaging (GCM) is a service provided by Google to send push notifications from the server to Android devices.
GCM is divided into client and server development.
Here we only introduce server-side development. In fact, the process is very simple, just use PHP to send post data.
API Key acquisition?
Want to add?
classGCM { Public $api _key= "AIZASYAU3WZS9RAIK-MHQ"; function__construct () {}/** * Sending Push Notification*/ Public functionSend_notification ($registatoin _ids,$message) { //Set POST Variables $url= ' Https://android.googleapis.com/gcm/send '; $fields=Array( ' Registration_ids ' =$registatoin _ids, ' data ' =$message, ); $headers=Array( ' authorization:key= '.$this->api_key, ' Content-type:application/json ' ); //Open Connection $ch=Curl_init (); //Set The URL, number of post VARs, post datacurl_setopt ($ch, Curlopt_url,$url); curl_setopt ($ch, Curlopt_post,true); curl_setopt ($ch, Curlopt_httpheader,$headers); curl_setopt ($ch, Curlopt_returntransfer,true); //disabling SSL Certificate support temporarlycurl_setopt ($ch, Curlopt_ssl_verifypeer,false); curl_setopt ($ch, Curlopt_postfields, Json_encode ($fields)); //Execute Post $result= Curl_exec ($ch); if($result===FALSE) { die(' Curl failed: '. Curl_error ($ch)); } //Close ConnectionCurl_close ($ch); return $result; }}
Run the program
$device _id = "Apa91bffbkpobz-rk"; $registatoin _ids Array ($device _id); $message Array ("Data" = "Test content from Finley"); $GCM New GCM (); $result $gcm->send_notification ($registatoin _ids$message); Echo $result;
Reference:
Http://developer.android.com/google/gcm/server.html#params
http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/