Use Google Cloud Messaging (GCM), PHP to develop Android push notifications (Android push notifications)

Source: Internet
Author: User
Tags ssl certificate

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/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.