我认为应用程序在安装期间仅在某些设备中崩溃。这是因为在安装过程中,GCM 服务还会Intent
从其他 Google 来源接收一些内容,而您的广播接收器还没有准备好处理这种类型的Intent
.
如果您只想通过推送通知接收您想从服务器中提取的 GCM Intent,那么只需在您的句柄 Intent 调用中使用它。
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
//String msg = intent.getStringExtra("message");
String from=extras.getString("from");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendErrorNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendErrorNotification("Deleted messages on server: " + extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i = 0; i < 5; i++) {
Log.i(TAG, "Working... " + (i + 1) + "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
// sendNotification("Received: " + extras.toString());
/*********ERROR IN SOME DEVICES*****************/
if(from.equals("google.com/iid"))
{
//related to google ... DO NOT PERFORM ANY ACTION
}
else {
//HANDLE THE RECEIVED NOTIFICATION
String msg = intent.getStringExtra("message");
sendNotification(msg);
Log.i(TAG, "Received: " + extras.toString());
}
/**************************/
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}