PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/PushSharp.WindowsPhone/WindowsPhoneNotification.cs

https://github.com/mustafagenc/PushSharp
C# | 233 lines | 177 code | 56 blank | 0 comment | 30 complexity | 894c18cd350ed97d77e4881edd9a4c36 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. namespace PushSharp.WindowsPhone
  7. {
  8. public enum WindowsPhoneDeviceOSVersion
  9. {
  10. Seven = 7,
  11. MangoSevenPointFive = 8,
  12. Eight = 9
  13. }
  14. public enum BatchingInterval
  15. {
  16. Immediate,
  17. Medium,
  18. Slow
  19. }
  20. public enum NotificationType
  21. {
  22. Tile,
  23. Toast,
  24. Raw
  25. }
  26. public class WindowsPhoneNotificationFactory : Common.Notification
  27. {
  28. public WindowsPhoneRawNotification Raw()
  29. {
  30. return new WindowsPhoneRawNotification();
  31. }
  32. public WindowsPhoneTileNotification Tile()
  33. {
  34. return new WindowsPhoneTileNotification();
  35. }
  36. public WindowsPhoneToastNotification Toast()
  37. {
  38. return new WindowsPhoneToastNotification();
  39. }
  40. }
  41. public abstract class WindowsPhoneNotification : Common.Notification
  42. {
  43. protected WindowsPhoneNotification()
  44. {
  45. this.Platform = Common.PlatformType.WindowsPhone;
  46. this.MessageID = Guid.NewGuid();
  47. }
  48. public string EndPointUrl { get; set; }
  49. public Guid MessageID { get; set; }
  50. public BatchingInterval? NotificationClass { get; set; } //Batching interval 2 = immediate, 12 = 450 seconds, 22 = 900 seconds
  51. public NotificationType NotificationType { get; set; } //Tile, toast, raw (raw default)
  52. public string CallbackURI { get; set; }
  53. public abstract string PayloadToString();
  54. public override string ToString()
  55. {
  56. return PayloadToString();
  57. }
  58. public WindowsPhoneDeviceOSVersion OSVersion { get; set; }
  59. protected string XmlEncode(string text)
  60. {
  61. return System.Security.SecurityElement.Escape(text);
  62. }
  63. }
  64. public class WindowsPhoneToastNotification : WindowsPhoneNotification
  65. {
  66. public WindowsPhoneToastNotification()
  67. : base()
  68. {
  69. }
  70. public string Text1 { get; set; }
  71. public string Text2 { get; set; }
  72. public string NavigatePath { get; set; }
  73. public System.Collections.Specialized.NameValueCollection Parameters { get; set; }
  74. public override string PayloadToString()
  75. {
  76. var sb = new StringBuilder();
  77. sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  78. sb.AppendLine("<wp:Notification xmlns:wp=\"WPNotification\">");
  79. sb.AppendLine("<wp:Toast>");
  80. if (!string.IsNullOrEmpty(Text1))
  81. sb.AppendLine("<wp:Text1>" + XmlEncode(Text1) + "</wp:Text1>");
  82. if (!string.IsNullOrEmpty(Text2))
  83. sb.AppendLine("<wp:Text2>" + XmlEncode(Text2) + "</wp:Text2>");
  84. if (this.OSVersion > WindowsPhoneDeviceOSVersion.Seven)
  85. {
  86. if (!string.IsNullOrEmpty(NavigatePath) || (Parameters != null && Parameters.Count > 0))
  87. {
  88. sb.Append("<wp:Param>");
  89. if (!string.IsNullOrEmpty(NavigatePath))
  90. sb.Append(XmlEncode("/" + NavigatePath.TrimStart('/')));
  91. if (Parameters != null && Parameters.Count > 0)
  92. {
  93. sb.Append("?");
  94. foreach (string key in Parameters.Keys)
  95. sb.Append(XmlEncode(key + "=" + Parameters[key].ToString()) + "&amp;");
  96. }
  97. sb.AppendLine("</wp:Param>");
  98. }
  99. }
  100. sb.AppendLine("</wp:Toast>");
  101. sb.AppendLine("</wp:Notification>");
  102. return sb.ToString();
  103. }
  104. }
  105. public class WindowsPhoneRawNotification : WindowsPhoneNotification
  106. {
  107. public WindowsPhoneRawNotification()
  108. : base()
  109. {
  110. }
  111. public string Raw { get; set; }
  112. public override string PayloadToString()
  113. {
  114. return Raw;
  115. }
  116. }
  117. public class WindowsPhoneTileNotification : WindowsPhoneNotification
  118. {
  119. public WindowsPhoneTileNotification()
  120. : base()
  121. {
  122. }
  123. public string TileId { get; set; } //Secondary tile id, leave blank for application tile
  124. public string BackgroundImage { get; set; }
  125. public int? Count { get; set; }
  126. public bool ClearCount { get; set; }
  127. public string Title { get; set; }
  128. public bool ClearTitle { get; set; }
  129. public string BackBackgroundImage { get; set; }
  130. public bool ClearBackBackgroundImage { get; set; }
  131. public string BackTitle { get; set; }
  132. public bool ClearBackTitle { get; set; }
  133. public string BackContent { get; set; }
  134. public bool ClearBackContent { get; set; }
  135. public override string PayloadToString()
  136. {
  137. var sb = new StringBuilder();
  138. sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  139. sb.AppendLine("<wp:Notification xmlns:wp=\"WPNotification\">");
  140. sb.Append("<wp:Tile");
  141. if (this.OSVersion > WindowsPhoneDeviceOSVersion.Seven)
  142. {
  143. if (!string.IsNullOrEmpty(this.TileId))
  144. sb.Append(" Id=\"" + XmlEncode(this.TileId) + "\"");
  145. }
  146. sb.AppendLine(">");
  147. if (!string.IsNullOrEmpty(BackgroundImage))
  148. sb.AppendLine("<wp:BackgroundImage>" + XmlEncode(this.BackgroundImage) + "</wp:BackgroundImage>");
  149. if (ClearCount)
  150. sb.AppendLine("<wp:Count Action=\"Clear\"></wp:Count>");
  151. else if (Count.HasValue)
  152. sb.AppendLine("<wp:Count>" + Count.ToString() + "</wp:Count>");
  153. if (ClearTitle)
  154. sb.AppendLine("<wp:Title Action=\"Clear\"></wp:Title>");
  155. else if (!string.IsNullOrEmpty(Title))
  156. sb.AppendLine("<wp:Title>" + XmlEncode(Title) + "</wp:Title>");
  157. if (this.OSVersion > WindowsPhoneDeviceOSVersion.Seven)
  158. {
  159. if (ClearBackBackgroundImage)
  160. sb.AppendLine("<wp:BackBackgroundImage Action=\"Clear\"></wp:BackBackgroundImage>");
  161. else if (!string.IsNullOrEmpty(BackBackgroundImage))
  162. sb.AppendLine("<wp:BackBackgroundImage>" + XmlEncode(BackBackgroundImage) + "</wp:BackBackgroundImage>");
  163. if (ClearBackTitle)
  164. sb.AppendLine("<wp:BackTitle Action=\"Clear\"></wp:BackTitle>");
  165. else if (!string.IsNullOrEmpty(BackTitle))
  166. sb.AppendLine("<wp:BackTitle>" + XmlEncode(BackTitle) + "</wp:BackTitle>");
  167. if (ClearBackContent)
  168. sb.AppendLine("<wp:BackContent Action=\"Clear\"></wp:BackContent>");
  169. else if (!string.IsNullOrEmpty(BackContent))
  170. sb.AppendLine("<wp:BackContent>" + XmlEncode(BackContent) + "</wp:BackContent>");
  171. }
  172. sb.AppendLine("</wp:Tile>");
  173. sb.AppendLine("</wp:Notification>");
  174. return sb.ToString();
  175. }
  176. }
  177. }