[Win10应用开发] 使用 Windows 推送服务
2016-05-20 21:36
573 查看
前言
Windows 推送服务(WNS)也是 Win10 通知机制中的一种,今天与大家一起学习一下有关WNS的相关知识。使用 Windows 推送服务的前提是你需要有一个微软开发者账号,这样才能得到一些合法的密钥信息用于与WNS服务器完成通讯操作。附上一张关于消息推送原理图:
(来自 MSDN )
创建消息通道
使用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 认证
推送消息请求
了解OAuth认证的童鞋应该知道,我们应该具有一些合法的密钥信息,才能让目标服务器信任我们,然后我们才能进行真正的请求。而与WNS打交道时所有的密钥信息从哪来呢?这就需要微软开发者账号
登录微软开发者网站,打开你的仪表盘(DashBoard),如果你还没有应用就先创建一个应用。在应用详情里选择 服务 → 推送通知
打开下图中链接
看到了么?这就是我们需要的信息
在进行 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额外的内容。
参考链接
Windows 推送通知服务 (WNS) 概述推送通知服务请求和响应头
相关文章推荐
- CNN公式推导
- MySQL入门——数据表的修改1:往指定表中添加单列(字段)
- latex基本语法
- Zookeeper 快速理解
- Eclipse导出JAR包【java】
- Oracle的update语句优化研究 批量更新
- uva 10815 Andy's First Dictionary
- HDU 2065 "红色病毒"问题 (泰勒级数推导)(快速幂)
- 简析Instgram的搜索架构
- iOS 证书导出为p12,再导入其他电脑只能导入其中一个的解决方法
- STL 简单 deque 的实现
- HttpClient使用HttpGet获取Json数据出现部分数据乱码的情况
- ssh ip "WARING:REMOTE HOST IDENTIFICATION HAS CHANGED!"
- 拷贝构造函数详解http://blog.chinaunix.net/uid-21411227-id-1826742.html
- 第107课: Spark Streaming电商广告点击综合案例底层数据层的建模和编码实现(基于MySQL)
- 一、TotoiseSVN的基本使用方法
- Java线程学习笔记(一)
- sor文件分析软件
- 啦啦啦啦~Django1.96---编写第一个Django应用(6)
- 冒泡排序法