Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could Intent be null in onHandleIntent()?

Tags:

My android app crashes and this is the logcat :-

java.lang.NullPointerException
    at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:194)
    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.os.HandlerThread.run(HandlerThread.java:60)

I looked into android gcm r3 source and I found that the argument intent is null in onHandleIntent().

Is this even possible? How to fix it?

(I know null intent could be seen with Service.onStartCopmmand returning START_STICKY but IntentService.onStartCommand doesn't use START_STICKY.)

like image 478
NoraBora Avatar asked Apr 04 '13 10:04

NoraBora


Video Answer


1 Answers

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);
}
like image 198
amit Avatar answered Sep 22 '22 18:09

amit