开发者

Android C2DM Application

开发者 https://www.devze.com 2023-02-07 11:43 出处:网络
I am developing an applica开发者_开发知识库tion using C2DM. I have received registration id. How can I send it to a Server. Can anyone explain me with code.?Here is a PHP function to send C2DM message

I am developing an applica开发者_开发知识库tion using C2DM. I have received registration id. How can I send it to a Server. Can anyone explain me with code.?


Here is a PHP function to send C2DM messages to a phone, with registration id.

/** * Sends a message to the phone.

  • @static
  • @access public
  • @param string $authCode Google authorization code of this server obtained through googleAuthenticate()
  • @param string $deviceRegistrationId The device registration id obtained from Google during device registration. Identifies the device.
  • @param string $msgType The type of this message (an Application thing). This will also serve as the collapse_key.
  • @param string $messageText the message to send to the phone.
  • @return boolean True on success or false on failure */

function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText) {

$headers = array('Authorization: GoogleLogin auth=' . $authCode);
$data = array(
    'registration_id' => $deviceRegistrationId,
    'collapse_key' => $msgType,
    'data.message' => $messageText         
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
if ($headers)
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

// for debugging the request
//curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging the request

$response = curl_exec($ch);

// for debugging the request
//var_dump(curl_getinfo($ch)); 
//   var_dump($response);

curl_close($ch);

return $response; }
0

精彩评论

暂无评论...
验证码 换一张
取 消