PHP-C2DM应用服务器的实现
我正在尝试使用以下代码设置C2DM推送消息的应用程序服务器部分- 我已经完成了应用程序方面的工作,并在Google上注册了一个电子邮件地址——每次我运行代码(在安装了php/cURL的服务器上)时,我都会收到错误“get auth token error”。这让我发疯,因为我不知道从哪里开始解决这个问题 我在代码中唯一更改的行是-在s2dm.php文件中-PHP-C2DM应用服务器的实现,php,android-c2dm,Php,Android C2dm,我正在尝试使用以下代码设置C2DM推送消息的应用程序服务器部分- 我已经完成了应用程序方面的工作,并在Google上注册了一个电子邮件地址——每次我运行代码(在安装了php/cURL的服务器上)时,我都会收到错误“get auth token error”。这让我发疯,因为我不知道从哪里开始解决这个问题 我在代码中唯一更改的行是-在s2dm.php文件中- 'source' => 'com.phonegap.chillimusicapp', $result = $
'source' => 'com.phonegap.chillimusicapp',
$result = $c2dm->getAuthToken("email@googlemail.com", "password");
我在post.php文件中添加了我的电子邮件/密码-
'source' => 'com.phonegap.chillimusicapp',
$result = $c2dm->getAuthToken("email@googlemail.com", "password");
任何建议都很好!
干杯
Paul尝试使用下面的示例代码,它运行良好
<?php
define("C2DM_ACCOUNT_EMAIL","[C2DM_EMAIL]");
define("C2DM_ACCOUNT_PASSWORD","[C2DM_PASSWORD]");
define("C2DM_CLIENT_LOGIN_URL","https://www.google.com/accounts/ClientLogin");
define("C2DM_MSG_SEND_URL","https://android.apis.google.com/c2dm/send");
function sendPushNotification($device_reg_id,$msg){
$auth_id=get_auth_id(); // To get Auth ID
$post_fields=array(
'collapse_key=ck_1',
'registration_id='. trim($device_reg_id),
'data.payload='. trim($msg),
);
$data_str=implode('&', $post_fields);
$headers = array(
'Authorization: GoogleLogin auth='.trim($auth_id),
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.trim(strlen($data_str)),
'Connection: close'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,C2DM_MSG_SEND_URL);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// print_r($server_output);
}
function get_auth_id(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,C2DM_CLIENT_LOGIN_URL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Email=".C2DM_ACCOUNT_EMAIL."&Passwd=".C2DM_ACCOUNT_PASSWORD."&accountType=GOOGLE&source=Google-cURL-Example&service=ac2dm");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// print_r($server_output);
$parts=explode("Auth=",$server_output);
$auth_id=$parts[1];
// echo $auth_id;
return $auth_id;
}
$reg_id = "[DEVICE_REG_ID]";
sendPushNotification($reg_id,"Hello World...!! Jay is testing C2DM...");
你好,Jay-非常感谢-这将完全取代以前的解决方案?通常的做法是将其与受保护的邮件提交表单绑定在一起吗?是的,Paul,这将取代您的解决方案。但我认为这将满足您的需要,对吗?嗨,杰,为您的帮助干杯-尽管我只是在提交时遇到了一个“无效注册”错误!?您可以从Android端获取注册ID,您可以通过WS将注册ID保存在DB中的某个位置。然后您可以使用该ID发送推送通知。