找不到类“ com.google.firebase.iid.FirebaseInstanceIdReceiver”

时间:2019-09-12 09:47:34

标签: java android firebase-cloud-messaging

我想通过Firebase发送通知。当我发送通知时,我的应用程序崩溃,从而在logcat中显示这种类型的堆栈跟踪:

 java.lang.RuntimeException: Unable to instantiate receiver com.google.firebase.iid.FirebaseInstanceIdReceiver: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.iid.FirebaseInstanceIdReceiver" on path: DexPathList[[zip file "/system/framework/com.google.android.maps.jar", zip file "/data/app/com.example.e_agriculture10-1.apk", zip file "/data/data/com.example.e_agriculture10/code_cache/secondary-dexes/com.example.e_agriculture10-1.apk.classes2.zip"],nativeLibraryDirectories=[/data/app-lib/com.example.e_agriculture10-1, /system/lib]]
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2398)
    at android.app.ActivityThread.access$1700(ActivityThread.java:135)

  Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.iid.FirebaseInstanceIdReceiver" on path: DexPathList[[zip file "/system/framework/com.google.android.maps.jar", zip file "/data/app/com.example.e_agriculture10-1.apk", zip file "/data/data/com.example.e_agriculture10/code_cache/secondary-dexes/com.example.e_agriculture10-1.apk.classes2.zip"],nativeLibraryDirectories=[/data/app-lib/com.example.e_agriculture10-1, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)

这是我的FirebaseMessagingService.java类

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public  static int NOTIFICATION_ID = 1;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    generateNotification(remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTitle());

}

private void generateNotification(String body, String title) {
    Intent intent = new Intent(this,NotificationFragment.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.agriculture_home )
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent);


    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    if (NOTIFICATION_ID > 1073741824){
        NOTIFICATION_ID = 0;
    }
    notificationManager.notify(NOTIFICATION_ID++,notificationBuilder.build());


}
}

这是我的GettingDeviceTokenService.java文件:

public class GettingDeviceTokenService extends  FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
    String DeviceToken = FirebaseInstanceId.getInstance().getToken();
    Log.d("DEVICE TOKEN",DeviceToken);

}
}

这是我的build.gradle(模块应用程序)

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
    applicationId "com.example.e_agriculture10"
    minSdkVersion 16
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'com.google.firebase:firebase-database:16.0.4'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.0.2'


implementation 'com.google.android.material:material:1.1.0-alpha9'

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.android.support:cardview-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation files('libs/YouTubeAndroidPlayerApi.jar')
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.firebase:firebase-client-android:2.5.2'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha09'

}

我如何摆脱这个错误,请任何人指导我。谢谢。

2 个答案:

答案 0 :(得分:1)

您可以删除此类“ GettingDeviceTokenService”,并覆盖“ MyFirebaseMessagingService”类中的“ onNewToken”方法。

public void onNewToken(@NonNull String your_new_token) {super.onNewToken(s);}

随心所欲地使用新令牌

答案 1 :(得分:0)

除了unownsp的答案,如果您使用proguard,这可能会有所帮助;将它们添加到您的proguard配置文件中:

-dontwarn com.google.android.gms.**
-keep class com.google.android.gms.** { *; }
-keep class com.google.firebase.** { *; }