我正在尝试构建一个可以从FCM(Firebase Cloud Messaging System(接收通知的简单应用程序。我能够生成令牌并将其显示在文本视图中。另外,我已经编写了逻辑,以使用log.d((在" firebasemessegingservice"中的log.d((上打印通知消息,该逻辑从" firebasemessagingservice"延伸。但是,一旦我从FCM控制台发送消息后,应用程序就会崩溃,并且在Android Monitor上收到以下错误消息。
Android Monitor
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清单
<?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(模块:app(
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'
看来您正在宣布一项名为 MyFirebaseMessagingService
的服务从你的清单。但是,您提供的类称为FirebaseMessegingService.java
也许在清单中与正确的班级名称匹配会保护您免于崩溃。
欢呼