OneSignal-Xamarin-SDK icon indicating copy to clipboard operation
OneSignal-Xamarin-SDK copied to clipboard

SendTags call results in System.InvalidCastException

Open ChrisMonson opened this issue 2 years ago • 1 comments

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 avatar Jun 29 '22 23:06 ChrisMonson

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

ChrisMonson avatar Jul 06 '22 20:07 ChrisMonson

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

kevin-david avatar Dec 31 '22 04:12 kevin-david

This has been fixed and released in 4.3.1

brismithers avatar Jan 19 '23 22:01 brismithers