I think application crashes only in some devices during installation time. It's because during installation GCM service also receives some Intent
from other Google source and your broadcast receiver is not prepared to handle this type of Intent
.
If you just want to receive GCM Intent which you want to pull from server through push notification, then just use this in your handle Intent call.
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);
}