為什麼叫大結局呢?因為推播通知服務就只有三種,前面扯了兩種,就剩下一種——Raw通知。
前面我們通過兩節的動手實驗,相信大家都知道了,推播通知其實並不複雜,為什麼呢?你看到了的,不管是哪種方式,使用方法基本一樣,如果你不願意寫代碼的話,完全可以把代碼Copy幾下就完事了,三種推播通知的實現代碼是一樣的,而僅僅是發送的內容不同罷了。
Raw推播通知比起前面兩種更簡單,因為它沒有規範的格式,只要你向指定URI POST一個位元組流數組就OK,也就是說,只要能變成byte[]的東西都可以發送。不過,不應該發送過大的資料,一般用於發送一些簡短的文本資訊就行了,別想著用來傳送檔案!!
嚴重提醒:要接收Raw通知,你的WP應用程式必須在前台運行,不然是收不到的,之與Toast通知可不一樣,如果你的程式不在前台運行,推送的通知就會被XX掉。
好了,F話就不說了,開始操練吧。
先做發送通知的伺服器端,這回就用WPF來做吧,介面我先截個TU。
這就是用WPF的好處,中大家未必能看到視窗上用到哪些控制項,設定了哪些屬性,但是,如果我把XAML一貼,我想大家就懂了。
<Window x:Class="RawNtfServer.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Raw通知伺服器端" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Grid Grid.Row="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="auto"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="目標URI:" VerticalAlignment="Center"/> <TextBox Name="txtUri" Grid.Column="1" Margin="2" Background="#FFD8E4E4"/> <Button Grid.Column="2" Padding="8,3,8,3" Margin="7,2,3,2" Content="發送" Click="OnSend"/> </Grid> <GroupBox Grid.Row="1" Header="發送內容"> <TextBox VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" Name="txtMsg" Background="#FFECF4D7" /> </GroupBox> <GroupBox Grid.Row="2" Header="回應內容"> <TextBox Name="txtResp" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" Background="#FFC9EDFA" /> </GroupBox> </Grid></Window>
好,前台幹好了,去搞搞後台吧。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Net;using System.IO;using System.Net.Mime;namespace RawNtfServer{ /// <summary> /// MainWindow.xaml 的互動邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void OnSend(object sender, RoutedEventArgs e) { if (txtUri.Text==""||txtMsg.Text=="") { MessageBox.Show("請輸入必備的參數。"); return; } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtUri.Text); request.Method = WebRequestMethods.Http.Post; request.ContentType = MediaTypeNames.Text.Plain; // HTTP標題: // X-NotificationClass:3 // 3:立即發送 // 13:450秒後發送 // 23:900秒後發送 request.Headers.Add("X-NotificationClass", "3"); byte[] buffer = Encoding.UTF8.GetBytes(txtMsg.Text); request.ContentLength = buffer.Length; using (Stream s = request.GetRequestStream()) { s.Write(buffer, 0, buffer.Length); } // 接收響應 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string hds = ""; foreach (string key in response.Headers.AllKeys) { hds += key + " : " + response.Headers.Get(key) + "\r\n"; } txtResp.Text = hds; } }}
有沒有覺得代碼很熟悉?和前兩節中的例子像不?
好了,伺服器端Done,下面輪到WP用戶端了。
布局不用TU了,放心,無圖有真相。上XAML。
<!--ContentPanel - 在此處放置其他內容--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <ListBox Name="lbMsg"/> </Grid>
簡單吧,就一個控制項——ListBox,待會兒我們接受到的通知,就扔到它裡面。
OK,看看背景C#代碼。
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Microsoft.Phone.Controls;using Microsoft.Phone.Notification;namespace WPClient{ public partial class MainPage : PhoneApplicationPage { // 建構函式 public MainPage() { InitializeComponent(); HttpNotificationChannel Channel = null; string ChannelName = "raw"; Channel = HttpNotificationChannel.Find(ChannelName); if (Channel==null) { Channel = new HttpNotificationChannel(ChannelName); Channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated); Channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred); Channel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(Channel_HttpNotificationReceived); Channel.Open(); } else { Channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated); Channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred); Channel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(Channel_HttpNotificationReceived); System.Diagnostics.Debug.WriteLine("URI: {0}", Channel.ChannelUri.ToString()); } } void Channel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e) { string msg = ""; using (System.IO.Stream stream=e.Notification.Body) { System.IO.StreamReader rd = new System.IO.StreamReader(stream, System.Text.Encoding.UTF8); msg = rd.ReadToEnd(); } Dispatcher.BeginInvoke(() => { this.lbMsg.Items.Add(DateTime.Now.ToLongTimeString() + " " + msg); }); } void Channel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e) { Dispatcher.BeginInvoke(() => { MessageBox.Show(e.Message); }); } void Channel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e) { Dispatcher.BeginInvoke(() => { System.Diagnostics.Debug.WriteLine("URI: {0}",e.ChannelUri.ToString()); }); } }}
避免有朋友說代碼看不懂,這回我是Ctrl + A後再貼出來的。
下面來執行一下,首先運行WP端,可以同時運行,隨你喜歡。,但至少要讓WP模擬器或手機收到雲端服務器分配的URI。
把這個URI複製,填到伺服器端的視窗中,然後輸入你要發送的東東,點擊“發送”。
嗯,就是這樣用,應該不複雜吧?
在收發訊息的過程中,編碼時建議使用UTF-8,貌似這個不會有亂碼。
哈,牛就吹到這了,下一節我們玩一玩比較恐怖的東西——Socket。