From daed665b2e0b7a741bfe125b0b0207714bc8f21d Mon Sep 17 00:00:00 2001 From: sadra-ab Date: Wed, 9 Apr 2014 12:03:22 -0600 Subject: [PATCH 1/2] Update AppleNotificationPayload.cs Lines 123 to 139 This section was modified to allow passing any object to the APNS. Previously, only primitive types were allowed. Lines 270 to 276 A helper function to return a JToken from any object --- PushSharp.Apple/AppleNotificationPayload.cs | 26 +++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/PushSharp.Apple/AppleNotificationPayload.cs b/PushSharp.Apple/AppleNotificationPayload.cs index b3cd5c7a..7ca4ea39 100644 --- a/PushSharp.Apple/AppleNotificationPayload.cs +++ b/PushSharp.Apple/AppleNotificationPayload.cs @@ -120,10 +120,23 @@ public string ToJson() foreach (string key in this.CustomItems.Keys) { + + // This section was modified to allow passing any object to the APNS. + // Previously, only primitive types were allowed. + if (this.CustomItems[key].Length == 1) - json[key] = new JValue(this.CustomItems[key][0]); + { + //json[key] = new JValue(this.CustomItems[key][0]); + json[key] = GetJsonToken(this.CustomItems[key][0]); + } else if (this.CustomItems[key].Length > 1) - json[key] = new JArray(this.CustomItems[key]); + { + //json[key] = new JArray(this.CustomItems[key]); + var list = new List(); + foreach (var customItem in this.CustomItems[key]) + list.Add(GetJsonToken(customItem)); + json[key] = new JArray(list); + } } string rawString = json.ToString(Newtonsoft.Json.Formatting.None, null); @@ -252,5 +265,14 @@ public override string ToString() { return ToJson(); } + + + private JToken GetJsonToken(object data) + { + if (data.GetType().IsPrimitive) + return new JValue(data); + + return JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(data)); + } } } From f096a9640251d53a4e3c7589c8655c3713d6ddd1 Mon Sep 17 00:00:00 2001 From: sadra-ab Date: Wed, 9 Apr 2014 15:25:06 -0600 Subject: [PATCH 2/2] Update AppleNotificationPayload.cs Updated the GetJsonToken function to fix the issue with data of type stirng and DateTime --- PushSharp.Apple/AppleNotificationPayload.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/PushSharp.Apple/AppleNotificationPayload.cs b/PushSharp.Apple/AppleNotificationPayload.cs index 7ca4ea39..1fab27ab 100644 --- a/PushSharp.Apple/AppleNotificationPayload.cs +++ b/PushSharp.Apple/AppleNotificationPayload.cs @@ -269,7 +269,8 @@ public override string ToString() private JToken GetJsonToken(object data) { - if (data.GetType().IsPrimitive) + var dataType = data.GetType(); + if ((dataType.IsValueType) || (dataType.IsAssignableFrom(typeof(string)))) return new JValue(data); return JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(data));