Arsayu
Arsayu

Reputation: 356

Unresolved class 'MyFirebaseMessagingService' Android Studio

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'

Upvotes: 22

Views: 27972

Answers (6)

MrManshenoy
MrManshenoy

Reputation: 591

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

Upvotes: 47

Majdi Ajroud
Majdi Ajroud

Reputation: 31

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>

Upvotes: 2

Karan
Karan

Reputation: 83

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

Upvotes: 1

Jaime Montoya
Jaime Montoya

Reputation: 7701

The following code is in my app/src/main/AndroidManifest.xml file:

<!-- [START firebase_service] -->
<service
    android:name="com.[myapp].MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
<!-- [END firebase_service] -->

The text MyFirebaseMessagingService that appears in red in the image below should appear in green:

enter image description here

The red color means that there is something wrong:

enter image description here

In order to fix it, I created the file app/src/main/java/com/[myapp]\MyFirebaseMessagingService.java with the following content:

package com.[myapp];
import com.google.firebase.messaging.FirebaseMessagingService;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
}

Now in Android Studio, the text that was red changed to green:

enter image description here

Upvotes: 0

Mohammed Farhan
Mohammed Farhan

Reputation: 1138

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

Upvotes: 7

Vinayak B
Vinayak B

Reputation: 4510

You are closing your </application> tag before </service> tag . try below code

    <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" />

            <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" />

    <service
        android:name="com.hajora.dndcommerce.MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    <service
        android:name="com.hajora.dndcommerce.MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
</application>

Upvotes: 0

Related Questions