PageRenderTime 61ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/WP7.1/Templates/VB/WPCloud.SQL.Mem/WindowsPhoneCloud.Web/Controllers/PushNotificationController.vb

#
Visual Basic | 240 lines | 171 code | 44 blank | 25 comment | 0 complexity | ba942da7e2dad44b78ab91614e32d21d MD5 | raw file
  1. ' ----------------------------------------------------------------------------------
  2. ' Microsoft Developer & Platform Evangelism
  3. '
  4. ' Copyright (c) Microsoft Corporation. All rights reserved.
  5. '
  6. ' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  7. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
  8. ' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  9. ' ----------------------------------------------------------------------------------
  10. ' The example companies, organizations, products, domain names,
  11. ' e-mail addresses, logos, people, places, and events depicted
  12. ' herein are fictitious. No association with any real company,
  13. ' organization, product, domain name, email address, logo, person,
  14. ' places, or events is intended or should be inferred.
  15. ' ----------------------------------------------------------------------------------
  16. Imports WindowsPhone.Recipes.Push.Messages
  17. Imports Microsoft.Samples.WindowsPhoneCloud.Web.UserAccountWrappers
  18. Imports Microsoft.Samples.WindowsPhoneCloud.Web.Models
  19. Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure
  20. Imports System.Text
  21. Imports System.Security.Cryptography.X509Certificates
  22. Imports System.Security.Authentication
  23. Imports System.Net.Sockets
  24. Imports System.Net.Security
  25. Imports System.IO
  26. Imports System.Globalization
  27. Imports System.Collections.Generic
  28. Namespace Controllers
  29. <CustomAuthorize(Roles:=PrivilegeConstants.AdminPrivilege)> _
  30. Public Class PushNotificationsController
  31. Inherits Controller
  32. Private Const DefaultPort As Integer = 2195
  33. Private ReadOnly pushUserEndpointsRepository As IPushUserEndpointsRepository
  34. #If ACS Then
  35. Private ReadOnly userRepository As IUserRepository
  36. Public Sub New()
  37. Me.New(Nothing, Nothing)
  38. End Sub
  39. <CLSCompliant(False)> _
  40. Public Sub New(ByVal pushUserEndpointsRepository As IPushUserEndpointsRepository, ByVal userRepository As IUserRepository)
  41. Dim context As SqlDataContext = Nothing
  42. If (pushUserEndpointsRepository Is Nothing) OrElse (userRepository Is Nothing) Then
  43. context = New SqlDataContext()
  44. End If
  45. Me.userRepository = If(userRepository, context)
  46. Me.pushUserEndpointsRepository = If(pushUserEndpointsRepository, context)
  47. End Sub
  48. Public Function Microsoft() As ActionResult
  49. Dim users = Me.pushUserEndpointsRepository.GetAllPushUsers().Select(Function(userId) New UserModel With {.UserId = userId, .UserName = Me.userRepository.GetUser(userId).Name})
  50. Return Me.View(users)
  51. End Function
  52. #Else
  53. Private ReadOnly membershipService As IMembershipService
  54. Public Sub New()
  55. Me.New(New SqlDataContext(), New AccountMembershipService())
  56. End Sub
  57. <CLSCompliant(False)> _
  58. Public Sub New(ByVal pushUserEndpointsRepository As IPushUserEndpointsRepository, ByVal membershipService As IMembershipService)
  59. Me.membershipService = membershipService
  60. Me.pushUserEndpointsRepository = pushUserEndpointsRepository
  61. End Sub
  62. Public Function Microsoft() As ActionResult
  63. Dim users = Me.pushUserEndpointsRepository.GetAllPushUsers().Select(Function(userId) New UserModel With {.UserId = userId, .UserName = Me.membershipService.GetUserByProviderUserKey(New Guid(userId)).UserName})
  64. Return Me.View(users)
  65. End Function
  66. #End If
  67. Public Function Apple() As ActionResult
  68. Return Me.View()
  69. End Function
  70. <HttpPost()> _
  71. Public Function SendMicrosoftToast(ByVal userId As String, ByVal message As String) As ActionResult
  72. If String.IsNullOrWhiteSpace(message) Then
  73. Return Me.Json("The notification message cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
  74. End If
  75. Dim resultList = New List(Of MessageSendResultLight)()
  76. Dim uris = Me.pushUserEndpointsRepository.GetPushUsersByName(userId).Select(Function(u) u.ChannelUri)
  77. Dim toast = New ToastPushNotificationMessage With {.SendPriority = MessageSendPriority.High, .Title = message}
  78. For Each uri In uris
  79. Dim messageResult = toast.SendAndHandleErrors(New Uri(uri))
  80. resultList.Add(messageResult)
  81. If messageResult.Status.Equals(MessageSendResultLight.Success) Then
  82. Me.QueueMessage(message, New Uri(uri))
  83. End If
  84. Next uri
  85. Return Me.Json(resultList, JsonRequestBehavior.AllowGet)
  86. End Function
  87. <HttpPost()> _
  88. Public Function SendMicrosoftTile(ByVal userId As String, ByVal message As String) As ActionResult
  89. If String.IsNullOrWhiteSpace(message) Then
  90. Return Me.Json("The notification message cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
  91. End If
  92. Dim resultList = New List(Of MessageSendResultLight)()
  93. Dim pushUserEndpointList = Me.pushUserEndpointsRepository.GetPushUsersByName(userId)
  94. For Each pushUserEndpoint In pushUserEndpointList
  95. pushUserEndpoint.TileCount += 1
  96. Dim tile = New TilePushNotificationMessage With {.SendPriority = MessageSendPriority.High, .Count = pushUserEndpoint.TileCount}
  97. Dim messageResult = tile.SendAndHandleErrors(New Uri(pushUserEndpoint.ChannelUri))
  98. resultList.Add(messageResult)
  99. If messageResult.Status.Equals(MessageSendResultLight.Success) Then
  100. Me.QueueMessage(message, New Uri(pushUserEndpoint.ChannelUri))
  101. Me.pushUserEndpointsRepository.UpdatePushUserEndpoint(pushUserEndpoint)
  102. End If
  103. Next pushUserEndpoint
  104. Return Me.Json(resultList, JsonRequestBehavior.AllowGet)
  105. End Function
  106. <HttpPost()> _
  107. Public Function SendMicrosoftRaw(ByVal userId As String, ByVal message As String) As ActionResult
  108. If String.IsNullOrWhiteSpace(message) Then
  109. Return Me.Json("The notification message cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
  110. End If
  111. Dim resultList = New List(Of MessageSendResultLight)()
  112. Dim uris = Me.pushUserEndpointsRepository.GetPushUsersByName(userId).Select(Function(u) u.ChannelUri)
  113. Dim raw = New RawPushNotificationMessage With {.SendPriority = MessageSendPriority.High, .RawData = Encoding.UTF8.GetBytes(message)}
  114. For Each uri In uris
  115. resultList.Add(raw.SendAndHandleErrors(New Uri(uri)))
  116. Next uri
  117. Return Me.Json(resultList, JsonRequestBehavior.AllowGet)
  118. End Function
  119. <HttpPost()> _
  120. Public Function SendAppleMessage(ByVal deviceId As String, ByVal message As String) As ActionResult
  121. If String.IsNullOrWhiteSpace(deviceId) Then
  122. Return Me.Json("The deviceId cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
  123. End If
  124. If String.IsNullOrWhiteSpace(message) Then
  125. Return Me.Json("The notification message cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
  126. End If
  127. ' Get the Apple Notification Service settings.
  128. Dim thumprint = ConfigReader.GetConfigValue("AppleNotificationService.Thumbprint")
  129. Dim host = ConfigReader.GetConfigValue("AppleNotificationService.Host")
  130. Dim port = 0
  131. If Not Integer.TryParse(ConfigReader.GetConfigValue("AppleNotificationService.Port"), NumberStyles.Integer, CultureInfo.InvariantCulture, port) Then
  132. port = DefaultPort
  133. End If
  134. Dim certCollection As X509Certificate2Collection = Nothing
  135. Try
  136. ' Load the Apple Notification Service certificate from the store.
  137. Dim clientCert = CertificateUtil.GetCertificate(StoreName.My, StoreLocation.LocalMachine, thumprint)
  138. certCollection = New X509Certificate2Collection(clientCert)
  139. Catch exception As Exception
  140. Dim errorMessage = String.Format(CultureInfo.InvariantCulture, "Error getting the X509 certificate from the store: {0}", exception.Message)
  141. Return Me.Json(errorMessage, JsonRequestBehavior.AllowGet)
  142. End Try
  143. Dim client As TcpClient = Nothing
  144. Try
  145. ' Open connection and connect.
  146. client = New TcpClient(host, port)
  147. Dim sslStream = New SslStream(client.GetStream(), False)
  148. sslStream.AuthenticateAsClient(host, certCollection, SslProtocols.Tls, False)
  149. Dim array() As Byte = Nothing
  150. Using memoryStream = New MemoryStream()
  151. Dim writer = New BinaryWriter(memoryStream)
  152. ' Construct the message.
  153. writer.Write(CByte(0)) ' Command
  154. writer.Write(CByte(0)) ' First byte of device ID length
  155. writer.Write(CByte(32)) ' Device id length
  156. ' Convert to hex and write to message.
  157. Dim deviceToken(deviceId.Length \ 2 - 1) As Byte
  158. For i As Integer = 0 To deviceToken.Length - 1
  159. deviceToken(i) = Byte.Parse(deviceId.Substring(i * 2, 2), NumberStyles.HexNumber)
  160. Next i
  161. writer.Write(deviceToken)
  162. ' Construct payload within JSON message framework.
  163. Dim payload = String.Format(CultureInfo.InvariantCulture, "{{""aps"":{{""alert"":""{0}"",""badge"":1}}}}", message)
  164. ' Write payload data.
  165. writer.Write(CByte(0)) ' First byte of payload length
  166. writer.Write(CByte(payload.Length)) ' Actual payload length
  167. Dim b1() As Byte = Encoding.UTF8.GetBytes(payload)
  168. writer.Write(b1)
  169. writer.Flush()
  170. array = memoryStream.ToArray()
  171. End Using
  172. ' Send across the wire.
  173. sslStream.Write(array)
  174. sslStream.Flush()
  175. Catch exception As AuthenticationException
  176. Dim errorMessage = String.Format(CultureInfo.InvariantCulture, "Error authenticating against APN: {0}", exception.Message)
  177. Return Me.Json(errorMessage, JsonRequestBehavior.AllowGet)
  178. Catch exception As Exception
  179. Dim errorMessage = String.Format(CultureInfo.InvariantCulture, "There was an error sending the notification message: {0}", exception.Message)
  180. Return Me.Json(errorMessage, JsonRequestBehavior.AllowGet)
  181. Finally
  182. ' Close the client connection.
  183. client.Close()
  184. End Try
  185. ' Success.
  186. Return Me.Json("Success", JsonRequestBehavior.AllowGet)
  187. End Function
  188. Private Sub QueueMessage(ByVal message As String, ByVal uri As Uri)
  189. Dim userEndpoint = Me.pushUserEndpointsRepository.GetPushUserEndpointByChannel(uri)
  190. If userEndpoint Is Nothing Then
  191. Throw New ArgumentException("The Channel URI is invalid", "uri")
  192. End If
  193. Me.pushUserEndpointsRepository.AddQueuedPushNotification(New QueuedPushNotification With {.PushUserEndpoint = userEndpoint, .ChannelUri = userEndpoint.ChannelUri, .Message = message})
  194. End Sub
  195. End Class
  196. End Namespace