Firebase Messaging Service null reference exception

braderbrader USMember ✭✭

I'm receiving the beloved NullReferenceException frequently on my FirebaseMessagingService class in the OnMessageReceived override method. I do not call the base class method that throws the exception, so it must be occurring during some magical call to underlying Java classes. Push notifications work in all the tests that I've done and I can't reproduce this exception while debugging or in any useful state to help me diagnose the issue.

MyApp.Droid.Firebase.MyFirebaseMessagingService.OnMessageReceived(Firebase.Messaging.RemoteMessage remoteMessage) in <c0bc1f0c58fe430c9ecacc4761092597>:0
Firebase.Messaging.FirebaseMessagingService.n_OnMessageReceived_Lcom_google_firebase_messaging_RemoteMessage_(System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_message) in <fa3e84c8a5294fd4a845ead3da9e647b>:0

Answers

  • braderbrader USMember ✭✭

    Here is the decompiled method that throws the exception. Based on the stack trace, it seems like it would have to be getting back null from Java.Lang.GetObject<FirebaseMessagingService>(jnienv, native__this, JniHandleOwnership.DoNoTransfer) so that calling OnMessageReceived causes the exception. Does that mean MyFirebaseMessagingService is not registered properly? I have [Service] and [IntentFilter(new [] { "com.google.firebase.MESSAGING_EVENT" })] attributes per documentation, as well as proper manifest configurations.

    private static void n_OnMessageReceived_Lcom_google_firebase_messaging_RemoteMessage_(IntPtr jnienv, IntPtr native__this, IntPtr native_message)
        {
          Java.Lang.Object.GetObject<FirebaseMessagingService>(jnienv, native__this, JniHandleOwnership.DoNotTransfer).OnMessageReceived(Java.Lang.Object.GetObject<RemoteMessage>(native_message, JniHandleOwnership.DoNotTransfer));
        }
    
  • jezhjezh Member, Xamarin Team Xamurai

    Please add a diagnostic build output about this issue. (As a file attachment or gist link), that would be really appreciated!

  • braderbrader USMember ✭✭

    @jezh Sorry, this has been several months. I'd appreciate if you took a look at this issue. I just completed a build in Release mode with diagnostic build output. The output is 22MB, which Gist didn't like :)

    Link: https://drive.google.com/file/d/14NoTpG3H3eL1sLJRTEx78NBsUWkd_AzK/view?usp=sharing

    As for why I didn't post originally... I realized I was reading the stack trace backwards and added some null checks that I thought would fix the issue. Now that I've released a fix, I found that the issue is still occurring. I don't believe there is any possibility for null within my method now, but I am still getting the NullReferenceException on my app (I haven't been able to reproduce for myself however). I check that the RemoteMessage parameter is not null, but after that all my method calls are static, objects are non-null or have null checking. I can't find anywhere in my OnMessageReceived override or any methods it calls that could potentially throw a NullReferenceException (and those methods should show up in the stack trace anyway).

Sign In or Register to comment.