OneSignal/OneSignal-Xamarin-SDK

SendTags call results in System.InvalidCastException

ChrisMonson opened this issue · 3 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

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

For anyone who hits this, I've shared a version that has @ChrisMonson 's fix in #313 applied here: #313 (comment)

This has been fixed and released in 4.3.1