简体   繁体   English

收到新的FCM消息(前景和背景)时,Android应用程序崩溃

[英]Android app crashes when receiving a new FCM message (foreground and background)

I am working on implementing FCM in my app. 我正在我的应用程序中实施FCM。 I'm following the guide on the documentation and trying to implement a simple test as suggested in the documentation, but whenever I send a notification from the console the app crashes, weather it is in the foreground or background. 我正在按照文档中的指南进行操作,并尝试按照文档中的建议实施简单的测试,但是每当我从控制台发送通知时,应用都会崩溃,无论它处于前台还是后台。

**UPDATE: I've removed the service class, its deceleration in gradle and the implementation. **更新:我已经删除了服务类,gradle中的减速度和实现。 The app still crashes if I try to test message. 如果我尝试测试消息,该应用程序仍然崩溃。 Shouldn't it be unaffected by it completely? 它不应该完全不受它影响吗?

This is my extended class: 这是我的延伸班级:

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.RemoteMessage

class 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.token

            val userRef = FirebaseDatabase.getInstance().getReference("/users/$uid/services/firebase-token")
            userRef.setValue(token)
        }
    }
}

This is my manifest: 这是我的清单:

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

This is the crash Log: 这是崩溃日志:

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)

Just try like this. 像这样尝试。 It may work 可能有效

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

add it in manifest file 将其添加到清单文件中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在前台在后台收到FCM通知时应用崩溃 - App Crashes when receiving FCM Notification on Foreground works on Background 收到新短信时,SMS接收应用程序崩溃-Android - SMS Receiving app crashes when new text message comes in - Android Fcm:当应用程序在后台和前台时向 Android 和 IOS 发送 DataMessage - Fcm: Send DataMessage to Android and IOS when app is in background and in foreground React Native Android App 在收到 FCM 通知时崩溃 - React Native Android App crashes when receiving FCM notification Android FCM:当应用程序在后台时 FCM 显示(通知)消息登录 - Android FCM: FCM display(notification) message loggin when app is in background Android FCM在从后台删除应用时未收到通知 - Android FCM not receiving notifications when app is removed from background 三星 S7 在接收高优先级 FCM 消息时从后台启动 Intent 时崩溃 - Samsung S7 crashes when launching an Intent from the background while receiving a high priority FCM message 应用程序在收到消息时崩溃 - App crashes when receiving of message 使用 react-native-push-notification,App 在收到新的 FCM 通知时崩溃 - Using react-native-push-notification, App crashes when receiving new FCM notification 当应用程序处于前台或后台时如何使用 FCM 处理通知 - How to handle notifications with FCM when app is in either foreground or background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM