自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Windows Phone開發(fā)(45):推送通知大結(jié)局之Raw通知

移動開發(fā)
Raw推送通知比起前面兩種更簡單,因?yàn)樗鼪]有規(guī)范的格式,只要你向指定URI POST一個(gè)字節(jié)流數(shù)組就OK,也就是說,只要能變成byte[]的東西都可以發(fā)送。不過,不應(yīng)該發(fā)送過大的數(shù)據(jù),一般用于發(fā)送一些簡短的文本信息就行了,別想著用來發(fā)送文件!

為什么叫大結(jié)局呢?因?yàn)橥扑屯ㄖ?wù)就只有三種,前面扯了兩種,就剩下一種——Raw通知。

前面我們通過兩節(jié)的動手實(shí)驗(yàn),相信大家都知道了,推送通知其實(shí)并不復(fù)雜,為什么呢?你看到了的,不管是哪種方式,使用方法基本一樣,如果你不愿意寫代碼的話,完全可以把代碼Copy幾下就完事了,三種推送通知的實(shí)現(xiàn)代碼是一樣的,而僅僅是發(fā)送的內(nèi)容不同罷了。

Raw推送通知比起前面兩種更簡單,因?yàn)樗鼪]有規(guī)范的格式,只要你向指定URI POST一個(gè)字節(jié)流數(shù)組就OK,也就是說,只要能變成byte[]的東西都可以發(fā)送。不過,不應(yīng)該發(fā)送過大的數(shù)據(jù),一般用于發(fā)送一些簡短的文本信息就行了,別想著用來發(fā)送文件!!

嚴(yán)重提醒:要接收Raw通知,你的WP應(yīng)用程序必須在前臺運(yùn)行,不然是收不到的,之與Toast通知可不一樣,如果你的程序不在前臺運(yùn)行,推送的通知就會被XX掉。

好了,F(xiàn)話就不說了,開始操練吧。

先做發(fā)送通知的服務(wù)器端,這回就用WPF來做吧,界面我先截個(gè)TU。

這就是用WPF的好處,截圖中大家未必能看到窗口上用到哪些控件,設(shè)置了哪些屬性,但是,如果我把XAML一貼,我想大家就懂了。

  1. <Window x:Class="RawNtfServer.MainWindow"   
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  4.         Title="Raw通知服務(wù)器端" Height="350" Width="525">   
  5.     <Grid>   
  6.         <Grid.RowDefinitions>   
  7.             <RowDefinition Height="Auto" />   
  8.             <RowDefinition />   
  9.             <RowDefinition />   
  10.         </Grid.RowDefinitions>   
  11.         <Grid Grid.Row="0">   
  12.             <Grid.ColumnDefinitions>   
  13.                 <ColumnDefinition Width="auto"/>   
  14.                 <ColumnDefinition Width="*"/>   
  15.                 <ColumnDefinition Width="auto"/>   
  16.             </Grid.ColumnDefinitions>   
  17.             <TextBlock Grid.Column="0" Text="目標(biāo)URI:" VerticalAlignment="Center"/>   
  18.             <TextBox Name="txtUri" Grid.Column="1" Margin="2" Background="#FFD8E4E4"/>   
  19.             <Button Grid.Column="2" Padding="8,3,8,3" Margin="7,2,3,2" Content="發(fā)送" Click="OnSend"/>   
  20.         </Grid>   
  21.         <GroupBox Grid.Row="1" Header="發(fā)送內(nèi)容">   
  22.             <TextBox VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" Name="txtMsg" Background="#FFECF4D7" />   
  23.         </GroupBox>   
  24.         <GroupBox Grid.Row="2" Header="回應(yīng)內(nèi)容">   
  25.             <TextBox Name="txtResp" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" Background="#FFC9EDFA" />   
  26.         </GroupBox>   
  27.     </Grid>   
  28. </Window>   

好,前臺干好了,去搞搞后臺吧。

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5. using System.Windows;   
  6. using System.Windows.Controls;   
  7. using System.Windows.Data;   
  8. using System.Windows.Documents;   
  9. using System.Windows.Input;   
  10. using System.Windows.Media;   
  11. using System.Windows.Media.Imaging;   
  12. using System.Windows.Navigation;   
  13. using System.Windows.Shapes;   
  14. using System.Net;   
  15. using System.IO;   
  16. using System.Net.Mime;   
  17. namespace RawNtfServer   
  18. {   
  19.     /// <summary>   
  20.     /// MainWindow.xaml 的交互邏輯   
  21.     /// </summary>   
  22.     public partial class MainWindow : Window   
  23.     {   
  24.         public MainWindow()   
  25.         {   
  26.             InitializeComponent();   
  27.         }   
  28.         private void OnSend(object sender, RoutedEventArgs e)   
  29.         {   
  30.             if (txtUri.Text==""||txtMsg.Text=="")   
  31.             {   
  32.                 MessageBox.Show("請輸入必備的參數(shù)。"); return;   
  33.             }   
  34.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtUri.Text);   
  35.             request.Method = WebRequestMethods.Http.Post;   
  36.             request.ContentType = MediaTypeNames.Text.Plain;   
  37.             // HTTP標(biāo)頭:   
  38.             // X-NotificationClass:3   
  39.             // 3:立即發(fā)送   
  40.             // 13:450秒后發(fā)送   
  41.             // 23:900秒后發(fā)送   
  42.             request.Headers.Add("X-NotificationClass""3");   
  43.             byte[] buffer = Encoding.UTF8.GetBytes(txtMsg.Text);   
  44.             request.ContentLength = buffer.Length;   
  45.             using (Stream s = request.GetRequestStream())   
  46.             {   
  47.                 s.Write(buffer, 0, buffer.Length);   
  48.             }   
  49.             // 接收響應(yīng)   
  50.             HttpWebResponse response = (HttpWebResponse)request.GetResponse();   
  51.             string hds = "";   
  52.             foreach (string key in response.Headers.AllKeys)   
  53.             {   
  54.                 hds += key + " : " + response.Headers.Get(key) + "\r\n";   
  55.             }   
  56.             txtResp.Text = hds;   
  57.         }   
  58.     }   
  59. }   

有沒有覺得代碼很熟悉?和前兩節(jié)中的例子像不?

好了,服務(wù)器端Done,下面輪到WP客戶端了。

布局不用TU了,放心,無圖有真相。上XAML。

  1. <!--ContentPanel - 在此處放置其他內(nèi)容-->   
  2. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">   
  3.     <ListBox Name="lbMsg"/>   
  4. </Grid> 

簡單吧,就一個(gè)控件——ListBox,待會兒我們接受到的通知,就扔到它里面。

OK,看看后臺的C#代碼。

  1.     using System;   
  2.     using System.Collections.Generic;   
  3.     using System.Linq;   
  4.     using System.Net;   
  5.     using System.Windows;   
  6.     using System.Windows.Controls;   
  7.     using System.Windows.Documents;   
  8.     using System.Windows.Input;   
  9.     using System.Windows.Media;   
  10.     using System.Windows.Media.Animation;   
  11.     using System.Windows.Shapes;   
  12.     using Microsoft.Phone.Controls;   
  13.     using Microsoft.Phone.Notification;   
  14.     namespace WPClient   
  15.     {   
  16.         public partial class MainPage : PhoneApplicationPage   
  17.         {   
  18.             // 構(gòu)造函數(shù)   
  19.             public MainPage()   
  20.             {   
  21.                 InitializeComponent();   
  22.                 HttpNotificationChannel Channel = null;   
  23.                 string ChannelName = "raw";   
  24.                 Channel = HttpNotificationChannel.Find(ChannelName);   
  25.                 if (Channel==null)   
  26.                 {   
  27.                     Channel = new HttpNotificationChannel(ChannelName);   
  28.                     Channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);   
  29.                     Channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);   
  30.                     Channel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(Channel_HttpNotificationReceived);   
  31.                     Channel.Open();   
  32.                 }   
  33.                 else   
  34.                 {   
  35.                     Channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);   
  36.                     Channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);   
  37.                     Channel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(Channel_HttpNotificationReceived);   
  38.                     System.Diagnostics.Debug.WriteLine("URI: {0}", Channel.ChannelUri.ToString());   
  39.                 }   
  40.             }   
  41.             void Channel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)   
  42.             {   
  43.                 string msg = "";   
  44.                 using (System.IO.Stream stream=e.Notification.Body)   
  45.                 {   
  46.                     System.IO.StreamReader rd = new System.IO.StreamReader(stream, System.Text.Encoding.UTF8);   
  47.                     msg = rd.ReadToEnd();   
  48.                 }   
  49.                 Dispatcher.BeginInvoke(() =>   
  50.                     {   
  51. this.lbMsg.Items.Add(DateTime.Now.ToLongTimeString() + "  " + msg);   
  52.                     });   
  53.             }   
  54.             void Channel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)   
  55.             {   
  56.                 Dispatcher.BeginInvoke(() =>   
  57.                     {   
  58.                         MessageBox.Show(e.Message);   
  59.                     });   
  60.             }   
  61.             void Channel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)   
  62.             {   
  63.                 Dispatcher.BeginInvoke(() =>   
  64.                     {   
  65.                         System.Diagnostics.Debug.WriteLine("URI: {0}",e.ChannelUri.ToString());   
  66.                     });   
  67.             }   
  68.         }   
  69.     }   

避免有朋友說代碼看不懂,這回我是Ctrl + A后再貼出來的。

下面來執(zhí)行一下,首先運(yùn)行WP端,可以同時(shí)運(yùn)行,隨你喜歡。但至少要讓W(xué)P模擬器或手機(jī)收到云服務(wù)器分配的URI。

把這個(gè)URI復(fù)制,填到服務(wù)器端的窗口中,然后輸入你要發(fā)送的東東,點(diǎn)擊“發(fā)送”。

嗯,就是這樣用,應(yīng)該不復(fù)雜吧?

在收發(fā)消息的過程中,編碼時(shí)建議使用UTF-8,貌似這個(gè)不會有亂碼。

哈,牛就吹到這了,下一節(jié)我們玩一玩比較恐怖的東西——Socket。

責(zé)任編輯:閆佳明 來源: oschina
相關(guān)推薦

2013-04-25 14:15:53

Windows PhoWindows PhoWindows Pho

2012-08-16 11:31:30

Windows Pho

2013-04-25 14:05:20

Windows PhoWindows PhoWindows Pho

2020-03-04 14:35:34

戴爾

2020-03-11 16:00:55

戴爾

2013-11-27 10:52:48

360騰訊

2013-07-31 13:13:50

Windows PhoMVVM模式

2011-08-03 16:45:09

iPhone APNS 推送通知

2013-04-24 13:19:06

Windows Pho動畫DoubleAni

2013-04-24 13:31:59

Windows Pho動畫之ColorAni

2011-04-06 09:33:40

Push動互聯(lián)網(wǎng)

2013-04-19 17:11:02

Windows PhoWindows Pho

2013-04-24 13:43:10

Windows Pho動畫PointAnim

2019-06-11 17:53:35

技術(shù)

2010-08-01 15:16:41

Android

2013-07-31 13:36:07

Windows PhoVS調(diào)試技巧Windows Pho

2013-04-23 16:55:15

Windows Pho路徑之其它Geomet

2011-05-04 14:40:57

推送通知iOS

2025-03-11 08:25:00

網(wǎng)絡(luò)犯罪網(wǎng)絡(luò)詐騙惡意推送

2011-07-18 13:56:19

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號