无法从FCM接收通知

时间:2017-06-26 12:51:24

标签: android firebase firebase-cloud-messaging

我正在尝试构建一个可以从FCM(Firebase云消息系统)接收通知的简单应用。我能够生成令牌并在文本视图中显示它。另外,我已经使用“FirebaseMessegingService”类中的log.d()编写了在LOG上打印通知消息的逻辑,该类扩展自“FirebaseMessagingService”。但是,一旦我从FCM控制台发送消息,应用程序就会崩溃,我在Android监视器上收到以下错误消息。

Android监视器

06-26 18:09:02.447 3798-3798/com.example.sumeet.firebasedemoyout E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.example.sumeet.firebasedemoyout, PID: 3798
                                                                                   java.lang.RuntimeException: Unable to instantiate service com.example.sumeet.firebasedemoyout.MyFirebaseMessagingService: java.lang.ClassNotFoundException: Didn't find class "com.example.sumeet.firebasedemoyout.MyFirebaseMessagingService" on path: DexPathList[[zip file "/data/app/com.example.sumeet.firebasedemoyout-1/base.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.sumeet.firebasedemoyout-1/lib/x86_64, /system/lib64, /vendor/lib64]]
                                                                                       at android.app.ActivityThread.handleCreateService(ActivityThread.java:3176)
                                                                                       at android.app.ActivityThread.-wrap5(ActivityThread.java)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.sumeet.firebasedemoyout.MyFirebaseMessagingService" on path: DexPathList[[zip file "/data/app/com.example.sumeet.firebasedemoyout-1/base.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.sumeet.firebasedemoyout-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.sumeet.firebasedemoyout-1/lib/x86_64, /system/lib64, /vendor/lib64]]
                                                                                       at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                                       at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                                       at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                                       at android.app.ActivityThread.handleCreateService(ActivityThread.java:3173)
                                                                                       at android.app.ActivityThread.-wrap5(ActivityThread.java) 
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                       at android.os.Looper.loop(Looper.java:154) 
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 


                                                                                   --------- beginning of system

你能否在我错误的逻辑上纠正我?发布相关代码下方。

MainActivity.java

package com.example.sumeet.firebasedemoyout;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    private BroadcastReceiver broadcastReceiver;

    //Token fu_TOJsifrA:APA91bEGAnpWMp5HBaWVSrwYXvyiqSf1EcgMCH4D_SFEheYUJ0hA46qOF_FoKjy7tkMfMzoacOlR6sKqHpNritx-jRb--VZK_Nn7rhYO6ZQlmGpgMP3OoQIRyFjGl81XlesHM2y__k1Q
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.token_tv);

        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Toast.makeText(getApplicationContext(), "SHOWME", Toast.LENGTH_SHORT).show();
                textView.setText(SharedPrefManager.getInstanceID(MainActivity.this).getToken());
            }
        };

        if (SharedPrefManager.getInstanceID(this).getToken() != null) {
            textView.setText(SharedPrefManager.getInstanceID(MainActivity.this).getToken());
            Log.d("MYSHAREDPREFTOKEN", SharedPrefManager.getInstanceID(this).getToken());
        }


        registerReceiver(broadcastReceiver, new IntentFilter(FireBaseTokenService.Token_broadcast));
    }
}

FirebaseMessegingService.java

package com.example.sumeet.firebasedemoyout;

import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

/**
 * Created by Sumeet on 25-06-2017.
 */

public class FirebaseMessegingService extends FirebaseMessagingService {
    private static final String TAG = "MyTAG";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

    }
}

SharedPrefManager.java

package com.example.sumeet.firebasedemoyout;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Sumeet on 25-06-2017.
 */

public class SharedPrefManager {
    private static Context mContext;
    private static SharedPrefManager mInstance;
    private static final String SharedPref_name = "FCMSharedPref";
    private static final String SharedPref_keyAccessToken = "FCMSharedPref_Key";


    public SharedPrefManager(Context context) {
        mContext = context;
    }

    public static synchronized SharedPrefManager getInstanceID(Context context) {
        if (mInstance == null) {
            mInstance = new SharedPrefManager(context);
        }
        return mInstance;

    }

    public boolean storeToken(String token) {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(SharedPref_name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(SharedPref_keyAccessToken, token);
        editor.apply();
        return true;

    }

    public String getToken() {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(SharedPref_name, Context.MODE_PRIVATE);
        return sharedPreferences.getString(SharedPref_keyAccessToken, null);

    }
}

FireBaseTokenService.java

package com.example.sumeet.firebasedemoyout;

import android.content.Intent;
import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

import static com.google.android.gms.internal.zzs.TAG;

/**
 * Created by Sumeet on 25-06-2017.
 */

public class FireBaseTokenService extends FirebaseInstanceIdService {

    public static final String Token_broadcast = "FCMToken_Broadcast";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);


        getApplicationContext().sendBroadcast(new Intent(Token_broadcast));
        storeToken(refreshedToken);
    }

    private void storeToken(String refreshedToken) {

        SharedPrefManager.getInstanceID(getApplicationContext()).storeToken(refreshedToken);
    }

}

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sumeet.firebasedemoyout">

    <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">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

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

</manifest>

的build.gradle(模块:应用)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.sumeet.firebasedemoyout"
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.firebase:firebase-messaging:10.0.1'
    testCompile 'junit:junit:4.12'
}


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

1 个答案:

答案 0 :(得分:0)

您似乎在宣布名为MyFirebaseMessagingService的服务 从你的清单。但是,您提供的课程称为FirebaseMessegingService.java

或许在清单中匹配正确的班级名称可以防止您崩溃。

干杯