贵有恒,何必三更起、五更眠、最无益,只怕一日曝、十日寒。这篇文章主要讲述接收新的FCM消息(前台和后台)时Android应用程序崩溃相关的知识,希望能为你提供帮助。
我正在努力在我的应用中实施FCM。我正在按照文档中的指南进行操作,并尝试按照文档中的建议实现一个简单的测试,但每当我从控制台发送通知时,应用程序崩溃,天气就在前台或后台。
**更新:我已经删除了服务类,它在gradle中的减速和实现。如果我尝试测试消息,应用程序仍会崩溃。它不应该完全不受它的影响吗?
这是我的扩展课程:
import android.util.Log
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessageclass MyFirebaseMessagingService : FirebaseMessagingService() {override fun onMessageReceived(p0: RemoteMessage?) {
super.onMessageReceived(p0)Log.d("newMessage", p0!!.notification!!.body)
}override fun onNewToken(p0: String?) {
super.onNewToken(p0)
FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener { idResult ->
val uid = FirebaseAuth.getInstance().uid ?: ""val token = idResult.tokenval userRef = FirebaseDatabase.getInstance().getReference("/users/$uid/services/firebase-token")
userRef.setValue(token)
}
}
}
这是我的表现:
<
service
android:name=".otherClasses.MyFirebaseMessagingService"
android:exported="false">
<
intent-filter>
<
action android:name="com.google.firebase.MESSAGING_EVENT" />
<
/intent-filter>
<
/service>
这是崩溃日志:
2019-04-18 11:45:44.007 21257-21295/co.getdere E/AndroidRuntime: FATAL EXCEPTION: Firebase-WrappedFirebaseMessagingService
Process: co.getdere, PID: 21257
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
at com.google.firebase.messaging.zzb.<
init>
(Unknown Source:5)
at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source:58)
at com.google.firebase.iid.zzb.run(Unknown Source:2)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source:6)
at java.lang.Thread.run(Thread.java:764)
答案试试吧。它可能会奏效
class MyFirebaseMessagingService : FirebaseMessagingService() {
companion object {private val TAG = "MyFirebaseMsgService"
}override fun onNewToken(s: String?) {
super.onNewToken(s)
//Displaying token on logcat
Log.e(TAG, "Refreshed token: " + s!!)
}override fun onMessageReceived(remoteMessage: RemoteMessage?) {
Log.e(TAG, "onMessageReceived")
}
}
【接收新的FCM消息(前台和后台)时Android应用程序崩溃】将其添加到清单文件中
<
service
android:name=".fcm.MyFirebaseMessagingService"
android:exported="false">
<
intent-filter>
<
action android:name="com.google.firebase.MESSAGING_EVENT" />
<
/intent-filter>
<
/service>
推荐阅读
- 当用户在Android中使用XMPP离线时向用户发送通知
- 我无法从Android GCM获取注册ID
- 推送通知是否在设置中被禁用后传递到Android手机()
- 将应用程序部署到Google App Engine时出现超时错误
- com.google.firebase.messaging.zza(找不到引用的类android.graphics.drawable.AdaptiveIconDrawable)
- Xamarin.Forms Android推送通知不会出现
- 如何将动态创建的.xml文件与主AndroidManifest.xml合并
- android是否会为了释放内存而杀死单身()
- Android - Singleton Class仅在第二次刷新时更新