Firebase onMessageReceived is crashing the application

Issue

Does anyone faced with the similar problem before? Whenever I send message, app is crashing immediately. Here is my MessageReceiver class:

public class MessageReceiver extends FirebaseMessagingService {
        final String TAG = "MessageReceiver";
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {

            Log.d(TAG, "onMessageReceived: "+remoteMessage.getFrom());

            if(remoteMessage.getData().size() >0){
                Log.d(TAG,"payload: "+remoteMessage.getData());
            }
        }
    }

I even tried with empty method onMessageReceived still crushing the app.

This is the version that Im compiling:

compile 'com.google.firebase:firebase-messaging:10.0.1'

This is the json that Im sending to app through PostMan:

{
    "to":"d55nTfZR5-A:APA91bFW88heY4Hoh34...",
    "data":{
        "message":"hello"
    }
}

LogCat:

Solution

I figure it out. If anyone face with the similar problem in the feature you need to make sure that you also compile firebase-core along firebase-messaging and that you match the same version with google play services.

 compile 'com.google.firebase:firebase-messaging:11.0.2'
 compile 'com.google.firebase:firebase-core:11.0.2'

After this everything worked fine. Thanks @FnR for your suggestions.

Edit 9th June 2022

This are the current most latest versions:

 implementation 'com.google.firebase:firebase-messaging:23.0.5'
 implementation 'com.google.firebase:firebase-core:21.0.0'

Also want to mention that it is a good practice to use now Firebase Android BoM (Bill of materials) to manage your Firebase dependencies, where the versions will be taken care of for you.
Example:

dependencies {
      // Import the BoM for the Firebase platform
      implementation platform('com.google.firebase:firebase-bom:30.1.0')
    
      // Declare the dependencies for the desired Firebase products without specifying versions
      // For example, declare the dependencies for Firebase Authentication and Cloud Firestore
      implementation 'com.google.firebase:firebase-auth'
      implementation 'com.google.firebase:firebase-firestore'
    }

The above snippet is from Firebase docs, please for more info see: https://firebase.google.com/docs/android/learn-more

Answered By – Alexander

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published