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