模拟器使用谷歌 API 镜像 4.3
设备有安卓 4.0.3
如前所述,该应用程序在两个系统上都成功注册,但仅在模拟器上接收。服务器在对 GCM 服务器的同一调用中向两个系统发送相同的通知,这些服务器正确响应所有指定的设备。
设备(配置了相同的应用程序和谷歌帐户)连接到同一个局域网。模拟器收到消息,设备没有。
这里的代码:
AndroidManifest.xml
<!-- PERMISSIONS -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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="it.uniroma1.informatica.didapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="it.uniroma1.informatica.didapp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<receiver
android:name="it.uniroma1.informatica.didapp.remote.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="it.uniroma1.informatica.didapp" />
</intent-filter>
</receiver>
<service android:name="it.uniroma1.informatica.didapp.remote.GCMIntentService" />
GCMIntentService.java
public class GCMIntentService extends IntentService {
public GCMIntentService() {
super(GCMIntentService.class.getName());
}
@Override
protected void onHandleIntent(Intent intent) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
Bundle extras = intent.getExtras();
if(!extras.isEmpty()) {
if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
} else if(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
} else if(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
sendNotification();
}
}
GCMBroadcastReceiver.completeWakefulIntent(intent);
Log.v("GCM", "HANDLED");
}
GCMBroadcastReceiver.java
public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(), GCMIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
Log.v("GCM", "RECEIVED");
}
}