Form1.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //-----------------------------------------------------------------------------
  2. // Form1.cs
  3. //
  4. // Microsoft Advanced Technology Group
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Xml;
  12. using System.Runtime.Serialization.Formatters.Binary;
  13. namespace PushNotificationSender
  14. {
  15. /// <summary>
  16. /// This application contains a single window with UI to send test notifications
  17. /// to a URI supplied by the user.
  18. /// </summary>
  19. public partial class Form1 : Form
  20. {
  21. PushNotificationSender pushSender;
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. pushSender = new PushNotificationSender();
  26. pushSender.NotificationSendCompleted += PushSender_NotificationSendCompleted;
  27. }
  28. private delegate void UpdateServerResponseDelegate(string text);
  29. private void UpdateServerResponse(string text)
  30. {
  31. serverResponse.Text = text;
  32. }
  33. /// <summary>
  34. /// Callback that asynchronously updates the UI with the server's response or error message.
  35. /// </summary>
  36. private void PushSender_NotificationSendCompleted(PushNotificationCallbackArgs args)
  37. {
  38. string text = "Status Code: " + args.StatusCode.ToString() + Environment.NewLine +
  39. "TimeStamp: " + args.Timestamp.ToString() + Environment.NewLine +
  40. "Notification Type: " + args.NotificationType.ToString() + Environment.NewLine +
  41. "Notification Status: " + args.NotificationStatus + Environment.NewLine +
  42. "Device Status: " + args.DeviceConnectionStatus + Environment.NewLine +
  43. "Subscription Status: " + args.DeviceConnectionStatus;
  44. if (serverResponse.InvokeRequired)
  45. {
  46. serverResponse.Invoke(new UpdateServerResponseDelegate(UpdateServerResponse), new object[] { text });
  47. }
  48. else
  49. {
  50. UpdateServerResponse(text);
  51. }
  52. }
  53. /// <summary>
  54. /// The user has clicked the button to send a raw notification.
  55. /// </summary>
  56. private void buttonSendRaw_Click(object sender, EventArgs e)
  57. {
  58. // Send a raw notification. A raw notification is just a stream of bytes,
  59. // so the server and client must agree on a format. In this case,
  60. // it's just simple string of text.
  61. MemoryStream stream = new MemoryStream();
  62. BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8);
  63. writer.Write(rawText.Text);
  64. byte[] payload = stream.ToArray();
  65. try
  66. {
  67. Uri deviceUri = new Uri(phoneURI.Text);
  68. pushSender.SendRawNotification(deviceUri, payload);
  69. }
  70. catch (UriFormatException ex)
  71. {
  72. serverResponse.Text = ex.Message;
  73. }
  74. }
  75. /// <summary>
  76. /// The user has clicked the button to send a tile notification.
  77. /// </summary>
  78. private void buttonSendTile_Click(object sender, EventArgs e)
  79. {
  80. // Send the tile data from the UI.
  81. // The client application project contains two tile images:
  82. // - tile.png
  83. // - tileUpdate.png
  84. // Sending either of these as an image URI from the sender application
  85. // will cause the tile background to update on the phone if the user has
  86. // pinned the application shortcut to their quicklaunch menu.
  87. int count = Int32.Parse(tileCount.Text);
  88. tileCount.Text = (count + 1).ToString();
  89. try
  90. {
  91. Uri deviceUri = new Uri(phoneURI.Text);
  92. pushSender.SendTileNotification(deviceUri, tileTitle.Text, count, tileBackgroundImageUri.Text);
  93. }
  94. catch (UriFormatException ex)
  95. {
  96. serverResponse.Text = ex.Message;
  97. }
  98. }
  99. /// <summary>
  100. /// The user has clicked the button to send a toast notification.
  101. /// </summary>
  102. private void buttonSendToast_Click(object sender, EventArgs e)
  103. {
  104. // Send the toast data from the UI.
  105. try
  106. {
  107. Uri deviceUri = new Uri(phoneURI.Text);
  108. pushSender.SendToastNotification(deviceUri, toastText1.Text, toastText2.Text);
  109. }
  110. catch (UriFormatException ex)
  111. {
  112. serverResponse.Text = ex.Message;
  113. }
  114. catch (ArgumentNullException ex)
  115. {
  116. serverResponse.Text = ex.Message;
  117. }
  118. }
  119. }
  120. }