我正在尝试构建一个简单的应用程序,可以从FCM(Firebase云消息传递系统)接收通知。我能够生成令牌并将其显示在文本视图中。另外,我还编写了在“FirebaseMessagingService”类“中使用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清单
<?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'
似乎您正在从清单中声明一个名为<code>MyFirebaseMessagingService</code>的服务。但是,您提供的类名为FirebaseMessegingService.java
也许在您的清单中匹配正确的类名可以防止您崩溃。
干杯
我正在处理FCM通知。当应用程序在后台时,我的通知工作正常,但当应用程序在前台时,我的通知无法接收。我几乎什么都试过了,但都不奏效。当应用程序处于前台时,不会收到通知。 manifest.xml: 代码(FirebaseMessagingService):
消息“成功订阅到广播频道。”在应用程序启动时打印。但是当我使用parse.com上的“发送推送通知”功能时,我的手机没有收到任何推送通知。 为什么不起作用?
当应用程序关闭时,我无法使通知工作。 我从FCM/GCM(python服务器)发送此消息: 所以curl是这样的: curl-头"授权:key=MYKEY"-头"内容类型:应用/json"https://fcm.googleapis.com/fcm/send-d'{"registration_ids":["APA91bEDB9dVf-..."],"collapse_key":空,"content_
在清单文件中:
我看过这个问题。我有完全相同的问题,但我有来自Thawte的有效ssl证书。我只使用id、type和address属性进行了观察请求,如这里所述。我的address属性类似于 我甚至没有收到同步消息。会出什么问题?
我知道我可以设置click_action,但这只是为了定制哪些活动将在通知点击时启动,我需要一些将在通知收到时自动启动的东西。 我尝试了quick start messaging firebase示例,其中有一个onMessageReceived()方法,但它仅在应用程序处于前台时才起作用。是否有一些东西将执行同时应用程序在后台?GCM可以通过直接从接收到通知时调用的广播接收器启动活动意图来完成我