日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

[Win10应用开发] 使用 Windows 推送服务 (WNS)

發(fā)布時(shí)間:2023/12/9 windows 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Win10应用开发] 使用 Windows 推送服务 (WNS) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

Windows 推送服務(wù)(WNS)也是 Win10 通知機(jī)制中的一種,今天與大家一起學(xué)習(xí)一下有關(guān)WNS的相關(guān)知識(shí)。使用 Windows 推送服務(wù)的前提是你需要有一個(gè)微軟開發(fā)者賬號(hào),這樣才能得到一些合法的密鑰信息用于與WNS服務(wù)器完成通訊操作。

附上一張關(guān)于消息推送原理圖:

(來自 MSDN )

創(chuàng)建消息通道

使用 PushNotificationChannelManager 中的 CreatePushNotificationChannelForApplicationAsync() 創(chuàng)建 PushNotificationChannel 對(duì)象,通過訂閱事件 PushNotificationReceived 接收 WNS 推送的消息。這里需要主意的是,PushNotificationChannel 內(nèi)的 Url 屬性。 WNS服務(wù)器怎么才能知道消息該推送給誰,就是依賴 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);} }

推送請(qǐng)求

這個(gè)過程分為兩步進(jìn)行:

  • OAuth 認(rèn)證

  • 推送消息請(qǐng)求

了解OAuth認(rèn)證的童鞋應(yīng)該知道,我們應(yīng)該具有一些合法的密鑰信息,才能讓目標(biāo)服務(wù)器信任我們,然后我們才能進(jìn)行真正的請(qǐng)求。而與WNS打交道時(shí)所有的密鑰信息從哪來呢?這就需要微軟開發(fā)者賬號(hào)

登錄微軟開發(fā)者網(wǎng)站,打開你的儀表盤(DashBoard),如果你還沒有應(yīng)用就先創(chuàng)建一個(gè)應(yīng)用。在應(yīng)用詳情里選擇 服務(wù) → 推送通知

打開下圖中鏈接

看到了么?這就是我們需要的信息

在進(jìn)行 OAuth 認(rèn)證我們需要 SID 與 Client_Id ,下面我們模擬一下 AppService 與 WNS OAuth認(rèn)證過程

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();

認(rèn)證成功后,可以得到 access_token ,這樣我們的身份就合法了。

{"access_token":"*****************/****************=", "token_type":"bearer" }

OAuth認(rèn)證通過以后,就可以向WNS發(fā)送真正的推送請(qǐng)求了。下面我們模擬一下 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 就是客戶端在創(chuàng)建 PushNotificationChannel 對(duì)象中的 Url 的值。請(qǐng)求成功后,WNS就會(huì)根據(jù)Url推送給與之對(duì)應(yīng)的客戶端。

結(jié)束

到此為止,我們已經(jīng)實(shí)現(xiàn)一個(gè)遠(yuǎn)程推送的 DEMO。當(dāng)然 WNS 里還有許多知識(shí)沒有提及到,比如除了Toast通知外,我們可以推送Tile等其他類型的通知。推薦大家去仔細(xì)閱讀一下官方的說明文檔,后續(xù)我也會(huì)補(bǔ)充WNS額外的內(nèi)容。

參考鏈接

Windows 推送通知服務(wù) (WNS) 概述
推送通知服務(wù)請(qǐng)求和響應(yīng)頭

總結(jié)

以上是生活随笔為你收集整理的[Win10应用开发] 使用 Windows 推送服务 (WNS)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。