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

SendTags call results in System.InvalidCastException #311

Closed ChrisMonson closed 1 year ago

ChrisMonson commented 2 years ago

Description:

Making a call to OneSignal.Default.SendTags(myTags) results in a System.InvalidCastException with a message of "Specified cast is not valid."

Environment

Visual Studio 2022 Xamarin 17.2.0.177 OneSignalSDK.Xamarin 4.1.0 Installed via nuget

Steps to Reproduce Issue:

  1. Start a new Xamarin Forms blank project
  2. Install OneSignalSDK.Xamarin 4.1.0
  3. Add initialization code following documentation
  4. Make an awaited call to OneSignal.Default.SendTags and supply a dictionary with tags and values.

Anything else:

In digging through code for SendTags in the Android implementation, it appears that in November the method was changed from directly supplying a string of JSON data, to serializing the dictionary to a string and then attempting to cast it to a JSONObject.

OneSignalNative.SendTags((JSONObject)Json.Serialize(tags), handler);

This throws an invalid cast exception as it can't cast from the string returned from the serializer to JSONObject. Perhaps a better approach would be to use the JSONObject constructor that accepts a JSON string as a parameter?

An additional note is that due to a bug in Xamarin.Android at the moment, the exception is currently being returned as a null reference exception if it is awaited and not wrapped in a try/catch.

Stack Trace: at OneSignalSDK.Xamarin.OneSignalImplementation.SendTags (System.Collections.Generic.Dictionary`2[TKey,TValue] tags) [0x00010] in /Users/tanay/Desktop/OneSignal/OneSignal-Xamarin-SDK/OneSignalSDK.Xamarin.Android/OneSignalImplementation.cs:169 at OneSignalTest.App.OnStart () [0x0004f] in C:\dev\OneSignalTest\OneSignalTest\OneSignalTest\App.xaml.cs:35

Reproduction project attached. OneSignalTest.zip

ChrisMonson commented 2 years ago

As an additional note, this is broken in both the 4.0 and 4.1 versions of the SDK.

kevin-david commented 1 year ago

For anyone who hits this, I've shared a version that has @ChrisMonson 's fix in #313 applied here: https://github.com/OneSignal/OneSignal-Xamarin-SDK/pull/313#issuecomment-1368161859

brismithers commented 1 year ago

This has been fixed and released in 4.3.1