Fcm集成获取异常 [英] Fcm integration getting the exception

查看:329
本文介绍了Fcm集成获取异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在处理 FCM 集成,但获取 java.lang.ClassNotFoundException:找不到类service.MyFirebaseMessagingService的路径:DexPathList [[zip文件/data/app/com.rk.servicepractise-2/base.apk\"],nativeLibraryDirectories=[/vendor/lib64,/ system / lib64]]

我已经搜索过它,但是还没有得到正确的解决方案,请检查下面的代码,让我知道我在哪里做错了?



请检查我的清单文件 gradle ,请帮我简单解释一下这个问题。

我的第一个类是 MyFirebaseInstanceIDService 扩展了 FirebaseInstanceIdService


  public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG =MyFirebaseIIDService;

@Override
public void onTokenRefresh(){

//获取注册令牌
String refreshedToken = FirebaseInstanceId.getInstance()。getToken();

//在logcat上显示令牌
Log.d(TAG,刷新令牌:+ refreshedToken);

}

private void sendRegistrationToServer(String token){
//您可以实现此方法以将令牌存储在服务器上
// Not目前需要的项目


code
$ b

另外还有一个类为 Fcm 扩展 FirebaseMessagingService 类,如下所示: -

  public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG =MyFirebaseMsgService;

@Override
public void onMessageReceived(RemoteMessage remoteMessage){
//在日志
中显示数据//可选
Log.d(TAG ,From:+ remoteMessage.getFrom());
Log.d(TAG,Notification Message Body:+ remoteMessage.getNotification()。getBody());

//调用方法来生成通知
sendNotification(remoteMessage.getNotification()。getBody());
}

//此方法仅生成推送通知
//它与我们在之前文章中所做的相同
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推送通知)
.setContentText(messageBody )
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

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

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




$ b $ p
$ b

下面是我的Manifest文件条目, code> Fcm

 < application> 





< service
android:name =com.rk.servicepractise.service.MyFirebaseMessagingService>
< intent-filter>
< / intent-filter>
< / service>

< service
android:name =com.rk.servicepractise.service.MyFirebaseInstanceIDService>
< intent-filter>
< / intent-filter>
< / service>

< / application>

以及我正在使用的 gradle 它如下:app : -

  apply plugin:'com.android。应用程序'

android {








依赖关系{
编译fileTree(包括:''* .jar'],dir:'libs')
testCompile'junit: junit:4.12'
compile'c​​om.android.support:appcompat-v7:23.1.1'
compile'c​​om.android.support:design:23.1.1'
compile'c​​om。 google.firebase:firebase-messaging:9.8.0'

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


build.gradle

Project:ProjectName

  buildscript {
储存库{
jcenter()
}
依赖项{
classpath'com.android.tools.build:gradle:1.5.0'
classpath'com.google.gms:google-services:3.0.0'
//注意:不要放置您的应用程序依赖项这里;它们属于单个模块build.gradle文件
//

$ b code


以下是我得到的 Exception

  java。 lang.RuntimeException:无法实例化服务service.MyFirebaseInstanceIDService:java.lang.ClassNotFoundException:在路径中找不到类service.MyFirebaseInstanceIDService:DexPathList [[zip file] /data/app/com.rk.servicepractise-1/在android.app.ActivityThread.handleCreateService(ActivityThread.java:2779)
at android.app.ActivityThread。
在android.app.ActivityThread。
在android.app.ActivityThread.handleCreateService(ActivityThread.java:2779)
在android.app.ActivityThread。访问$ 1900(ActivityThread.java:154)
在android.app.ActivityThread $ H.handl eMessage(ActivityThread.java:1404)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
在android.app.ActivityThread.main(ActivityThread.java:5292)$ b $在java.lang.reflect.Method.invoke(本地方法)$ b $在java.lang.reflect.Method.invoke(方法。 java:372)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 699)
导致:java.lang.ClassNotFoundException:没有找到路径上的类service.MyFirebaseInstanceIDService:DexPathList [[zip文件/data/app/com.rk.servicepractise-1/base.apk],nativeLibraryDirectories = [ / vendor / lib64,/ system / lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)$ b $ 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)$ b $ 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 $ b在java.lang.Class.classForName(Native方法)$ b在java中的
。 lang.BootClassLoader.findClass(ClassLoader.java:781)$ b $在java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
在java.lang.ClassLoader.loadClass(ClassLoader.java:504)

EDITED 只要你陷入同样的​​问题, @Marcin Orlowski ... 链接: - 点击此处

解决方案

Mainfest Service 之前加上点( $ c>文件。

换句话说,下面的技巧就是:

 << ; service android:name =。service.MyFirebaseMessagingService> 



I am working on the FCM integration but getting the 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]]

I have searched it about but did not get the proper solution yet,Please check my code below and let me know where am i doing wrong?

Please check my classes and manifest file with gradle, Please help me to short out from this problem.
My first class that is the MyFirebaseInstanceIDService which extends FirebaseInstanceIdService class

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

And one more class for the Fcm that extends FirebaseMessagingService class which are as follow:-

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());
    }
}

And here below is my Manifest file entry for this 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>

And the gradle which i am using for it as follow Module: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 foe the 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
    }
}

And the below is the Exception which i am getting:-

                                                           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)

EDITED Please follow this link whenever u become stuck in the same problem thanks @Marcin Orlowski ...Link:-Click here

解决方案

Just add dot (.) before Service in your Mainfest file.

In other words, the following does the trick:

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

这篇关于Fcm集成获取异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆