Unresolved class 'MyFirebaseMessagingService' Android Studio

18,999

Solution 1

See this image first Image with error

check if Firebase classes are in correct package folder. example if firebase classes are in package 'services' then in manifest service would have android:name=".services.FirebaseMessagingService" then Unresolved class 'MyFirebaseMessagingService' error will get solved.

enter image description here

Solution 2

<service
    android:name="com.google.firebase.messaging.FirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

Giving full path for this lib name com.google.firebase.messaging.FirebaseMessagingService fixed this issue.

Solution 3

try to change

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

by

 <service
            android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

Solution 4

Mohammed Farhan, solves my problem. Below is the detailed solution to it.

Here is the solution:

First of all, add below two statements into the manifest file:

<!--Everything for notifications part-->
<service
    android:name=".MyFirebaseMessagingService"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

<service
    android:name=".MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

The reason why we add MyFirebaseInstanceIDService class is to handle creation, updating of registration tokens.

Now, as the error is “Unresolved class”, create two classes in your root package (i.e. mentioned at top of the manifest file) and paste below code in both files:

1. MyFirebaseMessagingService.java:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseService";

public MyFirebaseMessagingService() {
}

@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.
    }
}

2. MyFirebaseInstanceIDService.java:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";

/**
 * Called if InstanceID token is updated. This may occur if the security of
 * the previous token had been compromised. Note that this is called when the InstanceID token
 * is initially generated so this is where you would retrieve the token.
 */
@Override
public void onTokenRefresh() {
    super.onTokenRefresh();

    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    sendRegistrationToServer(refreshedToken);
}

/**
 * Persist token to third-party servers.
 * <p>
 * Modify this method to associate the user's FCM InstanceID token with any server-side account
 * maintained by your application.
 *
 * @param token The new token.
 */
private void sendRegistrationToServer(String token) {
    // Add custom implementation, as needed.
}
}

And then you are all set. Lemme know if you have any hurdles.

Reference links: https://firebase.google.com/docs/cloud-messaging/android/client

Share:
18,999
Nikola Arsovski
Author by

Nikola Arsovski

Updated on June 08, 2022

Comments

  • Nikola Arsovski
    Nikola Arsovski almost 2 years

    I have problem with Firebase in Android studio. Here is what happen: In debug mod everything is working fine with follow AndroidManifest

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hajora.dndcommerce"
    android:installLocation="auto"
    android:versionCode="2"
    android:versionName="1.0"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21"
        tools:overrideLibrary="com.google.firebase.messaging"
        />
    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true"
     />
    <uses-permission android:name="android.permission.CALL_PHONE"  />
    <uses-feature android:name="android.hardware.telephony" android:required="false" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.hajora.dndcommerce.Education"
            android:label="@string/app_name">
        </activity>
        <activity android:name="com.hajora.dndcommerce.Splashscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.hajora.dndcommerce.SingleItemViewEducation" />
        <activity android:name="com.hajora.dndcommerce.SingleItemViewNews" />
        <activity android:name="com.hajora.dndcommerce.Main" />
        <activity android:name="com.hajora.dndcommerce.Submit" />
        <activity android:name="com.hajora.dndcommerce.News" />
        <activity android:name="com.hajora.dndcommerce.Contact" />
        <activity android:name="com.hajora.dndcommerce.About" />
        <activity android:name="com.hajora.dndcommerce.AboutCompany" />
        <activity android:name="com.hajora.dndcommerce.AboutHistory" />
        <activity android:name="com.hajora.dndcommerce.AboutVision" />
        <activity android:name="com.hajora.dndcommerce.AboutPartners" />
        <activity android:name="com.hajora.dndcommerce.Ask"
            android:windowSoftInputMode="stateUnchanged" />
        <activity android:name=".Partner" />
    </application>
        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <service
            android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
    

    When i switch to release mode, i get the error:

    Error:(54) Error: The <service> element must be a direct child of the <application> element [WrongManifestParent]
    

    So, i then put both services inside tag and after that i get the error:

    Unresolved class 'MyFirebaseMessagingService' less... (Ctrl+F1) 
    

    Validates resource references inside Android XML files.

    I cannot figure what is the problem

    UPDATE gradle

        apply plugin: 'com.android.application'
    
    android {
    
        compileSdkVersion 21
        buildToolsVersion "26.0.0"
        defaultConfig {
            applicationId 'com.hajora.dndcommerce'
            minSdkVersion 21
            targetSdkVersion 21
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
                signingConfig signingConfigs.release
            }
        }
        productFlavors {
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
    }
    
    dependencies {
        compile files('libs/activation.jar')
        compile files('libs/mail.jar')
        compile files('libs/additionnal.jar')
        compile 'com.google.firebase:firebase-messaging:10.0.1'
        compile 'com.android.support.constraint:constraint-layout:1.0.2'
        compile 'com.google.firebase:firebase-core:10.0.1'
    }
    apply plugin: 'com.google.gms.google-services'
    
  • Nikola Arsovski
    Nikola Arsovski over 6 years
    As i wrote, when i do that i get the error: Unresolved class 'MyFirebaseMessagingService' less... (Ctrl+F1) Validates resource references inside Android XML files. Unresolved class 'MyFirebaseInstanceIDService' less... (Ctrl+F1) Validates resource references inside Android XML files.
  • Vinayak B
    Vinayak B over 6 years
    try to add com.hajora.dndcommerce.MyFirebaseMessagingService in manifest
  • Vinayak B
    Vinayak B over 6 years
    And also remove <action android:name="com.google.firebase.MESSAGING_EVENT"/> from activity tag. I updated my answer. check it
  • Nikola Arsovski
    Nikola Arsovski over 6 years
    Did like you said but the same problem.
  • Vinayak B
    Vinayak B over 6 years
    Could you please check your MyFirebaseMessagingService package name in your class?
  • Nikola Arsovski
    Nikola Arsovski over 6 years
    I cannot find Firebase classes. I added Firebase via Android Studio tool (automatic), so i didnt put classes by myself
  • Jaime Montoya
    Jaime Montoya over 6 years
    Is there a template to write the MyFirebaseMessagingService and MyFirebaseInstanceIDService classes referenced at firebase.google.com/docs/cloud-messaging/android/client?
  • Jaime Montoya
    Jaime Montoya over 6 years
    I guess an example of the templates I am looking for could be at github.com/firebase/quickstart-android/tree/master/messaging‌​/….
  • Jaime Montoya
    Jaime Montoya over 6 years
    Confirmed, this is what I was looking for, from the official documentation: github.com/firebase/quickstart-android/tree/master/messaging‌​/….
  • MeLean
    MeLean over 2 years
    You should add android:exported="false" in the service tag as well.
  • ICW
    ICW about 2 years
    IDK why android studio docs don't have this as the full string