using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using WindowsPhone.Recipes.Push.Messasges.Properties; namespace WindowsPhone.Recipes.Push.Messasges { /// /// Represents a toast push notification message. /// /// /// Toast notifications are system-wide notifications that do not disrupt /// the user workflow or require intervention to resolve. They are displayed /// at the top of the screen for ten seconds before disappearing. If the toast /// notification is tapped, the application that sent the toast notification /// will launch. A toast notification can be dismissed with a flick. /// /// This class members are thread safe. /// public sealed class ToastPushNotificationMessage : PushNotificationMessage { #region Constants /// Calculated toast message headers size. /// This should ne updated if changing the protocol. private const int ToastMessageHeadersSize = 146; /// Toast push notification message maximum payload size. public const int MaxPayloadSize = MaxMessageSize - ToastMessageHeadersSize; /// Windows phone target. private const string WindowsPhoneTarget = "toast"; /// A well formed structure of the toast notification message. private const string PayloadString = "" + "" + "" + "{0}" + "{1}" + "" + ""; #endregion #region Fields /// The bolded string that should be displayed immediately after the application icon. private string _title; /// The non-bolded string that should be displayed immediately after the Title. private string _subTitle; #endregion #region Properties /// /// Gets or sets a bolded string that should be displayed immediately after the application icon. /// public string Title { get { return _title; } set { SafeSet(ref _title, value); } } /// /// Gets or sets a non-bolded string that should be displayed immediately after the Title. /// public string SubTitle { get { return _subTitle; } set { SafeSet(ref _subTitle, value); } } /// /// Toast push notification message class id. /// protected override int NotificationClassId { get { return 2; } } #endregion #region Ctor /// /// Initializes a new instance of this type. /// /// The send priority of this message in the MPNS. public ToastPushNotificationMessage(MessageSendPriority sendPriority = MessageSendPriority.Normal) : base(sendPriority) { } #endregion #region Overrides /// /// Create the toast message payload. /// /// The message payload bytes. protected override byte[] OnCreatePayload() { var payloadString = string.Format(PayloadString, Title, SubTitle); return Encoding.ASCII.GetBytes(payloadString); } /// /// Initialize the request with toast specific headers. /// /// The message request. protected override void OnInitializeRequest(HttpWebRequest request) { request.Headers[Headers.WindowsPhoneTarget] = WindowsPhoneTarget; } protected override void VerifyPayloadSize(byte[] payload) { if (payload.Length > MaxPayloadSize) { throw new ArgumentOutOfRangeException(string.Format(Resources.PayloadSizeIsTooBig, MaxPayloadSize)); } } #endregion } }