我正在尝试使用此代码设置应用服务器部分C2DM推送消息 - https://github.com/lytsing/c2dm-php。
我已完成应用程序方面的事情并已向Google注册了一个电子邮件地址 - 每次运行代码时(在安装了php / cURL的服务器上)我都会收到错误“get auth token error”。它让我疯狂,因为我不知道从哪里开始解决这个问题。
我在代码中更改的唯一行是 - 在s2dm.php文件中 -
'source' => 'com.phonegap.chillimusicapp',
我将我的电子邮件/密码添加到post.php文件中 -
$result = $c2dm->getAuthToken("email@googlemail.com", "password");
任何建议都会很棒! 干杯 保罗
答案 0 :(得分:1)
尝试使用下面的示例代码,它运行正常。
<?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...");
仅供参考!每次发送通知时都无需调用get_auth_id(),您也可以调用一次并将auth_id存储在配置文件中。