InboxView.xaml.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using System.IO;
  13. using System.Collections.ObjectModel;
  14. using System.Threading;
  15. using Microsoft.Phone.Shell;
  16. using Microsoft.Phone.Notification;
  17. using WindowsPhone.Recipes.Push.Client.Services;
  18. using WindowsPhone.Recipes.Push.Client.Controls;
  19. namespace WindowsPhone.Recipes.Push.Client.Views
  20. {
  21. public partial class InboxView : UserControl
  22. {
  23. #region Fields
  24. /// <value>Url of the GetTileImage REST service.</value>
  25. private static readonly string GetTileImageService = App.ServerAddress + "/ImageService/GetTileImage?uri={0}";
  26. private readonly ObservableCollection<string> _rawMessages = new ObservableCollection<string>();
  27. private ShellTileSchedule _tileSchedule;
  28. #endregion
  29. #region Properties
  30. public ObservableCollection<string> RawMessages
  31. {
  32. get { return _rawMessages; }
  33. }
  34. public IEnumerable<string> ServerImages
  35. {
  36. get
  37. {
  38. return new string[]
  39. {
  40. "number0.png",
  41. "number1.png",
  42. "number2.png",
  43. "number3.png",
  44. "number4.png",
  45. "number5.png",
  46. "number6.png",
  47. "number7.png",
  48. "number8.png",
  49. "number9.png"
  50. };
  51. }
  52. }
  53. public string SelectedServerImage
  54. {
  55. get { return (string)GetValue(SelectedServerImageProperty); }
  56. set { SetValue(SelectedServerImageProperty, value); }
  57. }
  58. // Using a DependencyProperty as the backing store for SelectedServerImage. This enables animation, styling, binding, etc...
  59. public static readonly DependencyProperty SelectedServerImageProperty =
  60. DependencyProperty.Register(
  61. "SelectedServerImage",
  62. typeof(string),
  63. typeof(InboxView),
  64. new PropertyMetadata("number0.png"));
  65. public IEnumerable<string> PushPatterns
  66. {
  67. get { return new string[] { "One Time", "Counter", "Ask to Pin", "Custom Tile", "Tile Schedule" }; }
  68. }
  69. public string PushPattern
  70. {
  71. get { return (string)GetValue(PushPatternProperty); }
  72. set { SetValue(PushPatternProperty, value); }
  73. }
  74. // Using a DependencyProperty as the backing store for PushPattern. This enables animation, styling, binding, etc...
  75. public static readonly DependencyProperty PushPatternProperty =
  76. DependencyProperty.Register(
  77. "PushPattern",
  78. typeof(string),
  79. typeof(InboxView),
  80. new PropertyMetadata(null, PushPatternChanged));
  81. private static void PushPatternChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  82. {
  83. var view = d as InboxView;
  84. if ("Tile Schedule".CompareTo(e.NewValue) == 0)
  85. {
  86. VisualStateManager.GoToState(view, "ScheduleView", false);
  87. }
  88. else
  89. {
  90. VisualStateManager.GoToState(view, "NormalView", false);
  91. }
  92. }
  93. public int Counter
  94. {
  95. get { return (int)GetValue(CounterProperty); }
  96. set { SetValue(CounterProperty, value); }
  97. }
  98. // Using a DependencyProperty as the backing store for Counter. This enables animation, styling, binding, etc...
  99. public static readonly DependencyProperty CounterProperty =
  100. DependencyProperty.Register(
  101. "Counter",
  102. typeof(int),
  103. typeof(InboxView),
  104. new PropertyMetadata(0));
  105. public string TileScheduleParameter
  106. {
  107. get { return (string)GetValue(TileScheduleParameterProperty); }
  108. set { SetValue(TileScheduleParameterProperty, value); }
  109. }
  110. // Using a DependencyProperty as the backing store for TileScheduleParameter. This enables animation, styling, binding, etc...
  111. public static readonly DependencyProperty TileScheduleParameterProperty =
  112. DependencyProperty.Register(
  113. "TileScheduleParameter",
  114. typeof(string),
  115. typeof(InboxView),
  116. new PropertyMetadata("number0.png"));
  117. #endregion
  118. public InboxView()
  119. {
  120. DataContext = this;
  121. InitializeComponent();
  122. Loaded += InboxView_Loaded;
  123. Unloaded += InboxView_Unloaded;
  124. UpdateServerInfo();
  125. }
  126. private void InboxView_Unloaded(object sender, RoutedEventArgs e)
  127. {
  128. UnregisterRawNotification();
  129. }
  130. private void InboxView_Loaded(object sender, RoutedEventArgs e)
  131. {
  132. RegisterRawNotification();
  133. }
  134. private void UpdateServerInfo()
  135. {
  136. try
  137. {
  138. var pushService = new PushServiceClient();
  139. pushService.GetServerInfoCompleted += (s1, e1) =>
  140. {
  141. try
  142. {
  143. pushService.CloseAsync();
  144. if (e1.Result != null)
  145. {
  146. Dispatcher.BeginInvoke(() =>
  147. {
  148. PushPattern = e1.Result.PushPattern;
  149. Counter = e1.Result.Counter;
  150. });
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. ex.Show();
  156. }
  157. };
  158. pushService.GetServerInfoAsync();
  159. }
  160. catch (Exception ex)
  161. {
  162. ex.Show();
  163. }
  164. }
  165. private void RegisterRawNotification()
  166. {
  167. var context = PushContext.Current;
  168. if (context != null)
  169. {
  170. context.RawNotification += context_RawNotification;
  171. }
  172. }
  173. private void UnregisterRawNotification()
  174. {
  175. var context = PushContext.Current;
  176. if (context != null)
  177. {
  178. context.RawNotification -= context_RawNotification;
  179. }
  180. }
  181. private void context_RawNotification(object sender, HttpNotificationEventArgs e)
  182. {
  183. try
  184. {
  185. using (var stream = new StreamReader(e.Notification.Body))
  186. {
  187. var rawMessage = stream.ReadToEnd();
  188. _rawMessages.Insert(0, rawMessage);
  189. if ("AskToPin".CompareTo(rawMessage) == 0)
  190. {
  191. AskToPin();
  192. }
  193. UpdateServerInfo();
  194. }
  195. }
  196. catch (Exception ex)
  197. {
  198. ex.Show();
  199. }
  200. }
  201. private void AskToPin()
  202. {
  203. NotificationBox.Show("Important", "Please pin your application to Start Screen so this application can work properly.");
  204. }
  205. private void ButtonSchedule_Click(object sender, RoutedEventArgs e)
  206. {
  207. _tileSchedule = new ShellTileSchedule();
  208. _tileSchedule.Recurrence = UpdateRecurrence.Interval;
  209. _tileSchedule.StartTime = DateTime.Now;
  210. _tileSchedule.Interval = UpdateInterval.EveryHour;
  211. _tileSchedule.RemoteImageUri = new Uri(string.Format(GetTileImageService, TileScheduleParameter));
  212. _tileSchedule.Start();
  213. }
  214. private void ButtonTestNow_Click(object sender, RoutedEventArgs e)
  215. {
  216. try
  217. {
  218. var pushService = new PushServiceClient();
  219. pushService.UpdateTileCompleted += (s1, e1) =>
  220. {
  221. try
  222. {
  223. pushService.CloseAsync();
  224. }
  225. catch (Exception ex)
  226. {
  227. ex.Show();
  228. }
  229. };
  230. pushService.UpdateTileAsync(PushContext.Current.NotificationChannel.ChannelUri, SelectedServerImage);
  231. }
  232. catch (Exception ex)
  233. {
  234. ex.Show();
  235. }
  236. }
  237. }
  238. }