简体   繁体   中英

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

I am working on implementing FCM in my app. 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. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM