1. 程式人生 > >背水一戰 Windows 10 (121)

背水一戰 Windows 10 (121)

/*
 * 演示如何向 app 推送通知
 * 由於本例沒有上商店,所以本例是無法演示的,需要看演示效果的話執行一下自己寫的“打字通”的 /TypingGame/PushNotification/Sample.xaml,然後用其生成的 channel 地址在 /WebApi/Controllers/PushNotificationController.cs 推送通知
 *
 *
 * 注:
 * 關於推送通知服務請求和響應頭的詳細說明參見:https://msdn.microsoft.com/zh-cn/library/windows/apps/hh465435.aspx
 */

using System;
using System.IO; using System.Net; using System.Net.Http; using System.Text; using System.Web.Http; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Threading.Tasks; namespace WebApi.Controllers { public class PushNotificationController : ApiController { [HttpGet]
public async Task<HttpResponseMessage> Get() { // 向某個 app 推送通知的 channel 地址(通過客戶端的 PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync() 方法生成) string notifyUrl = "https://hk2.notify.windows.com/?token=AwYAAAANZzLsCX%2fl1aavCSQhi%2fdEBO5wdplj7S4a3o4t8wGSGo05hRE6VC7xEMCFtGDrVuV%2f9J2ItuVri1F4Z0YNjtbuCqf6LQvov0UE3%2flD1sP1poaS1Qp30UQ%2fWVKVUBCjPFuWFLuyuq7UuuTvJcCcQzey
"; // 在商店後臺的 dashboard 中的“Package SID”中可以找到此值(可以在 https://apps.dev.microsoft.com/ 中查詢) string sid = "ms-app://s-1-15-2-1792688850-3283391166-**********-**********-**********-1809961044-230289451"; // 在商店後臺的 dashboard 中的“Application Secrets”中可以找到此值(可以在 https://apps.dev.microsoft.com/ 中查詢) string secret = "koghs4zz*************S+5sEoqoNb4"; OAuthHelper oAuth = new OAuthHelper(); OAuthToken token = oAuth.GetAccessToken(secret, sid); HttpResponseMessage result = null; try { HttpClient httpClient = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, notifyUrl); // 推送訊息的型別:wns/toast | wns/badge | wns/tile | wns/raw request.Headers.Add("X-WNS-Type", "wns/toast"); // 設定 access-token request.Headers.Add("Authorization", String.Format("Bearer {0}", token.AccessToken)); // 需要推送的 toast 通知的內容 string toastXml = $@" <toast activationType='foreground' launch='PushNotification-Toast-Arguments'> <visual> <binding template='ToastGeneric'> <text>toast - title</text> <text>toast - content {DateTime.Now.ToString("mm:ss")}</text> </binding> </visual> </toast>"; // toast, tile, badge 為 text/xml; raw 為 application/octet-stream request.Content = new StringContent(toastXml, Encoding.UTF8, "text/xml"); HttpResponseMessage response = await httpClient.SendAsync(request); /* * 響應程式碼說明 * 200 - OK,WNS 已接收到通知 * 400 - 錯誤的請求 * 401 - 未授權,token 可能無效 * 403 - 已禁止,manifest 中的 identity 可能不對 * 404 - 未找到 * 405 - 方法不允許 * 406 - 無法接受 * 410 - 不存在,通道不存在或過期 * 413 - 請求實體太大,限制為 5000 位元組 * 500 - 內部伺服器錯誤 * 503 - 服務不可用 */ HttpStatusCode statusCode = response.StatusCode; result = new HttpResponseMessage { Content = new StringContent(statusCode.ToString(), Encoding.UTF8, "text/html") }; } catch (Exception ex) { result = new HttpResponseMessage { Content = new StringContent(ex.ToString(), Encoding.UTF8, "text/html"), StatusCode = HttpStatusCode.InternalServerError }; } return result; } } /* * 用於反序列化從 https://login.live.com/accesstoken.srf 獲取到的結果 */ [DataContract] public class OAuthToken { [DataMember(Name = "access_token")] public string AccessToken { get; set; } [DataMember(Name = "token_type")] public string TokenType { get; set; } } /* * 用於從 https://login.live.com/accesstoken.srf 做 OAuth 驗證的幫助類 */ public class OAuthHelper { /// <summary> /// 獲取 https://login.live.com/accesstoken.srf 的 OAuth 驗證的 access-token /// </summary> /// <param name="secret">在商店後臺的 dashboard 中的“Application Secrets”中可以找到此值(可以在 https://apps.dev.microsoft.com/ 中查詢)</param> /// <param name="sid">在商店後臺的 dashboard 中的“Package SID”中可以找到此值(可以在 https://apps.dev.microsoft.com/ 中查詢)</param> /// <returns></returns> public OAuthToken GetAccessToken(string secret, string sid) { var urlEncodedSecret = UrlEncode(secret); var urlEncodedSid = UrlEncode(sid); var body = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com", urlEncodedSid, urlEncodedSecret); string response; using (WebClient client = new WebClient()) { client.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); response = client.UploadString("https://login.live.com/accesstoken.srf", body); } return GetOAuthTokenFromJson(response); } private OAuthToken GetOAuthTokenFromJson(string jsonString) { using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString))) { var ser = new DataContractJsonSerializer(typeof(OAuthToken)); var oAuthToken = (OAuthToken)ser.ReadObject(ms); return oAuthToken; } } private static string UrlEncode(string str) { StringBuilder sb = new StringBuilder(); byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); for (int i = 0; i < byStr.Length; i++) { sb.Append(@"%" + Convert.ToString(byStr[i], 16)); } return (sb.ToString()); } } }

相關推薦

背水一戰 Windows 10 (121) - 後臺任務: 推送通知

target 聲明 測試 show null result href -c Coding [源碼下載] 背水一戰 Windows 10 (121) - 後臺任務: 推送通知 作者:webabcd介紹背水一戰 Windows 10 之 後臺任務 推送通知

背水一戰 Windows 10 (121)

/* * 演示如何向 app 推送通知 * 由於本例沒有上商店,所以本例是無法演示的,需要看演示效果的話執行一下自己寫的“打字通”的 /TypingGame/PushNotification/Sample.xaml,然後用其生成的 channel 地址在 /WebApi/Controllers/P

背水一戰 Windows 10 (54) - 控件(集合類): ItemsControl 的布局控件 - OrientedVirtualizingPanel, VirtualizingStackPanel, WrapGrid

schema 事件 panel http bili .text meven bind employee [源碼下載] 背水一戰 Windows 10 (54) - 控件(集合類): ItemsControl 的布局控件 - OrientedVirtualizingPane

背水一戰 Windows 10 (55) - 控件(集合類): ItemsControl - SemanticZoom, ISemanticZoomInformation

ack tty 傳遞 用戶 gef isp als pro msg [源碼下載] 背水一戰 Windows 10 (55) - 控件(集合類): ItemsControl - SemanticZoom, ISemanticZoomInformation 作者:webab

背水一戰 Windows 10 (58) - 控件(集合類): ListViewBase - ListView, GridView

aml dpa net sealed em1 .net http per tran [源碼下載] 背水一戰 Windows 10 (58) - 控件(集合類): ListViewBase - ListView, GridView 作者:webabcd介紹背水一戰 Win

背水一戰 Windows 10 (64) - 控件(WebView): 加載指定 HttpMethod 的請求, 自定義請求的 http header, app 與 js 的交互

如何 pan runt logs threading dto void msg def [源碼下載] 背水一戰 Windows 10 (64) - 控件(WebView): 加載指定 HttpMethod 的請求, 自定義請求的 http header, app 與 js

背水一戰 Windows 10 (37) - 控件(彈出類): MessageDialog, ContentDialog

異步操作 partial his list 文本 err desire secondary sta 原文:背水一戰 Windows 10 (37) - 控件(彈出類): MessageDialog, ContentDialog[源碼下載] 背水一戰 Windows 10

背水一戰 Windows 10 (34) - 控件(進度類): RangeBase, Slider, ProgressBar, ProgressRing

文本 div button 基類 lang block enable led template 原文:背水一戰 Windows 10 (34) - 控件(進度類): RangeBase, Slider, ProgressBar, ProgressRing[源碼下載] 背水

背水一戰 Windows 10 (24) - MVVM: 通過 Binding 或 x:Bind 結合 Command 實現,通過非 ButtonBase 觸發命令

block files grid collect gin 綁定 專註 ins ext 原文:背水一戰 Windows 10 (24) - MVVM: 通過 Binding 或 x:Bind 結合 Command 實現,通過非 ButtonBase 觸發命令[源碼下載] 背

背水一戰 Windows 10 (33) - 控件(選擇類): ListBox, RadioButton, CheckBox, ToggleSwitch

windows view join lms .get orm orien control flipview 原文:背水一戰 Windows 10 (33) - 控件(選擇類): ListBox, RadioButton, CheckBox, ToggleSwitch[源碼下

背水一戰 Windows 10 (27) - 控件(文本類): TextBlock

放大 part psi abcdefg -c 控件 ring pri stat 原文:背水一戰 Windows 10 (27) - 控件(文本類): TextBlock[源碼下載] 背水一戰 Windows 10 (27) - 控件(文本類): TextBlock 作者

背水一戰 Windows 10 (29) - 控件(文本類): RichTextBlock, RichTextBlockOverflow, RichEditBox

names 一個 示例 還原 rabl 工業 指定 鍵盤 mas 原文:背水一戰 Windows 10 (29) - 控件(文本類): RichTextBlock, RichTextBlockOverflow, RichEditBox[源碼下載] 背水一戰 Windows

背水一戰 Windows 10 (59) - 控件(媒體類): Image, MediaElement

ive http .so erb names targe pix target openxml 原文:背水一戰 Windows 10 (59) - 控件(媒體類): Image, MediaElement[源碼下載] 背水一戰 Windows 10 (59) - 控件(媒

背水一戰 Windows 10 (57) - 控件(集合類): ListViewBase - 增量加載, 分步繪制

fun 設置 enter priority protected 最大 卡頓 tinc abc 原文:背水一戰 Windows 10 (57) - 控件(集合類): ListViewBase - 增量加載, 分步繪制[源碼下載] 背水一戰 Windows 10 (57) -

背水一戰 Windows 10 (50) - 控件(集合類): ItemsControl - 基礎知識, 數據綁定, ItemsPresenter, GridViewItemPresenter, ListViewItemPresenter

需要 emc rectangle ems sources mic navi schema mark 原文:背水一戰 Windows 10 (50) - 控件(集合類): ItemsControl - 基礎知識, 數據綁定, ItemsPresenter, GridViewI

背水一戰 Windows 10 (56) - 控件(集合類): ListViewBase - 基礎知識, 拖動項

tar accepted 類型 idv .get footer ati model 變化 原文:背水一戰 Windows 10 (56) - 控件(集合類): ListViewBase - 基礎知識, 拖動項[源碼下載] 背水一戰 Windows 10 (56) - 控件

背水一戰 Windows 10 (67) - 控件(控件基類): DependencyObject - CoreDispatcher, 依賴屬性的設置與獲取, 依賴屬性的變化回調

protected getprop prop 依賴屬性 其他 優先級 dto type 核心 [源碼下載] 背水一戰 Windows 10 (67) - 控件(控件基類): DependencyObject - CoreDispatcher, 依賴屬性的設置與獲取, 依賴

背水一戰 Windows 10 (68) - 控件(控件基類): UIElement - Pointer 相關事件, Tap 相關事件, Key 相關事件, Focus 相關事件

tar release 關於 場景 npr 相對 capture soft etc [源碼下載] 背水一戰 Windows 10 (68) - 控件(控件基類): UIElement - Pointer 相關事件, Tap 相關事件, Key 相關事件, Focus 相關

背水一戰 Windows 10 (69) - 控件(控件基類): UIElement - Manipulate 手勢處理, 路由事件的註冊, 路由事件的冒泡, 命中測試的可見性

alex remove void express 簡單 let win 位置 edr [源碼下載] 背水一戰 Windows 10 (69) - 控件(控件基類): UIElement - Manipulate 手勢處理, 路由事件的註冊, 路由事件的冒泡, 命中測試的可

背水一戰 Windows 10 (76) - 控件(控件基類): Control - 基礎知識, 焦點相關, 運行時獲取 ControlTemplate 和 DataTemplate 中的元素

normal 焦點 colors 指針 是否 樣式 Go 系統 rgs 原文:背水一戰 Windows 10 (76) - 控件(控件基類): Control - 基礎知識, 焦點相關, 運行時獲取 ControlTemplate 和 DataTemplate 中的元素[源