未解决的课程'MyFirebaseMessagingService'Android Studio

时间:2017-09-25 08:11:23

标签: java android firebase firebase-cloud-messaging

Android Studio中的Firebase存在问题。 这是发生的事情: 在调试mod中,一切正常,并遵循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>

当我切换到发布模式时,我收到错误:

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

所以,然后我将两个服务放在标签内,然后我得到错误:

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

验证Android XML文件中的资源引用。

我无法确定问题是什么

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'

6 个答案:

答案 0 :(得分:4)

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

为此库名称com.google.firebase.messaging.FirebaseMessagingService提供完整路径即可解决此问题。

答案 1 :(得分:3)

首先查看此图片 Image with error

检查Firebase类是否在正确的包文件夹中。例如,如果firebase类在包&#39;服务&#39;然后在清单服务中将android:name=".services.FirebaseMessagingService"然后Unresolved class 'MyFirebaseMessagingService'错误将得到解决。

enter image description here

答案 2 :(得分:0)

您正在</application>代码前关闭</service>代码。尝试以下代码

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

答案 3 :(得分:0)

以下代码位于我的app/src/main/AndroidManifest.xml文件中:

<!-- [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] -->

下图中以红色显示的文字MyFirebaseMessagingService应显示为绿色:

enter image description here

红色表示出现了问题:

enter image description here

为了解决这个问题,我使用以下内容创建了文件app/src/main/java/com/[myapp]\MyFirebaseMessagingService.java

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

现在在Android Studio中,红色的文字变为绿色:

enter image description here

答案 4 :(得分:0)

穆罕默德·法尔汉解决了我的问题。以下是详细的解决方案。

以下是解决方案:

首先,在清单文件中添加以下两个语句:

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

我们添加MyFirebaseInstanceIDService类的原因是为了处理注册令牌的创建和更新。

现在,由于错误是“未解析的类”,请在根包中创建两个类(即在清单文件顶部提到)并在两个文件中粘贴代码:

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

然后你们都准备好了。 Lemme知道你是否有任何障碍。

参考链接: https://firebase.google.com/docs/cloud-messaging/android/client

答案 5 :(得分:0)

尝试更改

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

作者

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