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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

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

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

前言

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) 概述
推送通知服務請求和響應頭

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。