ToastPushNotificationMessage.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using WindowsPhone.Recipes.Push.Messasges.Properties;
  7. namespace WindowsPhone.Recipes.Push.Messasges
  8. {
  9. /// <summary>
  10. /// Represents a toast push notification message.
  11. /// </summary>
  12. /// <remarks>
  13. /// Toast notifications are system-wide notifications that do not disrupt
  14. /// the user workflow or require intervention to resolve. They are displayed
  15. /// at the top of the screen for ten seconds before disappearing. If the toast
  16. /// notification is tapped, the application that sent the toast notification
  17. /// will launch. A toast notification can be dismissed with a flick.
  18. ///
  19. /// This class members are thread safe.
  20. /// </remarks>
  21. public sealed class ToastPushNotificationMessage : PushNotificationMessage
  22. {
  23. #region Constants
  24. /// <value>Calculated toast message headers size.</value>
  25. /// <remarks>This should ne updated if changing the protocol.</remarks>
  26. private const int ToastMessageHeadersSize = 146;
  27. /// <value>Toast push notification message maximum payload size.</value>
  28. public const int MaxPayloadSize = MaxMessageSize - ToastMessageHeadersSize;
  29. /// <value>Windows phone target.</value>
  30. private const string WindowsPhoneTarget = "toast";
  31. /// <value>A well formed structure of the toast notification message.</value>
  32. private const string PayloadString =
  33. "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
  34. "<wp:Notification xmlns:wp=\"WPNotification\">" +
  35. "<wp:Toast>" +
  36. "<wp:Text1>{0}</wp:Text1>" +
  37. "<wp:Text2>{1}</wp:Text2>" +
  38. "</wp:Toast>" +
  39. "</wp:Notification>";
  40. #endregion
  41. #region Fields
  42. /// <value>The bolded string that should be displayed immediately after the application icon.</value>
  43. private string _title;
  44. /// <value>The non-bolded string that should be displayed immediately after the Title.</value>
  45. private string _subTitle;
  46. #endregion
  47. #region Properties
  48. /// <summary>
  49. /// Gets or sets a bolded string that should be displayed immediately after the application icon.
  50. /// </summary>
  51. public string Title
  52. {
  53. get
  54. {
  55. return _title;
  56. }
  57. set
  58. {
  59. SafeSet(ref _title, value);
  60. }
  61. }
  62. /// <summary>
  63. /// Gets or sets a non-bolded string that should be displayed immediately after the Title.
  64. /// </summary>
  65. public string SubTitle
  66. {
  67. get
  68. {
  69. return _subTitle;
  70. }
  71. set
  72. {
  73. SafeSet(ref _subTitle, value);
  74. }
  75. }
  76. /// <summary>
  77. /// Toast push notification message class id.
  78. /// </summary>
  79. protected override int NotificationClassId
  80. {
  81. get { return 2; }
  82. }
  83. #endregion
  84. #region Ctor
  85. /// <summary>
  86. /// Initializes a new instance of this type.
  87. /// </summary>
  88. /// <param name="sendPriority">The send priority of this message in the MPNS.</param>
  89. public ToastPushNotificationMessage(MessageSendPriority sendPriority = MessageSendPriority.Normal)
  90. : base(sendPriority)
  91. {
  92. }
  93. #endregion
  94. #region Overrides
  95. /// <summary>
  96. /// Create the toast message payload.
  97. /// </summary>
  98. /// <returns>The message payload bytes.</returns>
  99. protected override byte[] OnCreatePayload()
  100. {
  101. var payloadString = string.Format(PayloadString, Title, SubTitle);
  102. return Encoding.ASCII.GetBytes(payloadString);
  103. }
  104. /// <summary>
  105. /// Initialize the request with toast specific headers.
  106. /// </summary>
  107. /// <param name="request">The message request.</param>
  108. protected override void OnInitializeRequest(HttpWebRequest request)
  109. {
  110. request.Headers[Headers.WindowsPhoneTarget] = WindowsPhoneTarget;
  111. }
  112. protected override void VerifyPayloadSize(byte[] payload)
  113. {
  114. if (payload.Length > MaxPayloadSize)
  115. {
  116. throw new ArgumentOutOfRangeException(string.Format(Resources.PayloadSizeIsTooBig, MaxPayloadSize));
  117. }
  118. }
  119. #endregion
  120. }
  121. }