我已经创建了 GCM Intent 服务,并且设备已成功注册。我也收到 REGISTRATION Intent,但 OnMessage 或 OnRegistered 方法没有被调用。
在我看到的日志下面。
07-23 06:24:23.542: V/GCMBroadcastReceiver(1168): onReceive: com.google.android.c2dm.intent.REGISTRATION
07-23 06:24:23.542: V/GCMBroadcastReceiver(1168): GCM IntentService class: com.app.demo.myApp.GCMIntentService
07-23 06:24:23.581: V/GCMBaseIntentService(1168): Acquiring wakelock
下面是 OnMessage 的代码。
有人可以帮我解释为什么 OnRegistered 或 OnMessage 没有被调用。
public class GCMIntentService extends GCMBaseIntentService {
@Override
protected void onMessage(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "registered", Toast.LENGTH_LONG).show();
String device_id = GCMRegistrar.getRegistrationId(this);
GSLogger.Log("mess_received", device_id);
}
@Override
protected void onRegistered(Context context, String arg1) {
Toast.makeText(context, "registered", Toast.LENGTH_LONG).show();
String device_id = GCMRegistrar.getRegistrationId(this);
GSLogger.Log("registered", device_id);
}
}
清单代码:
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-configuration android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="com.google.android.c2dm.permission.C2D_MESSAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.app.demo.myApp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.app.demo.myApp.permission.C2D_MESSAGE" />
清单中的接收方代码:
<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="com.app.demo.myApp" />
</intent-filter>
</receiver>
<service android:name="com.app.demo.myApp.GCMIntentService" />
我的包裹图片: