В FCM Push-уведомление System.NullReferenceException: ссылка на объект не установлена для экземпляра объекта.
Привет всем, я получаю исключение, когда я получил удаленное уведомление от FCM в моем кроссплатформенном приложении Xamarin.Forms на платформе Android
Это не дублирует, потому что я получаю эту ошибку при push-уведомлении, я думаю, это не связано с кодом.
Кроме того, метод OnReceded вызвал успешно, а также создал уведомление. Я вижу уведомление в строке состояния после полного вызова метода OnReceived, который вызывает сбой с этим исключением.
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);
}
}