Android Firebase云消息服务无法实例化服务FCM.MessagingService

Android Firebase云消息服务无法实例化服务FCM.MessagingService,android,firebase-cloud-messaging,Android,Firebase Cloud Messaging,我已经通过以下官方文档将Firebase云消息服务和推送通知服务包含在我的应用程序中。但是,当我收到消息时,我的应用程序崩溃 但是,我无法从服务器接收推送通知。错误日志如下所示 错误消息: Unable to instantiate service com.abc.myapp.MyFirebaseMessagingService: java.lang.ClassNotFoundException: Didn't find class "com.abc.myapp.MyFirebaseMessag

我已经通过以下官方文档将Firebase云消息服务和推送通知服务包含在我的应用程序中。但是,当我收到消息时,我的应用程序崩溃

但是,我无法从服务器接收推送通知。错误日志如下所示

错误消息:

Unable to instantiate service com.abc.myapp.MyFirebaseMessagingService: java.lang.ClassNotFoundException: Didn't find class "com.abc.myapp.MyFirebaseMessagingService" on path: DexPathList[[zip file "/data/app/com.abc.myapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.abc.myapp-2/lib/arm64, /system/lib64, /vendor/lib64]]
    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abc.myapp">

    <!-- adding internet permission -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.wifi" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">
        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <activity android:name=".QRScanner" />
        <activity android:name=".ApproveLeaveActivity" />
        <activity android:name=".AddLeaveActivity" />
        <activity android:name=".LeaveActivity" />
        <activity android:name=".CustInParking" />
        <activity android:name=".SalaryActivity" />
        <activity android:name=".AttendanceActivityDataHandler" />
        <activity android:name=".AttendanceActivity" />
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login"
            android:windowSoftInputMode="stateHidden" />
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_stat_name" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
        <meta-data
            android:name="firebase_messaging_auto_init_enabled"
            android:value="ture" />
        <meta-data
            android:name="firebase_analytics_collection_enabled"
            android:value="true" />

    </application>

</manifest>
    package com.abc.myapp;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import static android.content.ContentValues.TAG;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //showNotification(remoteMessage.getNotification().getBody());
        Log.e(TAG, "From: " + remoteMessage.getFrom());
        Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

    }
    @Override
    public void onNewToken(String token) {
        Log.d(TAG, "Refreshed token: " + token);

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.
        sendRegistrationToServer(token);
    }

    private void sendRegistrationToServer(String token) {
        Log.d("TOKEN ", token);
    }
    public void showNotification(String message){
        PendingIntent pi = PendingIntent.getActivity(this,0, new Intent(this, MainActivity.class),0);
        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setContentTitle("Suzuki Islamabad Motors")
                .setContentText(message)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(pi)
                .setAutoCancel(true)
                .build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0,notification);
    }
}
apply plugin: 'com.google.gms.google-services'

implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation 'com.google.firebase:firebase-messaging:20.1.0'
清单:

Unable to instantiate service com.abc.myapp.MyFirebaseMessagingService: java.lang.ClassNotFoundException: Didn't find class "com.abc.myapp.MyFirebaseMessagingService" on path: DexPathList[[zip file "/data/app/com.abc.myapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.abc.myapp-2/lib/arm64, /system/lib64, /vendor/lib64]]
    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abc.myapp">

    <!-- adding internet permission -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.wifi" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">
        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <activity android:name=".QRScanner" />
        <activity android:name=".ApproveLeaveActivity" />
        <activity android:name=".AddLeaveActivity" />
        <activity android:name=".LeaveActivity" />
        <activity android:name=".CustInParking" />
        <activity android:name=".SalaryActivity" />
        <activity android:name=".AttendanceActivityDataHandler" />
        <activity android:name=".AttendanceActivity" />
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login"
            android:windowSoftInputMode="stateHidden" />
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_stat_name" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
        <meta-data
            android:name="firebase_messaging_auto_init_enabled"
            android:value="ture" />
        <meta-data
            android:name="firebase_analytics_collection_enabled"
            android:value="true" />

    </application>

</manifest>
    package com.abc.myapp;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import static android.content.ContentValues.TAG;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //showNotification(remoteMessage.getNotification().getBody());
        Log.e(TAG, "From: " + remoteMessage.getFrom());
        Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

    }
    @Override
    public void onNewToken(String token) {
        Log.d(TAG, "Refreshed token: " + token);

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.
        sendRegistrationToServer(token);
    }

    private void sendRegistrationToServer(String token) {
        Log.d("TOKEN ", token);
    }
    public void showNotification(String message){
        PendingIntent pi = PendingIntent.getActivity(this,0, new Intent(this, MainActivity.class),0);
        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setContentTitle("Suzuki Islamabad Motors")
                .setContentText(message)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(pi)
                .setAutoCancel(true)
                .build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0,notification);
    }
}
apply plugin: 'com.google.gms.google-services'

implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation 'com.google.firebase:firebase-messaging:20.1.0'
梯度依赖性:

Unable to instantiate service com.abc.myapp.MyFirebaseMessagingService: java.lang.ClassNotFoundException: Didn't find class "com.abc.myapp.MyFirebaseMessagingService" on path: DexPathList[[zip file "/data/app/com.abc.myapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.abc.myapp-2/lib/arm64, /system/lib64, /vendor/lib64]]
    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abc.myapp">

    <!-- adding internet permission -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.wifi" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">
        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <activity android:name=".QRScanner" />
        <activity android:name=".ApproveLeaveActivity" />
        <activity android:name=".AddLeaveActivity" />
        <activity android:name=".LeaveActivity" />
        <activity android:name=".CustInParking" />
        <activity android:name=".SalaryActivity" />
        <activity android:name=".AttendanceActivityDataHandler" />
        <activity android:name=".AttendanceActivity" />
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login"
            android:windowSoftInputMode="stateHidden" />
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_stat_name" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
        <meta-data
            android:name="firebase_messaging_auto_init_enabled"
            android:value="ture" />
        <meta-data
            android:name="firebase_analytics_collection_enabled"
            android:value="true" />

    </application>

</manifest>
    package com.abc.myapp;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import static android.content.ContentValues.TAG;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //showNotification(remoteMessage.getNotification().getBody());
        Log.e(TAG, "From: " + remoteMessage.getFrom());
        Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

    }
    @Override
    public void onNewToken(String token) {
        Log.d(TAG, "Refreshed token: " + token);

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.
        sendRegistrationToServer(token);
    }

    private void sendRegistrationToServer(String token) {
        Log.d("TOKEN ", token);
    }
    public void showNotification(String message){
        PendingIntent pi = PendingIntent.getActivity(this,0, new Intent(this, MainActivity.class),0);
        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setContentTitle("Suzuki Islamabad Motors")
                .setContentText(message)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(pi)
                .setAutoCancel(true)
                .build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0,notification);
    }
}
apply plugin: 'com.google.gms.google-services'

implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation 'com.google.firebase:firebase-messaging:20.1.0'

MyFirebaseMessagingService的包名是什么?它与在清单中声明服务的错误有关。尝试使用确切的包名重写。package com.abc.myapp;这是MyFirebaseMessagingService的包名,它是正确的。我想您可以用包的详细信息更新MyFirebaseMessagingService的代码,并添加
AndroidManifest.xml
?@Md.Asaduzzaman代码更新吗