Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException in Beta 3 when receiving notification on iOS #274

Closed
Immons opened this issue Feb 12, 2022 · 2 comments
Closed

NullReferenceException in Beta 3 when receiving notification on iOS #274

Immons opened this issue Feb 12, 2022 · 2 comments

Comments

@Immons
Copy link

Immons commented Feb 12, 2022

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
Copy link
Author

Immons commented Feb 12, 2022

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
Copy link
Member

jkasten2 commented Mar 7, 2022

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants