获得异常的Fcm集成

时间:2016-11-12 09:08:00

标签: android google-cloud-messaging firebase-cloud-messaging


我正在进行FCM整合,但获得了java.lang.ClassNotFoundException: Didn't find class "service.MyFirebaseMessagingService" on path: DexPathList[[zip file "/data/app/com.rk.servicepractise-2/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]

我已搜索过但未找到合适的解决方案,请检查下面的代码,让我知道我在哪里做错了?

请使用classes检查我的manifestgradle文件,请帮我解决此问题。
我的第一个课程是MyFirebaseInstanceIDService,它扩展了 FirebaseInstanceIdService

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    @Override
    public void onTokenRefresh() {

        //Getting registration token
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        //Displaying token on logcat
        Log.d(TAG, "Refreshed token: " + refreshedToken);

    }

    private void sendRegistrationToServer(String token) {
        //You can implement this method to store the token on your server
        //Not required for current project
    }
}

另外还有一个Fcm课程,其中FirebaseMessagingService课程如下: -

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //Displaying data in log
        //It is optional
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        //Calling method to generate notification
        sendNotification(remoteMessage.getNotification().getBody());
    }

    //This method is only generating push notification
    //It is same as we did in earlier posts
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Firebase Push Notification")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());
    }
}

以下是此Fcm

的清单文件条目
<application>
.
.
.
.

  <service
        android:name="com.rk.servicepractise.service.MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

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

    </application>

我正在使用的gradle如下模块:app : -

apply plugin: 'com.android.application'

android {
.
.
.
.
.

}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.google.firebase:firebase-messaging:9.8.0'

}
apply plugin: 'com.google.gms.google-services'

build.gradle 对抗Project:ProjectName

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'com.google.gms:google-services:3.0.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

以下是我得到的例外: -

                                                           java.lang.RuntimeException: Unable to instantiate service service.MyFirebaseInstanceIDService: java.lang.ClassNotFoundException: Didn't find class "service.MyFirebaseInstanceIDService" on path: DexPathList[[zip file "/data/app/com.rk.servicepractise-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
                                                                    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2779)
                                                                    at android.app.ActivityThread.access$1900(ActivityThread.java:154)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1404)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
                                                                 Caused by: java.lang.ClassNotFoundException: Didn't find class "service.MyFirebaseInstanceIDService" on path: DexPathList[[zip file "/data/app/com.rk.servicepractise-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
                                                                    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2776)
                                                                    at android.app.ActivityThread.access$1900(ActivityThread.java:154) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1404) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                    at android.os.Looper.loop(Looper.java:135) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
                                                                    Suppressed: java.lang.ClassNotFoundException: service.MyFirebaseInstanceIDService
                                                                    at java.lang.Class.classForName(Native Method)
                                                                    at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                                    at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                                    at java.lang.ClassLoader.loadClass(ClassLoader.java:504)

已编辑如果您遇到同样的问题,请按照此链接感谢 @Marcin Orlowski ... 链接: - {{3 }}

4 个答案:

答案 0 :(得分:29)

只需在Service文件中的Mainfest之前添加点()即可。

换句话说,以下是诀窍:

<service android:name=".service.MyFirebaseMessagingService">

答案 1 :(得分:1)

就我而言,我不得不更换

<service android:name="MyFirebaseMessagingService" android:exported="false">

<service android:name="com.google.firebase.messaging.FirebaseMessagingService">

答案 2 :(得分:1)

我遇到了同样的错误,并通过执行 Clean 然后 Rebuild 来修复它。我认为该文件在重新索引项目之前没有被正确引用。

答案 3 :(得分:0)

为了它的价值,我已经通过将我的FirebaseMessagingService侦听器类移动到根包文件夹中来解决了这个问题。我使用的是android:name =&#34; .fcm.FCMListenerService&#34;,每当设备收到noti时,它就像上面所描述的那样一直崩溃。将它移动到它开始工作的根包文件夹,即android:name =&#34; .FCMListenerService&#34;。

更新:我将服务移回fcm文件夹并继续工作,所以这不是答案。然而,它确实做了一些事情来将事情付诸实践,所以它可能值得一试(我正在解决这个问题几个小时)。