成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

移動開發
Raw推送通知比起前面兩種更簡單,因為它沒有規范的格式,只要你向指定URI POST一個字節流數組就OK,也就是說,只要能變成byte[]的東西都可以發送。不過,不應該發送過大的數據,一般用于發送一些簡短的文本信息就行了,別想著用來發送文件!

為什么叫大結局呢?因為推送通知服務就只有三種,前面扯了兩種,就剩下一種——Raw通知。

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

Raw推送通知比起前面兩種更簡單,因為它沒有規范的格式,只要你向指定URI POST一個字節流數組就OK,也就是說,只要能變成byte[]的東西都可以發送。不過,不應該發送過大的數據,一般用于發送一些簡短的文本信息就行了,別想著用來發送文件!!

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

好了,F話就不說了,開始操練吧。

先做發送通知的服務器端,這回就用WPF來做吧,界面我先截個TU。

這就是用WPF的好處,截圖中大家未必能看到窗口上用到哪些控件,設置了哪些屬性,但是,如果我把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通知服務器端" 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="目標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="發送" Click="OnSend"/>   
  20.         </Grid>   
  21.         <GroupBox Grid.Row="1" Header="發送內容">   
  22.             <TextBox VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" Name="txtMsg" Background="#FFECF4D7" />   
  23.         </GroupBox>   
  24.         <GroupBox Grid.Row="2" Header="回應內容">   
  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("請輸入必備的參數。"); return;   
  33.             }   
  34.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtUri.Text);   
  35.             request.Method = WebRequestMethods.Http.Post;   
  36.             request.ContentType = MediaTypeNames.Text.Plain;   
  37.             // HTTP標頭:   
  38.             // X-NotificationClass:3   
  39.             // 3:立即發送   
  40.             // 13:450秒后發送   
  41.             // 23:900秒后發送   
  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.             // 接收響應   
  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. }   

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

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

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

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

簡單吧,就一個控件——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.             // 構造函數   
  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后再貼出來的。

下面來執行一下,首先運行WP端,可以同時運行,隨你喜歡。但至少要讓WP模擬器或手機收到云服務器分配的URI。

把這個URI復制,填到服務器端的窗口中,然后輸入你要發送的東東,點擊“發送”。

嗯,就是這樣用,應該不復雜吧?

在收發消息的過程中,編碼時建議使用UTF-8,貌似這個不會有亂碼。

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

責任編輯:閆佳明 來源: oschina
相關推薦

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

戴爾

2013-07-31 13:13:50

Windows PhoMVVM模式

2013-11-27 10:52:48

360騰訊

2011-08-03 16:45:09

iPhone APNS 推送通知

2020-03-11 16:00:55

戴爾

2013-04-24 13:31:59

Windows Pho動畫之ColorAni

2013-04-24 13:19:06

Windows Pho動畫DoubleAni

2011-04-06 09:33:40

Push動互聯網

2013-04-19 17:11:02

Windows PhoWindows Pho

2013-04-24 13:43:10

Windows Pho動畫PointAnim

2010-08-01 15:16:41

Android

2013-04-23 16:55:15

Windows Pho路徑之其它Geomet

2013-07-31 13:36:07

Windows PhoVS調試技巧Windows Pho

2010-04-21 17:07:54

Windows Pho

2013-07-30 12:37:56

Windows PhoWindows Pho

2025-03-11 08:25:00

網絡犯罪網絡詐騙惡意推送

2011-07-18 13:56:19

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 精品亚洲永久免费精品 | 在线黄色影院 | 欧美视频精品 | 日本黄色高清视频 | 丁香综合| 欧美日日 | www.日韩在线 | 国产高清在线观看 | 在线视频日韩 | 美女视频一区二区三区 | 久久久久黄色 | 国产婷婷色一区二区三区 | 国产视频一区二区三区四区五区 | 成人深夜福利 | 91免费高清视频 | 91资源在线 | 日韩欧美二区 | 亚洲精品女人久久久 | 精品久久国产 | 成人精品久久 | 欧洲一级毛片 | 亚洲欧美在线一区 | 国产成人精品一区二区三区在线观看 | 免费看国产a | 免费视频一区二区三区在线观看 | 亚洲午夜视频在线观看 | 黄a大片 | 成年人视频在线免费观看 | 综合网伊人 | 国产精品视频免费看 | 一区二区三区在线 | 国产一区二区三区四区三区四 | 成人一区精品 | 日韩欧美在线视频一区 | 成人精品鲁一区一区二区 | 成人国产综合 | 久久久国产一区二区三区 | 亚洲视频在线免费 | 国产精品国产精品国产专区不片 | 欧美亚洲视频 | 亚洲国产一区二区视频 |