PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/PushSharp.Windows/WindowsNotification.cs

https://github.com/mustafagenc/PushSharp
C# | 206 lines | 161 code | 45 blank | 0 comment | 2 complexity | c463e175c42008f97cb594e1fc7ec2f6 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.Threading.Tasks;
  6. namespace PushSharp.Windows
  7. {
  8. public abstract class WindowsNotification : Common.Notification
  9. {
  10. protected WindowsNotification()
  11. {
  12. this.Platform = Common.PlatformType.Windows;
  13. }
  14. public string ChannelUri { get; set; }
  15. public bool? RequestForStatus { get; set; }
  16. public int? TimeToLive { get; set; }
  17. public abstract string PayloadToString();
  18. public abstract WindowsNotificationType Type { get; }
  19. protected string XmlEncode(string text)
  20. {
  21. return System.Security.SecurityElement.Escape(text);
  22. }
  23. }
  24. public class WindowsTileNotification : WindowsNotification
  25. {
  26. public WindowsTileNotification()
  27. : base()
  28. {
  29. this.Texts = new List<string>();
  30. this.Images = new Dictionary<string, string>();
  31. this.TileTemplate = TileNotificationTemplate.TileSquareBlock;
  32. }
  33. public override WindowsNotificationType Type
  34. {
  35. get { return WindowsNotificationType.Tile; }
  36. }
  37. public WindowsNotificationCachePolicyType? CachePolicy { get; set; }
  38. public string NotificationTag { get; set; }
  39. public TileNotificationTemplate TileTemplate { get; set; }
  40. public Dictionary<string, string> Images { get; set; }
  41. public List<string> Texts { get; set; }
  42. public override string PayloadToString()
  43. {
  44. var xml = new StringBuilder();
  45. xml.Append("<tile>");
  46. xml.Append("<visual>");
  47. xml.AppendFormat("<binding template=\"{0}\">", this.TileTemplate.ToString());
  48. int idOn = 1;
  49. foreach (var imgSrc in Images.Keys)
  50. {
  51. var alt = Images[imgSrc];
  52. if (!string.IsNullOrEmpty(alt))
  53. xml.AppendFormat("<image id=\"{0}\" src=\"{1}\" alt=\"{2}\"/>", idOn, XmlEncode(imgSrc), XmlEncode(alt));
  54. else
  55. xml.AppendFormat("<image id=\"{0}\" src=\"{1}\"/>", idOn, XmlEncode(imgSrc));
  56. idOn++;
  57. }
  58. idOn = 1;
  59. foreach (var text in Texts)
  60. {
  61. xml.AppendFormat("<text id=\"{0}\">{1}</text>", idOn, XmlEncode(text));
  62. idOn++;
  63. }
  64. xml.Append("</binding>");
  65. xml.Append("</visual>");
  66. xml.Append("</tile>");
  67. return xml.ToString();
  68. }
  69. }
  70. public class WindowsToastNotification : WindowsNotification
  71. {
  72. public WindowsToastNotification()
  73. : base()
  74. {
  75. this.Texts = new List<string>();
  76. this.Images = new Dictionary<string, string>();
  77. this.TextTemplate = ToastNotificationTemplate.ToastImageAndText01;
  78. }
  79. public override WindowsNotificationType Type
  80. {
  81. get { return WindowsNotificationType.Toast; }
  82. }
  83. public ToastNotificationTemplate TextTemplate { get; set; }
  84. public Dictionary<string, string> Images { get; set; }
  85. public List<string> Texts { get; set; }
  86. public override string PayloadToString()
  87. {
  88. var xml = new StringBuilder();
  89. xml.Append("<toast>");
  90. xml.Append("<visual>");
  91. xml.AppendFormat("<binding template=\"{0}\">", this.TextTemplate.ToString());
  92. int idOn = 1;
  93. foreach (var imgSrc in Images.Keys)
  94. {
  95. var alt = Images[imgSrc];
  96. if (!string.IsNullOrEmpty(alt))
  97. xml.AppendFormat("<image id=\"{0}\" src=\"{1}\" alt=\"{2}\"/>", idOn, XmlEncode(imgSrc), XmlEncode(alt));
  98. else
  99. xml.AppendFormat("<image id=\"{0}\" src=\"{1}\"/>", idOn, XmlEncode(imgSrc));
  100. idOn++;
  101. }
  102. idOn = 1;
  103. foreach (var text in Texts)
  104. {
  105. xml.AppendFormat("<text id=\"{0}\">{1}</text>", idOn, XmlEncode(text));
  106. idOn++;
  107. }
  108. xml.Append("</binding>");
  109. xml.Append("</visual>");
  110. xml.Append("</toast>");
  111. return xml.ToString();
  112. }
  113. }
  114. public class WindowsBadgeNotification : WindowsNotification
  115. {
  116. public override WindowsNotificationType Type
  117. {
  118. get { return WindowsNotificationType.Badge; }
  119. }
  120. public WindowsNotificationCachePolicyType? CachePolicy { get; set; }
  121. public override string PayloadToString()
  122. {
  123. throw new NotImplementedException();
  124. }
  125. }
  126. public class WindowsRawNotification : WindowsNotification
  127. {
  128. public override WindowsNotificationType Type
  129. {
  130. get { return WindowsNotificationType.Raw; }
  131. }
  132. public string RawXml { get; set; }
  133. public override string PayloadToString()
  134. {
  135. return RawXml;
  136. }
  137. }
  138. public enum WindowsNotificationCachePolicyType
  139. {
  140. Cache,
  141. NoCache
  142. }
  143. public enum WindowsNotificationType
  144. {
  145. Badge,
  146. Tile,
  147. Toast,
  148. Raw
  149. }
  150. public enum ToastNotificationTemplate
  151. {
  152. ToastText01,
  153. ToastText02,
  154. ToastText03,
  155. ToastText04,
  156. ToastImageAndText01,
  157. ToastImageAndText02,
  158. ToastImageAndText03,
  159. ToastImageAndText04
  160. }
  161. }