PageRenderTime 78ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/PushSharp.WindowsPhone/WindowsPhonePushChannel.cs

https://github.com/mustafagenc/PushSharp
C# | 155 lines | 116 code | 36 blank | 3 comment | 19 complexity | d0724d965551592f7b0660ab446bd1c4 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using PushSharp.Common;
  7. namespace PushSharp.WindowsPhone
  8. {
  9. public class WindowsPhonePushChannel : PushChannelBase
  10. {
  11. WindowsPhonePushChannelSettings windowsPhoneSettings;
  12. public WindowsPhonePushChannel(WindowsPhonePushChannelSettings channelSettings, PushServiceSettings serviceSettings = null) : base(channelSettings, serviceSettings)
  13. {
  14. windowsPhoneSettings = channelSettings;
  15. }
  16. public override PlatformType PlatformType
  17. {
  18. get { return Common.PlatformType.WindowsPhone; }
  19. }
  20. protected override void SendNotification(Notification notification)
  21. {
  22. var wpNotification = notification as WindowsPhoneNotification;
  23. var wr = HttpWebRequest.Create(wpNotification.EndPointUrl) as HttpWebRequest;
  24. wr.ContentType = "text/xml";
  25. wr.Method = "POST";
  26. if (wpNotification.MessageID != null)
  27. wr.Headers.Add("X-MessageID", wpNotification.MessageID.ToString());
  28. if (wpNotification.NotificationClass.HasValue)
  29. {
  30. var immediateValue = 3;
  31. var mediumValue = 13;
  32. var slowValue = 23;
  33. if (wpNotification is WindowsPhoneToastNotification)
  34. {
  35. immediateValue = 2;
  36. mediumValue = 12;
  37. slowValue = 22;
  38. }
  39. else if (wpNotification is WindowsPhoneTileNotification)
  40. {
  41. immediateValue = 1;
  42. mediumValue = 11;
  43. slowValue = 21;
  44. }
  45. var val = immediateValue;
  46. if (wpNotification.NotificationClass.Value == BatchingInterval.Medium)
  47. val = mediumValue;
  48. else if (wpNotification.NotificationClass.Value == BatchingInterval.Slow)
  49. val = slowValue;
  50. wr.Headers.Add("X-NotificationClass", val.ToString());
  51. }
  52. if (wpNotification is WindowsPhoneToastNotification)
  53. wr.Headers.Add("X-WindowsPhone-Target", "toast");
  54. else if (wpNotification is WindowsPhoneTileNotification)
  55. wr.Headers.Add("X-WindowsPhone-Target", "Tile");
  56. var payload = wpNotification.PayloadToString();
  57. var data = Encoding.Default.GetBytes(payload);
  58. wr.ContentLength = data.Length;
  59. using (var rs = wr.GetRequestStream())
  60. {
  61. rs.Write(data, 0, data.Length);
  62. }
  63. try
  64. {
  65. wr.BeginGetResponse(new AsyncCallback(getResponseCallback), new object[] { wr, wpNotification });
  66. }
  67. catch (WebException wex)
  68. {
  69. //Handle different httpstatuses
  70. var status = ParseStatus(wex.Response as HttpWebResponse, wpNotification);
  71. HandleStatus(status);
  72. }
  73. }
  74. void getResponseCallback(IAsyncResult asyncResult)
  75. {
  76. //Good list of statuses:
  77. //http://msdn.microsoft.com/en-us/library/ff941100(v=vs.92).aspx
  78. var objs = (object[])asyncResult.AsyncState;
  79. var wr = (HttpWebRequest)objs[0];
  80. var wpNotification = (WindowsPhoneNotification)objs[1];
  81. var resp = wr.EndGetResponse(asyncResult) as HttpWebResponse;
  82. var status = ParseStatus(resp, wpNotification);
  83. HandleStatus(status);
  84. }
  85. WindowsPhoneMessageStatus ParseStatus(HttpWebResponse resp, WindowsPhoneNotification notification)
  86. {
  87. var result = new WindowsPhoneMessageStatus();
  88. result.Notification = notification;
  89. result.HttpStatus = resp.StatusCode;
  90. var wpStatus = resp.Headers["X-NotificationStatus"];
  91. var wpChannelStatus = resp.Headers["X-SubscriptionStatus"];
  92. var wpDeviceConnectionStatus = resp.Headers["X-DeviceConnectionStatus"];
  93. var messageID = resp.Headers["X-MessageID"];
  94. Guid msgGuid = Guid.NewGuid();
  95. if (Guid.TryParse(messageID, out msgGuid))
  96. result.MessageID = msgGuid;
  97. WPDeviceConnectionStatus devConStatus = WPDeviceConnectionStatus.InActive;
  98. Enum.TryParse<WPDeviceConnectionStatus>(wpDeviceConnectionStatus, out devConStatus);
  99. result.DeviceConnectionStatus = devConStatus;
  100. WPNotificationStatus notStatus = WPNotificationStatus.Dropped;
  101. Enum.TryParse<WPNotificationStatus>(wpStatus, out notStatus);
  102. result.NotificationStatus = notStatus;
  103. WPSubscriptionStatus subStatus = WPSubscriptionStatus.Expired;
  104. Enum.TryParse<WPSubscriptionStatus>(wpChannelStatus, out subStatus);
  105. result.SubscriptionStatus = subStatus;
  106. return result;
  107. }
  108. void HandleStatus(WindowsPhoneMessageStatus status)
  109. {
  110. if (status.HttpStatus == HttpStatusCode.OK
  111. && status.NotificationStatus == WPNotificationStatus.Received)
  112. {
  113. Events.RaiseNotificationSent(status.Notification);
  114. return;
  115. }
  116. Events.RaiseNotificationSendFailure(status.Notification, new WindowsPhoneNotificationSendFailureException(status));
  117. }
  118. }
  119. }