OneTimePushPatternViewModel.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel.Composition;
  6. using System.Windows.Input;
  7. using System.IO;
  8. using Microsoft.Win32;
  9. using WindowsPhone.Recipes.Push.Messasges;
  10. using WindowsPhone.Recipes.Push.Server.Models;
  11. namespace WindowsPhone.Recipes.Push.Server.ViewModels
  12. {
  13. /// <summary>
  14. /// Represents the One Time push notification pattern.
  15. /// </summary>
  16. /// <remarks>
  17. /// This is the simplest push notification pattern of just pushing single time message
  18. /// to a registered client.
  19. /// </remarks>
  20. [Export(typeof(PushPatternViewModel)), PartCreationPolicy(CreationPolicy.Shared)]
  21. internal sealed class OneTimePushPatternViewModel : PushPatternViewModel
  22. {
  23. #region Properties
  24. /// <summary>
  25. /// Gets or sets a value indicating if tile message should be sent.
  26. /// </summary>
  27. public bool IsTileEnabled { get; set; }
  28. /// <summary>
  29. /// Gets or sets a value indicating if toast message should be sent.
  30. /// </summary>
  31. public bool IsToastEnabled { get; set; }
  32. /// <summary>
  33. /// Gets or sets a value indicating if raw message should be sent.
  34. /// </summary>
  35. public bool IsRawEnabled { get; set; }
  36. #endregion
  37. #region Ctor
  38. /// <summary>
  39. /// Initialize new instance of this type with defaults.
  40. /// </summary>
  41. public OneTimePushPatternViewModel()
  42. {
  43. InitializeDefaults();
  44. }
  45. #endregion
  46. #region Overrides
  47. /// <summary>
  48. /// Depends on what message was selected, send all subscribers zero or all three push message types (Tile, Toast, Raw).
  49. /// </summary>
  50. protected override void OnSend()
  51. {
  52. var messages = new List<PushNotificationMessage>();
  53. if (IsTileEnabled)
  54. {
  55. // Prepare a tile push notification message.
  56. messages.Add(new TilePushNotificationMessage(MessageSendPriority.High)
  57. {
  58. BackgroundImageUri = BackgroundImageUri,
  59. Count = Count,
  60. Title = Title
  61. });
  62. }
  63. if (IsToastEnabled)
  64. {
  65. // Prepare a toast push notification message.
  66. messages.Add(new ToastPushNotificationMessage(MessageSendPriority.High)
  67. {
  68. Title = ToastTitle,
  69. SubTitle = ToastSubTitle
  70. });
  71. }
  72. if (IsRawEnabled)
  73. {
  74. // Prepare a raw push notification message.
  75. messages.Add(new RawPushNotificationMessage(MessageSendPriority.High)
  76. {
  77. RawData = Encoding.ASCII.GetBytes(RawMessage)
  78. });
  79. }
  80. foreach (var subscriber in PushService.Subscribers)
  81. {
  82. messages.ForEach(m => m.SendAsync(subscriber.ChannelUri, Log, Log));
  83. }
  84. }
  85. #endregion
  86. #region Privates
  87. private void InitializeDefaults()
  88. {
  89. DisplayName = "One Time";
  90. Description = "This is the simplest push notification pattern of just pushing single time message to a registered client.";
  91. Count = 1;
  92. Title = "Game Update";
  93. ToastTitle = Title;
  94. ToastSubTitle = "Game has been released";
  95. RawMessage = ToastSubTitle;
  96. IsTileEnabled = true;
  97. IsToastEnabled = true;
  98. IsRawEnabled = true;
  99. }
  100. #endregion
  101. }
  102. }