OneSignal / OneSignal-Xamarin-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your Xamarin app with OneSignal. https://onesignal.com
Other
104 stars 50 forks source link

NullReferenceException in Beta 3 when receiving notification on iOS #274

Closed Immons closed 2 years ago

Immons commented 2 years ago

Description:

I am sending notification through REST API, it was working fine with OneSignal 3.x, but when updated to 4.0.0 beta 3, it crashes with following stack trace.

Environment

  1. Xamarin.iOS
  2. Com.OneSignal 4.0.0-beta3 from Nuget

Steps to Reproduce Issue:

Example:

  1. Add version 4.0.0 beta 3 of the Xamarin SDK to your project
  2. Initialize the SDK with your App ID
  3. Attempt to receive a push notification

Anything else:

 System.NullReferenceException: Object reference not set to an instance of an object
  at Foundation.NSNumber.op_Explicit (Foundation.NSNumber source) [0x00000] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSNumber2.cs:117 
  at Com.OneSignal.NativeConversion.NotificationToXam (Com.OneSignal.iOS.OSNotification notification) [0x00000] in /Users/tanay/Desktop/Xamarin_SDK/OneSignal_Xamarin-4_0_0/OneSignal-Xamarin-SDK/Com.OneSignal.iOS/Utilities/NativeConversion.cs:41 
  at Com.OneSignal.OneSignalImplementation+<>c.<.ctor>b__99_0 (Com.OneSignal.iOS.OSNotification nativeNotification, Com.OneSignal.iOS.OSNotificationDisplayResponse response) [0x00014] in /Users/tanay/Desktop/Xamarin_SDK/OneSignal_Xamarin-4_0_0/OneSignal-Xamarin-SDK/Com.OneSignal.iOS/OneSignalCallbacks.cs:54 
  at ObjCRuntime.Trampolines+SDOSNotificationWillShowInForegroundBlock.Invoke (System.IntPtr block, System.IntPtr arg0, System.IntPtr arg1) [0x00016] in /Users/tanay/Desktop/Xamarin_SDK/OneSignal_Xamarin-4_0_0/OneSignal-Xamarin-SDK/OneSignal.iOS.Binding/obj/Release/iOS/ObjCRuntime/Trampolines.g.cs:298 
  at (wrapper native-to-managed) ObjCRuntime.Trampolines+SDOSNotificationWillShowInForegroundBlock.Invoke(intptr,intptr,intptr)
  at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00013] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:73 
  at KidsTime.iOS.Application.Main (System.String[] args) [0x00002] in /Users/immons/Projects/KidsTime/KidsTime.iOS/Main.cs:18 
Immons commented 2 years ago

So there seems to be exactly two issues.

First one is inside NotificationToXam assigning relevance score that way:

relevanceScore = (float) notification.RelevanceScore,

in this case, it will throw NullReferenceException, fix for that should be:

relevanceScore = notification.RelevanceScore != null ? (float) notification.RelevanceScore : 0,

and the second problem, is this line:

additionalData = Json.Deserialize(notification.AdditionalData.ToString()) as Dictionary<string, object>,

it "deserializes" to null, the solution could be code from your 3.10 implementation:

var additionalData = new Dictionary<string, object>();
         if (notification.AdditionalData != null)
         {
            foreach (KeyValuePair<NSObject, NSObject> element in notification.AdditionalData)
            {
               additionalData.Add((NSString)element.Key, element.Value);
            }
         }
jkasten2 commented 2 years ago

@Immons Thanks for reporting, providing the code, and reviewing the PR above!

Just to let you know we have the fix included in the 3.0.0-beta4 release: https://github.com/OneSignal/OneSignal-Xamarin-SDK/releases/tag/4.0.0-beta4