Windows 推送服务(WNS)也是 Win10 通知机制中的一种,今天与你们一块儿学习一下有关WNS的相关知识。使用 Windows 推送服务的前提是你须要有一个微软开发者帐号,这样才能获得一些合法的密钥信息用于与WNS服务器完成通信操做。 windows
附上一张关于消息推送原理图:服务器
(来自 MSDN )app
使用 PushNotificationChannelManager
中的 CreatePushNotificationChannelForApplicationAsync()
建立 PushNotificationChannel
对象,经过订阅事件 PushNotificationReceived
接收 WNS 推送的消息。这里须要主意的是,PushNotificationChannel
内的 Url
属性。 WNS服务器怎么才能知道消息该推送给谁,就是依赖 Url
属性。学习
PushNotificationChannel pushNotificationChannel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); pushNotificationChannel.PushNotificationReceived += PushNotificationChannel_PushNotificationReceived;
private void PushNotificationChannel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args) { if (args.NotificationType == PushNotificationType.Toast) { ToastNotificationManager.CreateToastNotifier().Show(args.ToastNotification); } }
这个过程分为两步进行:网站
OAuth 认证url
推送消息请求spa
了解OAuth认证的童鞋应该知道,咱们应该具备一些合法的密钥信息,才能让目标服务器信任咱们,而后咱们才能进行真正的请求。而与WNS打交道时全部的密钥信息从哪来呢?这就须要微软开发者帐号 code
登陆微软开发者网站,打开你的仪表盘(DashBoard),若是你尚未应用就先建立一个应用。在应用详情里选择 服务 → 推送通知orm
打开下图中连接xml
看到了么?这就是咱们须要的信息
在进行 OAuth 认证咱们须要 SID 与 Client_Id ,下面咱们模拟一下 AppService 与 WNS OAuth认证过程
HttpClient httpClient = new HttpClient(); Dictionary<string, string> @params = new Dictionary<string, string> { {"grant_type", "client_credentials"}, { "client_id", "ms-app://************* SID ********************" }, {"client_secret", "/********** Client Id *************"}, {"scope", "notify.windows.com"} }; HttpFormUrlEncodedContent httpFormUrlEncodedContent = new HttpFormUrlEncodedContent(@params); httpFormUrlEncodedContent.Headers["Content-Type"] = "application/x-www-form-urlencoded"; var response = await httpClient.PostAsync(new Uri("https://login.live.com/accesstoken.srf"), httpFormUrlEncodedContent); string content = await response.Content.ReadAsStringAsync();
认证成功后,能够获得 access_token ,这样咱们的身份就合法了。
{ "access_token":"*****************/****************=", "token_type":"bearer" }
OAuth认证经过之后,就能够向WNS发送真正的推送请求了。下面咱们模拟一下 AppService 是如何给 Client 推送 Toast消息。
HttpClient httpClient2 = new HttpClient(); HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(ChannelUrl)); httpRequestMessage.Headers.Add("Authorization", "Bearer " + access_token); httpRequestMessage.Headers.Add("X-WNS-Type", "wns/toast"); string toastContent = @" <toast> <visual> <binding template='ToastGeneric'> <text>Hello World!</text> <text>This is the first Example!</text> </binding> </visual> </toast>"; HttpStringContent httpStringContent = new HttpStringContent(toastContent); httpStringContent.Headers["Content-Type"] = "text/xml"; httpStringContent.Headers["Content-Length"] = Encoding.UTF8.GetBytes(toastContent.ToCharArray()).Length.ToString(); httpRequestMessage.Content = httpStringContent; var response2 = await httpClient.SendRequestAsync(httpRequestMessage);
该注意的是 ChannelUrl
就是客户端在建立 PushNotificationChannel
对象中的 Url
的值。请求成功后,WNS就会根据Url
推送给与之对应的客户端。
到此为止,咱们已经实现一个远程推送的 DEMO。固然 WNS 里还有许多知识没有说起到,好比除了Toast
通知外,咱们能够推送Tile
等其余类型的通知。推荐你们去仔细阅读一下官方的说明文档,后续我也会补充WNS额外的内容。