TilePushNotificationMessage.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using WindowsPhone.Recipes.Push.Messasges.Properties;
  6. namespace WindowsPhone.Recipes.Push.Messasges
  7. {
  8. /// <summary>
  9. /// Represents a tile push notification message.
  10. /// </summary>
  11. /// <remarks>
  12. /// Every phone application has one assigned 'tile' – a visual, dynamic
  13. /// representation of the application or its content. A tile displays in
  14. /// the Start screen if the end user has pinned it.
  15. ///
  16. /// This class members are thread safe.
  17. /// </remarks>
  18. public sealed class TilePushNotificationMessage : PushNotificationMessage
  19. {
  20. #region Constants
  21. /// <value>Calculated tile message headers size.</value>
  22. /// <remarks>This should ne updated if changing the protocol.</remarks>
  23. private const int TileMessageHeadersSize = 146;
  24. /// <value>Tile push notification message maximum payload size.</value>
  25. public const int MaxPayloadSize = MaxMessageSize - TileMessageHeadersSize;
  26. /// <value>The minimum <see cref="TilePushNotificationMessage.Count"/> value.</value>
  27. public const int MinCount = 0;
  28. /// <value>The maximum <see cref="TilePushNotificationMessage.Count"/> value.</value>
  29. public const int MaxCount = 99;
  30. /// <value>Windows phone target.</value>
  31. private const string WindowsPhoneTarget = "token";
  32. /// <value>A well formed structure of the tile notification message.</value>
  33. private const string PayloadString =
  34. "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
  35. "<wp:Notification xmlns:wp=\"WPNotification\">" +
  36. "<wp:Tile>" +
  37. "<wp:BackgroundImage>{0}</wp:BackgroundImage>" +
  38. "<wp:Count>{1}</wp:Count>" +
  39. "<wp:Title>{2}</wp:Title>" +
  40. "</wp:Tile>" +
  41. "</wp:Notification>";
  42. #endregion
  43. #region Fields
  44. /// <value>The phone's local path, or a remote path for the background image.</value>
  45. private Uri _backgroundImageUri;
  46. /// <value>An integer value to be displayed in the tile.</value>
  47. private int _count = MinCount;
  48. /// <value>The title text should be displayed in the tile.</value>
  49. private string _title;
  50. #endregion
  51. #region Properties
  52. /// <summary>
  53. /// Gets or sets the phone's local path, or a remote path for the background image.
  54. /// </summary>
  55. /// <remarks>
  56. /// If the uri references a remote resource, the maximum allowed size of the tile
  57. /// image is 80 KB, with a maximum download time of 15 seconds.
  58. /// </remarks>
  59. public Uri BackgroundImageUri
  60. {
  61. get
  62. {
  63. return _backgroundImageUri;
  64. }
  65. set
  66. {
  67. SafeSet(ref _backgroundImageUri, value);
  68. }
  69. }
  70. /// <summary>
  71. /// Gets or sets an integer value from 1 to 99 to be displayed in the tile, or 0 to clear count.
  72. /// </summary>
  73. public int Count
  74. {
  75. get
  76. {
  77. return _count;
  78. }
  79. set
  80. {
  81. if (value < MinCount || value > MaxCount)
  82. {
  83. throw new ArgumentOutOfRangeException(string.Format(Resources.CountValueIsNotValid, value, MinCount, MaxCount));
  84. }
  85. SafeSet(ref _count, value);
  86. }
  87. }
  88. /// <summary>
  89. /// Gets or sets the title text should be displayed in the tile. Null keeps the existing title.
  90. /// </summary>
  91. /// <remarks>
  92. /// The Title must fit a single line of text and should not be wider than the actual tile.
  93. /// Imperatively a good number of letters would be 18-20 characters long.
  94. /// </remarks>
  95. public string Title
  96. {
  97. get
  98. {
  99. return _title;
  100. }
  101. set
  102. {
  103. SafeSet(ref _title, value);
  104. }
  105. }
  106. /// <summary>
  107. /// Tile push notification message class id.
  108. /// </summary>
  109. protected override int NotificationClassId
  110. {
  111. get { return 1; }
  112. }
  113. #endregion
  114. #region Ctor
  115. /// <summary>
  116. /// Initializes a new instance of this type.
  117. /// </summary>
  118. /// <param name="sendPriority">The send priority of this message in the MPNS.</param>
  119. public TilePushNotificationMessage(MessageSendPriority sendPriority = MessageSendPriority.Normal)
  120. : base(sendPriority)
  121. {
  122. }
  123. #endregion
  124. #region Overrides
  125. /// <summary>
  126. /// Create the tile message payload.
  127. /// </summary>
  128. /// <returns>The message payload bytes.</returns>
  129. protected override byte[] OnCreatePayload()
  130. {
  131. var payloadString = string.Format(PayloadString, BackgroundImageUri, Count, Title);
  132. return Encoding.ASCII.GetBytes(payloadString);
  133. }
  134. /// <summary>
  135. /// Initialize the request with tile specific headers.
  136. /// </summary>
  137. /// <param name="request">The message request.</param>
  138. protected override void OnInitializeRequest(System.Net.HttpWebRequest request)
  139. {
  140. request.Headers[Headers.WindowsPhoneTarget] = WindowsPhoneTarget;
  141. }
  142. protected override void VerifyPayloadSize(byte[] payload)
  143. {
  144. if (payload.Length > MaxPayloadSize)
  145. {
  146. throw new ArgumentOutOfRangeException(string.Format(Resources.PayloadSizeIsTooBig, MaxPayloadSize));
  147. }
  148. }
  149. #endregion
  150. }
  151. }