这个问题已经在这里有了答案:
已关闭8年。
尝试启动并运行GCM,但我的设备正在发送注册请求,但没有收到来自GCM服务器的响应。下面,我包括 list 和GCMIntentService定义,以及向GCM服务器注册的调用。很久以来我一直在盯着它,如果可能的话,我可能会遗漏一些明显的东西,对不起。
欢迎所有想法!
这是我的 list :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="PACKAGE"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<permission android:name="PACKAGE.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="PACKAGE.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name=".GCMIntentService"
android:enabled="true"/>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="PACKAGE.GCMIntentService" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的GCMIntentService,用于处理来自GCM的退货:
import com.google.android.gcm.GCMBaseIntentService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class GCMIntentService extends GCMBaseIntentService {
public static String TAG = "GCMIntentService";
public GCMIntentService(String senderId) {
super(senderId);
Log.d("GCMIntentService", senderId);
}
@Override
protected void onError(Context arg0, String arg1) {
Log.d("onError", arg1);
}
@Override
protected boolean onRecoverableError(Context context, String errorId){
Log.d("onRecoverableError", errorId);
return false;
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
Log.d("onMessage", String.valueOf(arg1));
}
@Override
protected void onRegistered(Context arg0, String arg1) {
Log.d("onRegistered", arg1);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
Log.d("onUnregistered", arg1);
}
}
我尝试在我的主类的onCreate方法中注册以下内容:
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.d(TAG, "Already registered");
}
更新
我尝试了Raz的建议,但遇到以下错误:
07-03 12:07:40.459: W/dalvikvm(341): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-03 12:07:40.508: E/AndroidRuntime(341): FATAL EXCEPTION: main
07-03 12:07:40.508: E/AndroidRuntime(341): java.lang.RuntimeException: Unable to instantiate service package.gcm2.GCMIntentService: java.lang.InstantiationException: package.gcm2.GCMIntentService
07-03 12:07:40.508: E/AndroidRuntime(341): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2943)
07-03 12:07:40.508: E/AndroidRuntime(341): at android.app.ActivityThread.access$3300(ActivityThread.java:125)
07-03 12:07:40.508: E/AndroidRuntime(341): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087)
07-03 12:07:40.508: E/AndroidRuntime(341): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 12:07:40.508: E/AndroidRuntime(341): at android.os.Looper.loop(Looper.java:123)
07-03 12:07:40.508: E/AndroidRuntime(341): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-03 12:07:40.508: E/AndroidRuntime(341): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 12:07:40.508: E/AndroidRuntime(341): at java.lang.reflect.Method.invoke(Method.java:521)
07-03 12:07:40.508: E/AndroidRuntime(341): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-03 12:07:40.508: E/AndroidRuntime(341): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-03 12:07:40.508: E/AndroidRuntime(341): at dalvik.system.NativeStart.main(Native Method)
07-03 12:07:40.508: E/AndroidRuntime(341): Caused by: java.lang.InstantiationException: package.gcm2.GCMIntentService
07-03 12:07:40.508: E/AndroidRuntime(341): at java.lang.Class.newInstanceImpl(Native Method)
07-03 12:07:40.508: E/AndroidRuntime(341): at java.lang.Class.newInstance(Class.java:1429)
07-03 12:07:40.508: E/AndroidRuntime(341): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2940)
07-03 12:07:40.508: E/AndroidRuntime(341): ... 10 more
07-03 12:08:34.560: I/Process(341): Sending signal. PID: 341 SIG: 9
最佳答案
在 list 中尝试以下操作:
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="eimmer.liav.elucidate.gcm2" />
</intent-filter>
</receiver>
类别名称必须是您的应用程序包。
如果服务使用相同的程序包名称,则将其保留,就像您将其更改为实际程序包而不是
.GCMIntentService
一样,但是您还需要extend
和override
GCMBroadcastReceiver
。如果您的软件包不是
eimmer.liav.elucidate.gcm2.GCMIntentService
,则必须扩展 GCMBroadcastReceiver 并覆盖函数 getGCMIntentServiceClassName()。在该函数中,以字符串示例
eimmer.liav.example.GCMIntentService
返回实际的包和类。同样不要忘了将接收者名称更改为新名称。例子:
<receiver
android:name="eimmer.liav.example.MyGCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="eimmer.liav.elucidate.gcm2" />
</intent-filter>
</receiver>
进一步将您的服务名称更改为:
<service android:name="eimmer.liav.example.GCMIntentService"
android:enabled="true"/>
根据Google的另一件事,您需要将您的构造函数更改为默认的并发送一些类似这样的字符串:
public GCMIntentService() {
super("Test");
}
据我所知,该字符串并不是很重要。
玩得开心。