1

Hello all, I'm receiving the exception when I received a remote notification from FCM in my cross-platform Xamarin.Forms app in Android Platform

Its not duplicate because i'm recieving this error on push notification i guess it's not related to the code.

Also, the OnRecieved Method called successfully and also created a notification I can see the notification in status bar after the complete call of OnReceived Method it gets crash with this excpetion.

    Thread started: <Thread Pool> #11
    06-04 19:32:45.768 D/Mono    (30988): DllImport attempting to load: '/system/lib64/liblog.so'.
    06-04 19:32:45.768 D/Mono    (30988): DllImport loaded library '/system/lib64/liblog.so'.
    06-04 19:32:45.768 D/Mono    (30988): DllImport searching in: '/system/lib64/liblog.so' ('/system/lib64/liblog.so').
    06-04 19:32:45.768 D/Mono    (30988): Searching for '__android_log_print'.
    06-04 19:32:45.768 D/Mono    (30988): Probing '__android_log_print'.
    06-04 19:32:45.768 D/Mono    (30988): Found as '__android_log_print'.
    06-04 19:32:45.773 I/MonoDroid(30988): UNHANDLED EXCEPTION:
    06-04 19:32:45.808 I/MonoDroid(30988): System.NullReferenceException: Object reference not set to an instance of an object.
    06-04 19:32:45.808 I/MonoDroid(30988):   at PushNotification.Plugin.PushNotificationGcmListener.OnMessageReceived (System.String from, Android.OS.Bundle extras) [0x00095] in C:\xamarin-plugins\PushNotification\PushNotification\PushNotification.Plugin.Android\PushNotificationGcmListener.cs:65 
    06-04 19:32:45.808 I/MonoDroid(30988):   at Android.Gms.Gcm.GcmListenerService.n_OnMessageReceived_Ljava_lang_String_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_from, System.IntPtr native_data) [0x00017] in <eb44ad118cca47de8c776a05c1e4d8fa>:0 
    06-04 19:32:45.808 I/MonoDroid(30988):   at (wrapper dynamic-method) System.Object.638ecfc0-336d-447f-b043-1ec50cea86dd(intptr,intptr,intptr,intptr)
    06-04 19:32:45.821 W/art     (30988): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
    06-04 19:32:45.825 D/Mono    (30988): DllImport searching in: '__Internal' ('(null)').
    06-04 19:32:45.825 D/Mono    (30988): Searching for 'java_interop_jnienv_throw'.
    06-04 19:32:45.825 D/Mono    (30988): Probing 'java_interop_jnienv_throw'.
    06-04 19:32:45.825 D/Mono    (30988): Found as 'java_interop_jnienv_throw'.
    An unhandled exception occured.

    referenceTable GDEF length=814 1
    referenceTable GSUB length=11364 1
    referenceTable GPOS length=47302 1
    referenceTable GSUB length=68 106-04 19:32:58.147 F/art     (30988): art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: JNI NewString called with pending exception android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object.

    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at PushNotification.Plugin.PushNotificationGcmListener.OnMessageReceived (System.String from, Android.OS.Bundle extras) [0x00095] in C:\xamarin-plugins\PushNotification\PushNotification\PushNotification.Plugin.Android\PushNotificationGcmListener.cs:65 
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at Android.Gms.Gcm.GcmListenerService.n_OnMessageReceived_Ljava_lang_String_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_from, System.IntPtr native_data) [0x00017] in <eb44ad118cca47de8c776a05c1e4d8fa>:0 
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at (wrapper dynamic-method) System.Object.638ecfc0-336d-447f-b043-1ec50cea86dd(intptr,intptr,intptr,intptr)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at void pushnotification.plugin.PushNotificationGcmListener.n_onMessageReceived(java.lang.String, android.os.Bundle) (PushNotificationGcmListener.java:-2)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at void pushnotification.plugin.PushNotificationGcmListener.onMessageReceived(java.lang.String, android.os.Bundle) (PushNotificationGcmListener.java:29)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at void com.google.android.gms.gcm.GcmListenerService.handleIntent(android.content.Intent) ((null):-1)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at void com.google.firebase.iid.zzc.run() ((null):-1)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at void java.util.concurrent.ThreadPoolExecutor.runWorker(java.util.concurrent.ThreadPoolExecutor$Worker) (ThreadPoolExecutor.java:1113)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at void java.util.concurrent.ThreadPoolExecutor$Worker.run() (ThreadPoolExecutor.java:588)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at void java.lang.Thread.run() (Thread.java:818)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410] 
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]     in call to NewString
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]     from void pushnotification.plugin.PushNotificationGcmListener.n_onMessageReceived(java.lang.String, android.os.Bundle)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410] "pool-5-thread-1" prio=5 tid=12 Runnable
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   | group="main" sCount=0 dsCount=0 obj=0x130e7640 self=0x55c1924000
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   | sysTid=31138 nice=0 cgrp=default sched=0/0 handle=0x7f7a9bb450
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   | state=R schedstat=( 227870939 30097503 210 ) utm=20 stm=2 core=0 HZ=100
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   | stack=0x7f7a8b9000-0x7f7a8bb000 stackSize=1037KB
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   | held mutexes= "mutator lock"(shared held)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   native: #00 pc 000000000048fe68  /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiPKcPNS_9ArtMethodEPv+236)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   native: #01 pc 000000000045f028  /system/lib64/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+220)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   native: #02 pc 0000000000311400  /system/lib64/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+1000)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   native: #03 pc 0000000000311cb8  /system/lib64/libart.so (_ZN3art9JavaVMExt9JniAbortVEPKcS2_St9__va_list+116)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   native: #04 pc 000000000014501c  /system/lib64/libart.so (_ZN3art11ScopedCheck6AbortFEPKcz+144)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   native: #05 pc 000000000014becc  /system/lib64/libart.so (_ZN3art11ScopedCheck5CheckERNS_18ScopedObjectAccessEbPKcPNS_12JniValueTypeE.constprop.116+6088)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   native: #06 pc 0000000000156098  /system/lib64/libart.so (_ZN3art8CheckJNI9NewStringEP7_JNIEnvPKti+488)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   native: #07 pc 000000000000b420  /data/app/com.relyfy.caregiver-1/lib/arm64/libmonodroid.so (java_interop_jnienv_new_string+36)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   native: #08 pc 0000000000016a68   (???)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at pushnotification.plugin.PushNotificationGcmListener.n_onMessageReceived(Native method)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at pushnotification.plugin.PushNotificationGcmListener.onMessageReceived(PushNotificationGcmListener.java:29)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at com.google.android.gms.gcm.GcmListenerService.handleIntent(unavailable:-1)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at com.google.firebase.iid.zzc.run(unavailable:-1)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410]   at java.lang.Thread.run(Thread.java:818)
    06-04 19:32:58.148 F/art     (30988): art/runtime/java_vm_ext.cc:410] 

**This is the code for receiver in application element in Android.manifest file**


<receiver
      android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
      android:exported="false" />

    <receiver
     android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
     android:exported="false" />
    <receiver
        android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND">
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="${applicationId}" />
      </intent-filter>
    </receiver>

**Code in my MainActivity.cs for Firebase initialization**

    FirebaseApp.InitializeApp(this);
            if (Intent.Extras != null)
            {
                foreach (var key in Intent.Extras.KeySet())
                {
                    if (key != null)
                    {
                        var value = Intent.Extras.GetString(key);
                        Log.Info(TAG, "Key: {0} Value: {1}", key, value);
                    }
                }
            }
            Task.Run(async () =>
            {
                var instanceId = FirebaseInstanceId.Instance;
                Log.Info("TAG", "{0} {1}", instanceId?.Token?.ToString(),
                  instanceId.GetToken(GetString(Resource.String.gcm_defaultSenderId),
                    Firebase.Messaging.FirebaseMessaging.InstanceIdScope));
            });



            OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init();

            Xamarin.FormsMaps.Init(this, savedInstanceState);
            Forms.Init(this, savedInstanceState);
            AppDomain.CurrentDomain.UnhandledException += 
    CurrentDomain_UnhandledException;

            StartService(new Intent(this, typeof(MyFirebaseIIDService)));
            StartService(new Intent(this, typeof(MyFirebaseMessagingService)));

**Code in MyFirebaseIIDService.cs**

    [Service]
    [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
    public class MyFirebaseIIDService : FirebaseInstanceIdService
    {   
        const string TAG = "MyFirebaseIIDService";

        NotificationHub hub;



        public override void OnTokenRefresh()
        {
            var refreshedToken = FirebaseInstanceId.Instance.Token;
            Log.Debug(TAG, "Refreshed token: " + refreshedToken);

            Settings.DeviceToken = refreshedToken;

            try
            {
                SendRegistrationToServer(refreshedToken);
            }
            catch(Exception ex)
            {

            }
        }



        void SendRegistrationToServer(string token)
        {
            try
            {
                // Register with Notification Hubs
                hub = new NotificationHub(Constants.NotificationHubName,
                                          Constants.ListenConnectionString, this);

                var tags = new List<string>() { };
                var regID = hub.Register(token, tags.ToArray()).RegistrationId;

                Log.Debug(TAG, $"Successful registration of ID {regID}");
            }
            catch(Exception ex)
            {

            }
        }


    }

**Code in MyFirebaseMessagingService.cs**

    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {

        const string TAG = "MyFirebaseMsgService";
        public override void HandleIntent(Intent p0)
        {
            try
            {
                base.HandleIntent(p0);
            }
            catch(Java.Lang.Exception excd)
            {

            }
            catch (System.Exception excd)
            {

            }
        }
        public override void OnMessageReceived(RemoteMessage message)
        {
            Log.Debug(TAG, "From: " + message.From);
            try
            {
                //These is how most messages will be received
                    //Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
                    //SendNotification(message.GetNotification().Body);
                    Log.Debug(TAG, "From: " + message.From);

                    // Pull message body out of the template
                    var messageBody = message.Data["message"];


                    if (string.IsNullOrWhiteSpace(messageBody))
                        return;
                    Log.Debug(TAG, "Notification message body: " + messageBody);
                    SendNotification(messageBody);
            }
            catch(Java.Lang.Exception ex1)
            {

            }

        }

        public async Task SendNotification(string messageBody)
        {
            try
            {
                /*var intent = new Intent(this, typeof(MainActivity));
                intent.AddFlags(ActivityFlags.ClearTop);
                var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

                var notificationBuilder = new Notification.Builder(this)
                            .SetContentTitle("RelyFy Caregivers")
                            .SetSmallIcon(Resource.Drawable.app_logo)
                            .SetContentText(messageBody)
                            .SetAutoCancel(true)
                            .SetContentIntent(pendingIntent);

                var notificationManager = NotificationManager.FromContext(this);

                notificationManager.Notify(0, notificationBuilder.Build());*/
            }
            catch(Java.Lang.Exception ex)
            {

            }
        }

        public override void OnMessageSent(string msgId)
        {
            base.OnMessageSent(msgId);
        }
        public override void OnSendError(string msgId, Java.Lang.Exception exception)
        {
            base.OnSendError(msgId, exception);
        }
        public override void OnStart(Intent intent, int startId)
        {
            base.OnStart(intent, startId);
        }
    }
  • So on which line you received null reference Error? – Srusti Thakkar Jun 05 '18 at 04:24
  • @SrustiThakkar actually it's not the line or about code I guess because I can't figure it out. When I receive a push notification after a successful execution of OnRecieved method, it gives the exception in the exception details I checked the whole log but didn't find any line no. or reference related to my code – Vikram Singh Shekhawat Jun 05 '18 at 04:35
  • Have you tried to see the messagebody which is Received? Also it's depend on what kind of payload you are using. Are you trying to send it from Azure Portal? – Srusti Thakkar Jun 05 '18 at 04:41
  • @SrustiThakkar Yes, I have viewed the message body and I have used both Firebase console and azure portal in both cases the app crashes. This is the json from azure portal {"aps":{"alert":"Notification Hub test notification"}} from firebase simple message was sent – Vikram Singh Shekhawat Jun 05 '18 at 05:18
  • You are trying to test on Android or iOS? – Srusti Thakkar Jun 05 '18 at 05:30
  • Your payload from Azure will be like {"data":{"message":"Notification Hub test notification"}} If you are testing on Android device. – Srusti Thakkar Jun 05 '18 at 05:34
  • @SrustiThakkar Testing on android, android app is crashing. – Vikram Singh Shekhawat Jun 05 '18 at 05:34
  • @SrustiThakkar the iOS app works fine – Vikram Singh Shekhawat Jun 05 '18 at 05:35
  • Sorry i correct myself this was the json used {"data":{"message":"Notification Hub test notification"}} – Vikram Singh Shekhawat Jun 05 '18 at 05:38
  • So Now it's works fine? – Srusti Thakkar Jun 05 '18 at 05:39
  • Nah Not yet, the Issue is same – Vikram Singh Shekhawat Jun 05 '18 at 05:52
  • Okay Try to replace your code onMessageReceived() method with this code: if (message.GetNotification() != null) { Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body); SendNotification(message.GetNotification().Body); } else { l SendNotification(message.Data.Values.First()); } – Srusti Thakkar Jun 05 '18 at 05:55
  • @SrustiThakkar i tried that code but that code i.e. message.GetNotification is always NULL – Vikram Singh Shekhawat Jun 05 '18 at 06:16
  • Are you trying from Azure Portal Right? So It display notification how many devices get that notification. Can you please check that? – Srusti Thakkar Jun 05 '18 at 06:36
  • @SrustiThakkar yes from the Azure portal, is shows 3 passed and 2 failed the passed device is mine on which I'm receiving the notification – Vikram Singh Shekhawat Jun 05 '18 at 06:46
  • Okay So as you are using payload as "data" then you will get data value instead of message.GetNotification().body. Please check that on debug. – Srusti Thakkar Jun 05 '18 at 06:55
  • @SrustiThakkar exactly what I'm saying is that i'm getting the notification data through data object of json with message parameter the thing is that I can get notification content correctly, the debug console confirms that. But after successful execution of OnMessageRecieved() method just after the execution of it, the app crashes with Null pointer exception. – Vikram Singh Shekhawat Jun 05 '18 at 07:05
  • Uncomment your SendNotification() method code and change it from task to void. – Srusti Thakkar Jun 05 '18 at 07:15
  • @SrustiThakkar Let me explain it the code in Send Notification also works fine i can see the notification in the status bar, but when the command goes back to OnMessageRecieved, it exits OnMessageRecieved with success but after the exit, the app crashes immediately, the return type for SendNotification is void/Task both i had tried – Vikram Singh Shekhawat Jun 05 '18 at 07:33
  • I don't know exact reason. But I think due to your builder code (i.e. .SetContentIntent(pendingIntent);). I think you use intent variable you have commented on your code. – Srusti Thakkar Jun 05 '18 at 08:40
  • @SrustiThakkar Actually when I commented that code then also the same exception arises, I also can't get what's the problem out there. – Vikram Singh Shekhawat Jun 05 '18 at 09:29
  • @SrustiThakkar Also when remove the implementation of FirebaseMessagingService from my handler class the app doesn't crash it only happens when i handle a notification – Vikram Singh Shekhawat Jun 05 '18 at 09:46
  • @SrustiThakkar My issue has been solved by examining the error logs, i realized that there was a plugin called Xam.Plugin.PushNotification creating problem. so i removed that. Thanks for your help – Vikram Singh Shekhawat Jun 06 '18 at 13:25
  • 1
    Great. Good to here that. – Srusti Thakkar Jun 07 '18 at 11:14

0 Answers0