Implement GCM (GoogleCloudMessaging) Cloud push messages in php

Source: Internet
Author: User
First, we need to obtain the apikey and use the google account to log on to the code. google. on the comapisconsole, click apiaccess. on this page, you can view the applied appkey. If not, click createnewserverkey... after obtaining the appkey, the button simply uses the php code :? Phpclasspush

First we want to get the api key, use google Account Login https://code.google.com/apis/console click api access below on the page can see the applied app key if not can click create new server key... if the button gets the app key, the php code is simple :? Phpclass push

First we want to get the api key, use the google account to log on to the https://code.google.com/apis/console click api access as follows

On this page, you can see the applied app key. If not, click the create new server key... button, as shown in figure

After obtaining the app key, the php code is simple:

  Load-> Model ('common'); // $ this-> common-> requireLogin ();} function index () {$ reg_id = "APA91bGLGb2AiMM-gLMRKNKqlrsKRJHFjQ1_1XFkmlITjd8X7ucV9WFFAOmkICkMn7hyMqIevkET-XrprY32LtiAuZtzreYYow4aiZCpH_Q04dfOiC85e-bH7NRIMsF0W3BvJ_ki8PE5 "; $ msg = "00000"; $ this-> send_gcm_policy ($ reg_id, $ msg);} function send_gcm_policy ($ reg_id, $ message) {$ fields = array ('registration _ id' => array ($ reg_id), 'data' => array ("message" => $ message ),); $ headers = array ('authorization: key = '. 'aizasybv4vw7n9ajdjwox9muwc ******* ', // The obtained app 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, success, true); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($ ch, CURLOPT_POSTFIELDS, json_encode ($ fields); $ result = curl_exec ($ ch); if ($ result = FALSE) {die ('problem occurred :'. curl_error ($ ch);} curl_close ($ ch); echo $ result ;}}?>

$ Reg_id in the Code is obtained by calling the method in gcm. jar in the app, as follows. In the test code, the ID is written to the server.

import com.google.android.gcm.GCMRegistrar; final String regId = GCMRegistrar.getRegistrationId(this);

The interaction of the entire push system should be:

1. When the client app runs, the regId is obtained successfully, sent to the server, and saved to the database.

2. The server selects a regId based on certain conditions, and then calls the google PUSH Service.


If the push is successful, the following message is returned:

{   multicast_id: 5381354139383071000,   success: 1,   failure: 0,   canonical_ids: 0,   results: [    {      message_id: "0:1377835083519762%2adac3a0f9fd7ecd"    }   ]}

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.