Search code examples
androidfirebase-cloud-messaging

Unresolved class 'MyFirebaseMessagingService'


I'm following the tutorial on Setting up a Firebase Cloud Messaging client app on Android which involves adding a <service> component to AndroidManifest.xml.

<service android:name=".java.MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

However, I'm getting Unresolved class MyFirebaseMessagingService. Where exactly do I import that class from?

NOTE: I'm writing in Kotlin


Solution

  • On the same link that you have provided, it is mentioned that you need to add a service class in your project and declare it in your AndroidManifest.xml like this:

    <service android:name=".java.MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    

    So basically these are the steps that you need to follow:

    1. Inside your package where you have kept Java classes, create a class called MyFirebaseMessagingService (you can choose any name)
    2. Declare it in AndroidManifest.xml as shown above.
    3. This class which you have defined in step 1 above, will extend FirebaseMessagingService, therefore it will have a method called onMessageReceived, that you will have to override, as shown below:

         @Override
          public void onMessageReceived(RemoteMessage remoteMessage) {
              // ...
      
          // TODO(developer): Handle FCM messages here.
          Log.d(TAG, "From: " + remoteMessage.getFrom());
      
          // Check if message contains a data payload.
          if (remoteMessage.getData().size() > 0) {
              Log.d(TAG, "Message data payload: " + remoteMessage.getData());
      
              if (/* Check if data needs to be processed by long running job */ true) {
                  // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
                  scheduleJob();
              } else {
                  // Handle message within 10 seconds
                  handleNow();
              }
      
          }
      
          // Check if message contains a notification payload.
          if (remoteMessage.getNotification() != null) {
              Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
          }
      
          // Also if you intend on generating your own notifications as a result of a received FCM
          // message, here is where that should be initiated. See sendNotification method below.
      }
      

    If you are writing code in Kotlin, then you need to name your class as MyFirebaseMessagingService.kt and then create a class and put below code. This is also mentioned on that link itself.

    override fun onMessageReceived(remoteMessage: RemoteMessage?) {
    // ...
    
    // TODO(developer): Handle FCM messages here.
    Log.d(TAG, "From: ${remoteMessage?.from}")
    
    // Check if message contains a data payload.
    remoteMessage?.data?.isNotEmpty()?.let {
        Log.d(TAG, "Message data payload: " + remoteMessage.data)
    
        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
            scheduleJob()
        } else {
            // Handle message within 10 seconds
            handleNow()
        }
    }
    
    // Check if message contains a notification payload.
    remoteMessage?.notification?.let {
        Log.d(TAG, "Message Notification Body: ${it.body}")
    }
    
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
    }