如何解决Android:C2DM无效注册错误
| 一切似乎都正常,直到我尝试发出通知为止。该应用获得了注册ID,服务器获得了Auth代码,但显然注册ID无效。 从应用程序获取注册后运行PHP代码时,它将返回returns0ѭ 我将发布有关C2DM实现和服务器端CURL请求的所有代码。 在OnCreate内部:Intent registrationIntent = new Intent(\"com.google.android.c2dm.intent.REGISTER\");
registrationIntent.putExtra(\"app\",PendingIntent.getBroadcast(this,new Intent(),0));
registrationIntent.putExtra(\"sender\",\"MYEMAIL\");
startService(registrationIntent);
C2DM类:
import java.net.URL;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;
public class C2DMReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context,Intent intent) {
String receiver = context.getPackageName() + \".C2DMReceiver\";
intent.setClassName(context,receiver);
context.startService(intent);
if (intent.getAction().equals(\"com.google.android.c2dm.intent.REGISTRATION\")) {
handleRegistration(context,intent);
} else if (intent.getAction().equals(\"com.google.android.c2dm.intent.RECEIVE\")) {
handleMessage(context,intent);
}
}
private void handleRegistration(Context context,Intent intent) {
String registration = intent.getStringExtra(\"registration_id\");
if (intent.getStringExtra(\"error\") != null) {
// Registration failed,should try again later.
} else if (intent.getStringExtra(\"unregistered\") != null) {
// unregistration done,new messages from the authorized sender will be rejected
} else if (registration != null) {
// Send the registration ID to the 3rd party site that is sending the messages.
// This should be done in a separate thread.
// When done,remember that all registration is done.
sendReg(registration,context);
Toast.makeText(context,registration,Toast.LENGTH_SHORT).show();
}
}
private void handleMessage(Context context,Intent intent)
{
}
public void sendReg(String key,Context context)
{
//Send key to server,this works fine
}
}
PHP代码:
<?php
// get Auth from Google ClientLogin
$new = shell_exec(\"curl -d accountType=HOSTED_OR_GOOGLE -d Email=MYEMAIL -d Passwd=MYPASSWORD -d service=ac2dm -d source=MYSOURCE -k https://www.google.com/accounts/ClientLogin | grep Auth=\");
$auth = substr($new,5); // \"DQAAAKY...\"
$fp = fopen(\'/home/me/Desktop/test.txt\',\'w\');
fputs($fp,$new.\"\\nAuth: \".$auth.\"\\n\");
// ID is from the app
$id = \"APA91b...\";
fputs($fp,\"ID: \".$id.\"\\n\\n\");
$msg = \"hello\";
// if I don\'t include ContentLength,Google tells me I need it
$len = strlen(\"registration_id=\".$id.\"&data.message=\".$msg.\"&collapse_key=something\");
fputs($fp,\"Len: \".$len.\"\\n\\n\");
$info = shell_exec(\"curl --header \\\"Content-Length: \".$len.\"\\\" --header \\\"Authorization: GoogleLogin auth=\".$auth.\"\\\" -d registration_id=\".$id.\" -d data.message=\".$msg.\" -d collapse_key=something -k https://android.apis.google.com/c2dm/send\");
fputs($fp,$info); // \"Error=InvalidRegistration\"
fclose($fp);
?>
解决方法
您确定整个数据包都正确发送了吗?尝试在使用shell_exec的两行中同时使用fputs,以确保其格式正确。除了您做错的事情,我看不到其他任何东西。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。