PageRenderTime 1828ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/PushSharp.Apple/AppleNotificationPayload.cs

http://github.com/Redth/PushSharp
C# | 256 lines | 119 code | 48 blank | 89 comment | 30 complexity | 2fca267ae7752273e42352e3e9845a40 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Runtime.Serialization;
  6. using System.Runtime.Serialization.Json;
  7. using System.Text;
  8. using Newtonsoft.Json.Linq;
  9. namespace PushSharp.Apple
  10. {
  11. public class AppleNotificationPayload
  12. {
  13. public AppleNotificationAlert Alert { get; set; }
  14. public int? ContentAvailable { get; set; }
  15. public int? Badge { get; set; }
  16. public string Sound { get; set; }
  17. public bool HideActionButton { get; set; }
  18. public Dictionary<string, object[]> CustomItems
  19. {
  20. get;
  21. private set;
  22. }
  23. public AppleNotificationPayload()
  24. {
  25. HideActionButton = false;
  26. Alert = new AppleNotificationAlert();
  27. CustomItems = new Dictionary<string, object[]>();
  28. }
  29. public AppleNotificationPayload(string alert)
  30. {
  31. HideActionButton = false;
  32. Alert = new AppleNotificationAlert() { Body = alert };
  33. CustomItems = new Dictionary<string, object[]>();
  34. }
  35. public AppleNotificationPayload(string alert, int badge)
  36. {
  37. HideActionButton = false;
  38. Alert = new AppleNotificationAlert() { Body = alert };
  39. Badge = badge;
  40. CustomItems = new Dictionary<string, object[]>();
  41. }
  42. public AppleNotificationPayload(string alert, int badge, string sound)
  43. {
  44. HideActionButton = false;
  45. Alert = new AppleNotificationAlert() { Body = alert };
  46. Badge = badge;
  47. Sound = sound;
  48. CustomItems = new Dictionary<string, object[]>();
  49. }
  50. public void AddCustom(string key, params object[] values)
  51. {
  52. if (values != null)
  53. this.CustomItems.Add(key, values);
  54. }
  55. public string ToJson()
  56. {
  57. JObject json = new JObject();
  58. JObject aps = new JObject();
  59. if (!this.Alert.IsEmpty)
  60. {
  61. if (!string.IsNullOrEmpty(this.Alert.Body)
  62. && string.IsNullOrEmpty(this.Alert.LocalizedKey)
  63. && string.IsNullOrEmpty(this.Alert.ActionLocalizedKey)
  64. && (this.Alert.LocalizedArgs == null || this.Alert.LocalizedArgs.Count <= 0)
  65. && string.IsNullOrEmpty(this.Alert.LaunchImage)
  66. && !this.HideActionButton)
  67. {
  68. aps["alert"] = new JValue(this.Alert.Body);
  69. }
  70. else
  71. {
  72. JObject jsonAlert = new JObject();
  73. if (!string.IsNullOrEmpty(this.Alert.LocalizedKey))
  74. jsonAlert["loc-key"] = new JValue(this.Alert.LocalizedKey);
  75. if (this.Alert.LocalizedArgs != null && this.Alert.LocalizedArgs.Count > 0)
  76. jsonAlert["loc-args"] = new JArray(this.Alert.LocalizedArgs.ToArray());
  77. if (!string.IsNullOrEmpty(this.Alert.Body))
  78. jsonAlert["body"] = new JValue(this.Alert.Body);
  79. if (this.HideActionButton)
  80. jsonAlert["action-loc-key"] = new JValue((string)null);
  81. else if (!string.IsNullOrEmpty(this.Alert.ActionLocalizedKey))
  82. jsonAlert["action-loc-key"] = new JValue(this.Alert.ActionLocalizedKey);
  83. if (!string.IsNullOrEmpty(this.Alert.LaunchImage))
  84. jsonAlert["launch-image"] = new JValue(this.Alert.LaunchImage);
  85. aps["alert"] = jsonAlert;
  86. }
  87. }
  88. if (this.Badge.HasValue)
  89. aps["badge"] = new JValue(this.Badge.Value);
  90. if (!string.IsNullOrEmpty(this.Sound))
  91. aps["sound"] = new JValue(this.Sound);
  92. if (this.ContentAvailable.HasValue)
  93. aps["content-available"] = new JValue(this.ContentAvailable.Value);
  94. if (aps.Count > 0)
  95. json["aps"] = aps;
  96. foreach (string key in this.CustomItems.Keys)
  97. {
  98. if (this.CustomItems[key].Length == 1)
  99. json[key] = new JValue(this.CustomItems[key][0]);
  100. else if (this.CustomItems[key].Length > 1)
  101. json[key] = new JArray(this.CustomItems[key]);
  102. }
  103. string rawString = json.ToString(Newtonsoft.Json.Formatting.None, null);
  104. StringBuilder encodedString = new StringBuilder();
  105. foreach (char c in rawString)
  106. {
  107. if ((int)c < 32 || (int)c > 127)
  108. encodedString.Append("\\u" + String.Format("{0:x4}", Convert.ToUInt32(c)));
  109. else
  110. encodedString.Append(c);
  111. }
  112. return rawString;// encodedString.ToString();
  113. }
  114. //public string ToJson()
  115. //{
  116. // bool tmpBool;
  117. // double tmpDbl;
  118. // var json = new StringBuilder();
  119. // var aps = new StringBuilder();
  120. // if (!this.Alert.IsEmpty)
  121. // {
  122. // if (!string.IsNullOrEmpty(this.Alert.Body)
  123. // && string.IsNullOrEmpty(this.Alert.LocalizedKey)
  124. // && string.IsNullOrEmpty(this.Alert.ActionLocalizedKey)
  125. // && (this.Alert.LocalizedArgs == null || this.Alert.LocalizedArgs.Count <= 0)
  126. // && !this.HideActionButton)
  127. // {
  128. // aps.AppendFormat("\"alert\":\"{0}\",", this.Alert.Body);
  129. // }
  130. // else
  131. // {
  132. // var jsonAlert = new StringBuilder();
  133. // if (!string.IsNullOrEmpty(this.Alert.LocalizedKey))
  134. // jsonAlert.AppendFormat("\"loc-key\":\"{0}\",", this.Alert.LocalizedKey);
  135. // if (this.Alert.LocalizedArgs != null && this.Alert.LocalizedArgs.Count > 0)
  136. // {
  137. // var locArgs = new StringBuilder();
  138. // foreach (var larg in this.Alert.LocalizedArgs)
  139. // {
  140. // if (double.TryParse(larg.ToString(), out tmpDbl)
  141. // || bool.TryParse(larg.ToString(), out tmpBool))
  142. // locArgs.AppendFormat("{0},", larg.ToString());
  143. // else
  144. // locArgs.AppendFormat("\"{0}\",", larg.ToString());
  145. // }
  146. // jsonAlert.AppendFormat("\"loc-args\":[{0}],", locArgs.ToString().TrimEnd(','));
  147. // }
  148. // if (!string.IsNullOrEmpty(this.Alert.Body))
  149. // jsonAlert.AppendFormat("\body\":\"{0}\",", this.Alert.Body);
  150. // if (this.HideActionButton)
  151. // jsonAlert.AppendFormat("\"action-loc-key\":null,");
  152. // else if (!string.IsNullOrEmpty(this.Alert.ActionLocalizedKey))
  153. // jsonAlert.AppendFormat("\action-loc-key\":\"{0}\",", this.Alert.ActionLocalizedKey);
  154. // aps.Append("\"alert\":{");
  155. // aps.Append(jsonAlert.ToString().TrimEnd(','));
  156. // aps.Append("},");
  157. // }
  158. // }
  159. // if (this.Badge.HasValue)
  160. // aps.AppendFormat("\"badge\":{0},", this.Badge.Value.ToString());
  161. // if (!string.IsNullOrEmpty(this.Sound))
  162. // aps.AppendFormat("\"sound\":\"{0}\",", this.Sound);
  163. // if (this.ContentAvailable.HasValue)
  164. // aps.AppendFormat("\"content-available\":{0},", this.ContentAvailable.Value.ToString());
  165. // json.Append("\"aps\":{");
  166. // json.Append(aps.ToString().TrimEnd(','));
  167. // json.Append("},");
  168. // foreach (string key in this.CustomItems.Keys)
  169. // {
  170. // if (this.CustomItems[key].Length == 1)
  171. // {
  172. // if (double.TryParse(this.CustomItems[key].ToString(), out tmpDbl)
  173. // || bool.TryParse(this.CustomItems[key].ToString(), out tmpBool))
  174. // json.AppendFormat("\"{0}\":[{1}],", key, this.CustomItems[key][0].ToString());
  175. // else
  176. // json.AppendFormat("\"{0}\":[\"{1}\",", key, this.CustomItems[key][0].ToString());
  177. // }
  178. // else if (this.CustomItems[key].Length > 1)
  179. // {
  180. // var jarr = new StringBuilder();
  181. // foreach (var item in this.CustomItems[key])
  182. // {
  183. // if (double.TryParse(item.ToString(), out tmpDbl)
  184. // || bool.TryParse(item.ToString(), out tmpBool))
  185. // jarr.AppendFormat("{0},", item.ToString());
  186. // else
  187. // jarr.AppendFormat("\"{0}\",", item.ToString());
  188. // }
  189. // json.AppendFormat("\"{0}\":[{1}],", key, jarr.ToString().Trim(','));
  190. // }
  191. // }
  192. // string rawString = "{" + json.ToString().TrimEnd(',') + "}";
  193. // StringBuilder encodedString = new StringBuilder();
  194. // foreach (char c in rawString)
  195. // {
  196. // if ((int)c < 32 || (int)c > 127)
  197. // encodedString.Append("\\u" + String.Format("{0:x4}", Convert.ToUInt32(c)));
  198. // else
  199. // encodedString.Append(c);
  200. // }
  201. // return rawString;// encodedString.ToString();
  202. //}
  203. public override string ToString()
  204. {
  205. return ToJson();
  206. }
  207. }
  208. }