PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Visual Basic | 267 lines | 193 code | 49 blank | 25 comment | 2 complexity | 1da1520948c2bab0076c45728568a64f 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.WindowsAzure.StorageClient
  18. Imports Microsoft.WindowsAzure
  19. Imports Microsoft.Samples.WindowsPhoneCloud.Web.UserAccountWrappers
  20. Imports Microsoft.Samples.WindowsPhoneCloud.Web.Models
  21. Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure
  22. Imports System.Security.Cryptography.X509Certificates
  23. Imports System.Security.Authentication
  24. Imports System.Net.Sockets
  25. Imports System.Net.Security
  26. Imports System.IO
  27. Imports System.Globalization
  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 cloudQueueClient As CloudQueueClient
  34. Private ReadOnly pushUserEndpointsRepository As IPushUserEndpointsRepository
  35. #If ACS Then
  36. Private ReadOnly userRepository As IUserRepository
  37. Public Sub New()
  38. Me.New(Nothing, New UserTablesServiceContext(), New UserTablesServiceContext())
  39. End Sub
  40. <CLSCompliant(False)> _
  41. Public Sub New(ByVal cloudQueueClient As CloudQueueClient, ByVal pushUserEndpointsRepository As IPushUserEndpointsRepository, ByVal userRepository As IUserRepository)
  42. If GetStorageAccountFromConfigurationSetting() Is Nothing Then
  43. If cloudQueueClient Is Nothing Then
  44. Throw New ArgumentNullException("cloudQueueClient", "Cloud Queue Client cannot be null if no configuration is loaded.")
  45. End If
  46. End If
  47. Me.cloudQueueClient = If(cloudQueueClient, GetStorageAccountFromConfigurationSetting().CreateCloudQueueClient())
  48. Me.userRepository = userRepository
  49. Me.pushUserEndpointsRepository = pushUserEndpointsRepository
  50. End Sub
  51. Public Function Microsoft() As ActionResult
  52. Dim users = Me.pushUserEndpointsRepository.GetAllPushUsers().Select(Function(userId) New UserModel With {.UserId = userId, .UserName = Me.userRepository.GetUser(userId).Name})
  53. Return Me.View(users)
  54. End Function
  55. #Else
  56. Private ReadOnly membershipService As IMembershipService
  57. Public Sub New()
  58. Me.New(Nothing, New UserTablesServiceContext(), New AccountMembershipService())
  59. End Sub
  60. <CLSCompliant(False)> _
  61. Public Sub New(ByVal cloudQueueClient As CloudQueueClient, ByVal pushUserEndpointsRepository As IPushUserEndpointsRepository, ByVal membershipService As IMembershipService)
  62. If GetStorageAccountFromConfigurationSetting() Is Nothing Then
  63. If cloudQueueClient Is Nothing Then
  64. Throw New ArgumentNullException("cloudQueueClient", "Cloud Queue Client cannot be null if no configuration is loaded.")
  65. End If
  66. End If
  67. Me.cloudQueueClient = If(cloudQueueClient, GetStorageAccountFromConfigurationSetting().CreateCloudQueueClient())
  68. Me.membershipService = membershipService
  69. Me.pushUserEndpointsRepository = pushUserEndpointsRepository
  70. End Sub
  71. Public Function Microsoft() As ActionResult
  72. Dim users = Me.pushUserEndpointsRepository.GetAllPushUsers().Select(Function(userId) New UserModel With {.UserId = userId, .UserName = Me.membershipService.GetUserByProviderUserKey(New Guid(userId)).UserName})
  73. Return Me.View(users)
  74. End Function
  75. #End If
  76. Public Function Apple() As ActionResult
  77. Return Me.View()
  78. End Function
  79. <HttpPost()> _
  80. Public Function SendMicrosoftToast(ByVal userId As String, ByVal message As String) As ActionResult
  81. If String.IsNullOrWhiteSpace(message) Then
  82. Return Me.Json("The notification message cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
  83. End If
  84. Dim resultList = New List(Of MessageSendResultLight)()
  85. Dim pushUserEndpoint = Me.pushUserEndpointsRepository.GetPushUsersByName(userId).FirstOrDefault()
  86. Dim uris = Me.pushUserEndpointsRepository.GetPushUsersByName(userId).Select(Function(u) u.ChannelUri)
  87. Dim toast = New ToastPushNotificationMessage With {.SendPriority = MessageSendPriority.High, .Title = message}
  88. For Each uri In uris
  89. Dim messageResult = toast.SendAndHandleErrors(New Uri(uri))
  90. resultList.Add(messageResult)
  91. If messageResult.Status.Equals(MessageSendResultLight.Success) Then
  92. Me.QueueMessage(pushUserEndpoint, message)
  93. End If
  94. Next uri
  95. Return Me.Json(resultList, JsonRequestBehavior.AllowGet)
  96. End Function
  97. <HttpPost()> _
  98. Public Function SendMicrosoftTile(ByVal userId As String, ByVal message As String) As ActionResult
  99. If String.IsNullOrWhiteSpace(message) Then
  100. Return Me.Json("The notification message cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
  101. End If
  102. Dim resultList = New List(Of MessageSendResultLight)()
  103. Dim pushUserEndpointList = Me.pushUserEndpointsRepository.GetPushUsersByName(userId)
  104. For Each pushUserEndpoint In pushUserEndpointList
  105. pushUserEndpoint.TileCount += 1
  106. Dim tile = New TilePushNotificationMessage With {.SendPriority = MessageSendPriority.High, .Count = pushUserEndpoint.TileCount}
  107. Dim messageResult = tile.SendAndHandleErrors(New Uri(pushUserEndpoint.ChannelUri))
  108. resultList.Add(messageResult)
  109. If messageResult.Status.Equals(MessageSendResultLight.Success) Then
  110. Me.QueueMessage(pushUserEndpoint, message)
  111. Me.pushUserEndpointsRepository.UpdatePushUserEndpoint(pushUserEndpoint)
  112. End If
  113. Next pushUserEndpoint
  114. Return Me.Json(resultList, JsonRequestBehavior.AllowGet)
  115. End Function
  116. <HttpPost()> _
  117. Public Function SendMicrosoftRaw(ByVal userId As String, ByVal message As String) As ActionResult
  118. If String.IsNullOrWhiteSpace(message) Then
  119. Return Me.Json("The notification message cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
  120. End If
  121. Dim resultList = New List(Of MessageSendResultLight)()
  122. Dim uris = Me.pushUserEndpointsRepository.GetPushUsersByName(userId).Select(Function(u) u.ChannelUri)
  123. Dim raw = New RawPushNotificationMessage With {.SendPriority = MessageSendPriority.High, .RawData = Encoding.UTF8.GetBytes(message)}
  124. For Each uri In uris
  125. resultList.Add(raw.SendAndHandleErrors(New Uri(uri)))
  126. Next uri
  127. Return Me.Json(resultList, JsonRequestBehavior.AllowGet)
  128. End Function
  129. <HttpPost()> _
  130. Public Function SendAppleMessage(ByVal deviceId As String, ByVal message As String) As ActionResult
  131. If String.IsNullOrWhiteSpace(deviceId) Then
  132. Return Me.Json("The deviceId cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
  133. End If
  134. If String.IsNullOrWhiteSpace(message) Then
  135. Return Me.Json("The notification message cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
  136. End If
  137. ' Get the Apple Notification Service settings.
  138. Dim thumprint = ConfigReader.GetConfigValue("AppleNotificationService.Thumbprint")
  139. Dim host = ConfigReader.GetConfigValue("AppleNotificationService.Host")
  140. Dim port = 0
  141. If Not Integer.TryParse(ConfigReader.GetConfigValue("AppleNotificationService.Port"), NumberStyles.Integer, CultureInfo.InvariantCulture, port) Then
  142. port = DefaultPort
  143. End If
  144. Dim certCollection As X509Certificate2Collection = Nothing
  145. Try
  146. ' Load the Apple Notification Service certificate from the store.
  147. Dim clientCert = CertificateUtil.GetCertificate(StoreName.My, StoreLocation.LocalMachine, thumprint)
  148. certCollection = New X509Certificate2Collection(clientCert)
  149. Catch exception As Exception
  150. Dim errorMessage = String.Format(CultureInfo.InvariantCulture, "Error getting the X509 certificate from the store: {0}", exception.Message)
  151. Return Me.Json(errorMessage, JsonRequestBehavior.AllowGet)
  152. End Try
  153. Dim client As TcpClient = Nothing
  154. Try
  155. ' Open connection and connect.
  156. client = New TcpClient(host, port)
  157. Dim sslStream = New SslStream(client.GetStream(), False)
  158. sslStream.AuthenticateAsClient(host, certCollection, SslProtocols.Tls, False)
  159. Dim array() As Byte = Nothing
  160. Using memoryStream = New MemoryStream()
  161. Dim writer = New BinaryWriter(memoryStream)
  162. ' Construct the message.
  163. writer.Write(CByte(0)) ' Command
  164. writer.Write(CByte(0)) ' First byte of device ID length
  165. writer.Write(CByte(32)) ' Device id length
  166. ' Convert to hex and write to message.
  167. Dim deviceToken(deviceId.Length \ 2 - 1) As Byte
  168. For i As Integer = 0 To deviceToken.Length - 1
  169. deviceToken(i) = Byte.Parse(deviceId.Substring(i * 2, 2), NumberStyles.HexNumber)
  170. Next i
  171. writer.Write(deviceToken)
  172. ' Construct payload within JSON message framework.
  173. Dim payload = String.Format(CultureInfo.InvariantCulture, "{{""aps"":{{""alert"":""{0}"",""badge"":1}}}}", message)
  174. ' Write payload data.
  175. writer.Write(CByte(0)) ' First byte of payload length
  176. writer.Write(CByte(payload.Length)) ' Actual payload length
  177. Dim b1() As Byte = Encoding.UTF8.GetBytes(payload)
  178. writer.Write(b1)
  179. writer.Flush()
  180. array = memoryStream.ToArray()
  181. End Using
  182. ' Send across the wire.
  183. sslStream.Write(array)
  184. sslStream.Flush()
  185. Catch exception As AuthenticationException
  186. Dim errorMessage = String.Format(CultureInfo.InvariantCulture, "Error authenticating against APN: {0}", exception.Message)
  187. Return Me.Json(errorMessage, JsonRequestBehavior.AllowGet)
  188. Catch exception As Exception
  189. Dim errorMessage = String.Format(CultureInfo.InvariantCulture, "There was an error sending the notification message: {0}", exception.Message)
  190. Return Me.Json(errorMessage, JsonRequestBehavior.AllowGet)
  191. Finally
  192. ' Close the client connection.
  193. client.Close()
  194. End Try
  195. ' Success.
  196. Return Me.Json("Success", JsonRequestBehavior.AllowGet)
  197. End Function
  198. Private Shared Function GetStorageAccountFromConfigurationSetting() As CloudStorageAccount
  199. Dim account As CloudStorageAccount = Nothing
  200. Try
  201. account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString")
  202. Catch e1 As InvalidOperationException
  203. account = Nothing
  204. End Try
  205. Return account
  206. End Function
  207. Private Sub QueueMessage(ByVal pushUser As PushUserEndpoint, ByVal message As String)
  208. Dim queueName = GetQueueName(pushUser.PartitionKey, pushUser.RowKey, pushUser.UserId)
  209. Dim queue = Me.cloudQueueClient.GetQueueReference(queueName)
  210. queue.CreateIfNotExist()
  211. queue.AddMessage(New CloudQueueMessage(message))
  212. End Sub
  213. Private Shared Function GetQueueName(ByVal applicationId As String, ByVal deviceId As String, ByVal userId As String) As String
  214. Dim uniqueName = String.Concat(applicationId, deviceId, userId)
  215. Return String.Concat("notification", uniqueName.GetHashCode())
  216. End Function
  217. End Class
  218. End Namespace