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

Windows Phone開發(44):推送通知第二集之磁貼通知

移動開發
重點就是,Action="Clear",但要注意,磁貼正面的背景圖不能清除。好,再來新建一個WP應用,這回要做客戶端。直接新建即可,XAML文檔不用改,因為我們不需要界面設計了,直打開后臺代碼吧。

前面我們說了***個類型——Toast通知,這玩意兒不知大家是不是覺得很新鮮,以前玩.NET編程應該沒接觸過吧?

其實這東西絕對不復雜,只是剛接觸的時候會有點莫名罷了,Toast通知和今天要說的磁貼通知,都有一個共同點,那就是格式都規定死了D。

本質就是向特定的URI地址POST一個XML文檔罷了,相信很多人都會,如果你還不會,真的,要補一補基礎課了。

多說無益,還是快點切入主題,開門見水吧。

首先,我們要知道我們在服務器端要POST什么樣的XML文檔,來,一起來看看。

  1. <?xml version="1.0" encoding="utf-8" ?>   
  2. <wp:Notification xmlns:wp="WPNotification">   
  3.   <wp:Tile ID="導航URI">   
  4.     <wp:BackgroundImage>正面背景圖片</wp:BackgroundImage>   
  5.     <wp:Count>計數器</wp:Count>   
  6.     <wp:Title>正面標題</wp:Title>   
  7.     <wp:BackBackgroundImage>背面背景圖片</wp:BackBackgroundImage>   
  8.     <wp:BackTitle>背面標題</wp:BackTitle>   
  9.     <wp:BackContent>背面內容</wp:BackContent>   
  10.   </wp:Tile>   
  11. </wp:Notification> 

前面關于磁貼的內容,大家有印象吧?

磁帖者,有正面的標題、背景圖、計數器;背面有標題、背景圖和正文。有印象就好,不用我打水口槍。

來吧,我們通過一個現場演練來體會體會吧。

先做服務器端,這回我選擇用ASP.NET,不要告訴我你不會。

啟動VS,建一個ASP.NET網站,然后,把default.aspx改造一下,如果你嫌生成的代碼不好看,可以把文件刪除,然后新建一個頁面。

好了,頁面布局嘛,我貼一下HTML就行了。

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>   
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  3. <html xmlns="http://www.w3.org/1999/xhtml">   
  4. <head runat="server">   
  5.     <title></title>   
  6. </head>   
  7. <body>   
  8.     <form id="form1" runat="server">   
  9.     <div>   
  10.         <div>   
  11.             目標URI:   
  12.             <asp:TextBox ID="txtURI" runat="server" Width="911px"></asp:TextBox>   
  13.         </div>   
  14.         <div>   
  15.             <table border="0">   
  16.                 <tr>   
  17.                     <td>正面背景:</td>   
  18.                     <td>   
  19.                         <asp:TextBox ID="txtBackImg" runat="server" Width="316px"></asp:TextBox></td>   
  20.                 </tr>   
  21.                 <tr>   
  22.                     <td>正面標題:</td>   
  23.                     <td>   
  24.                         <asp:TextBox ID="txtTitle" runat="server" Width="316px"></asp:TextBox>   
  25.                     </td>   
  26.                 </tr>   
  27.                 <tr>   
  28.                     <td>計數:</td>   
  29.                     <td>   
  30.                         <asp:TextBox ID="txtCount" runat="server" Width="313px"></asp:TextBox>   
  31.                     </td>   
  32.                 </tr>   
  33.                 <tr>   
  34.                     <td>背面背景:</td>   
  35.                     <td>   
  36.                         <asp:TextBox ID="txtBackBackImg" runat="server" Width="316px"></asp:TextBox>   
  37.                     </td>   
  38.                 </tr>   
  39.                 <tr>   
  40.                     <td>背面標題:</td>   
  41.                     <td>   
  42.                         <asp:TextBox ID="txtBackTitle" runat="server" Width="321px"></asp:TextBox>   
  43.                     </td>   
  44.                 </tr>   
  45.                 <tr>   
  46.                     <td>背面正文:</td>   
  47.                     <td>   
  48.                         <asp:TextBox ID="txtBackContent" runat="server" Width="309px"></asp:TextBox>   
  49.                     </td>   
  50.                 </tr>   
  51.             </table>   
  52.             <div style="margin-left:20px; margin-top:10px;">   
  53.                 <asp:Button ID="btnSend" runat="server" Text="發送" onclick="btnSend_Click" /></div>   
  54.         </div>   
  55.         <div style=" margin-top:20px;">   
  56.             <asp:TextBox ID="txtRes" runat="server" Height="155px" TextMode="MultiLine"    
  57.                 Width="729px"></asp:TextBox>   
  58.         </div>   
  59.     </div>   
  60.     </form>   
  61. </body>   
  62. </html>   

還是別少了后臺代碼。

  1. /*  
  2.  <?xml version="1.0" encoding="utf-8" ?>  
  3. <wp:Notification xmlns:wp="WPNotification">  
  4.   <wp:Tile ID="導航URI">  
  5.     <wp:BackgroundImage>正面背景圖片</wp:BackgroundImage>  
  6.     <wp:Count>計數器</wp:Count>  
  7.     <wp:Title>正面標題</wp:Title>  
  8.     <wp:BackBackgroundImage>背面背景圖片</wp:BackBackgroundImage>  
  9.     <wp:BackTitle>背面標題</wp:BackTitle>  
  10.     <wp:BackContent>背面內容</wp:BackContent>  
  11.   </wp:Tile>  
  12. </wp:Notification>  
  13.  * 清除磁貼的屬性值  
  14.  <?xml version="1.0" encoding="utf-8" ?>  
  15. <wp:Notification xmlns:wp="WPNotification">  
  16.   <wp:Tile ID="導航URI">  
  17.     <wp:BackgroundImage></wp:BackgroundImage>  
  18.     <wp:Count Action="Clear"></wp:Count>  
  19.     <wp:Title Action="Clear"></wp:Title>  
  20.     <wp:BackBackgroundImage Action="Clear"></wp:BackBackgroundImage>  
  21.     <wp:BackTitle Action="Clear"></wp:BackTitle>  
  22.     <wp:BackContent Action="Clear"></wp:BackContent>  
  23.   </wp:Tile>  
  24. </wp:Notification>  
  25.  * HTTP標頭  
  26.  X-WindowsPhone-Target: token  
  27. X-NotificationClass:1  
  28. 1 立即發送  
  29. 11 450秒發送  
  30. 21  900秒發送  
  31.  */   
  32. using System;   
  33. using System.Collections.Generic;   
  34. using System.Linq;   
  35. using System.Web;   
  36. using System.Web.UI;   
  37. using System.Web.UI.WebControls;   
  38. using System.Net;   
  39. using System.Net.Mime;   
  40. using System.IO;   
  41. using System.Text;   
  42. public partial class _Default : System.Web.UI.Page   
  43. {   
  44.     protected void Page_Load(object sender, EventArgs e)   
  45.     {   
  46.     }   
  47.     protected void btnSend_Click(object sender, EventArgs e)   
  48.     {   
  49.         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtURI.Text);   
  50.         request.Method = WebRequestMethods.Http.Post;   
  51.         // 加上HTTP標頭   
  52.         request.Headers.Add("X-WindowsPhone-Target""token");   
  53.         request.Headers.Add("X-NotificationClass""1");   
  54.         // 拼接內容,XML文檔   
  55.         string Msg = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +   
  56.                      "<wp:Notification xmlns:wp=\"WPNotification\">" +   
  57.                         "<wp:Tile>" +   
  58.                             "<wp:BackgroundImage>" + txtBackImg.Text + "</wp:BackgroundImage>" +   
  59.                             "<wp:Count>" + txtCount.Text + "</wp:Count>" +   
  60.                             "<wp:Title>" + txtTitle.Text + "</wp:Title>" +   
  61.                             "<wp:BackBackgroundImage>" + txtBackBackImg.Text + "</wp:BackBackgroundImage>" +   
  62.                             "<wp:BackTitle>" + txtBackTitle.Text + "</wp:BackTitle>" +   
  63.                             "<wp:BackContent>" + txtBackContent.Text + "</wp:BackContent>" +   
  64.                         "</wp:Tile>" +   
  65.                       "</wp:Notification>";   
  66.         byte[] buffer = Encoding.UTF8.GetBytes(Msg);   
  67.         request.ContentType = MediaTypeNames.Text.Xml;   
  68.         // POST數據要記得設置內容長度   
  69.         request.ContentLength = buffer.Length;   
  70.         // 寫入流   
  71.         using (Stream stream = request.GetRequestStream())   
  72.         {   
  73.             stream.Write(buffer, 0, buffer.Length);   
  74.         }   
  75.         // 接收回應   
  76.         HttpWebResponse response = (HttpWebResponse)request.GetResponse();   
  77.         // 讀出響應的HTTP頭   
  78.         string headers = "";   
  79.         foreach (string key in response.Headers.AllKeys)   
  80.         {   
  81.             headers += key + " : " + response.Headers.Get(key) + "\r\n";   
  82.         }   
  83.         txtRes.Text = headers;   
  84.     }   
  85. }   

補充一下,上面代碼中,前面的注釋我已經寫上了,其實MSDN上都有,我想很多人不看,我說一下,如果你打算清除磁貼某些屬性的值,如標題等,這可以用以下的XML文檔。

  1. <?xml version="1.0" encoding="utf-8" ?>   
  2. <wp:Notification xmlns:wp="WPNotification">   
  3.   <wp:Tile ID="導航URI">   
  4.     <wp:BackgroundImage></wp:BackgroundImage>   
  5.     <wp:Count Action="Clear"></wp:Count>   
  6.     <wp:Title Action="Clear"></wp:Title>   
  7.     <wp:BackBackgroundImage Action="Clear"></wp:BackBackgroundImage>   
  8.     <wp:BackTitle Action="Clear"></wp:BackTitle>   
  9.     <wp:BackContent Action="Clear"></wp:BackContent>   
  10.   </wp:Tile>   
  11. </wp:Notification>  

重點就是,Action="Clear",但要注意,磁貼正面的背景圖不能清除。

好,再來新建一個WP應用,這回要做客戶端。

直接新建即可,XAML文檔不用改,因為我們不需要界面設計了,直打開后臺代碼吧。

  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.             // 通道名,隨便弄一個,不要與其它應用程序重復   
  24.             string Channel_Name = "TileNoftification";   
  25.             // 在現有的通道里面找找,看能不能找著?   
  26.             Channel = HttpNotificationChannel.Find(Channel_Name);   
  27.             if (Channel == null)   
  28.             {   
  29.                 // 找不到,那就新建一個唄   
  30.                 Channel = new HttpNotificationChannel(Channel_Name);   
  31.                 // 打開通道前要先注冊事件處理,為什么?自己想一下吧   
  32.                 // 就是因為ChannelUriUpdated事件,如不這樣,   
  33.                 // 當***次獲取URI時你就得不到更新通知   
  34.                 Channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);   
  35.                 // 出事了,總得向上級匯報一下吧?   
  36.                 Channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);   
  37.                 // 登記注冊完畢,著手辦喜事   
  38.                 Channel.Open();   
  39.                 // 辦喜事別忘記了發請帖啊,調用BindToShellTile   
  40.                 Channel.BindToShellTile();   
  41.             }   
  42.             else   
  43.             {   
  44.                 // 如果找到了通道,還是要注冊一下事件   
  45.                 // 老夫妻也可以再拍一回婚紗照吧?   
  46.                 Channel.ChannelUriUpdated+=new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);   
  47.                 Channel.ErrorOccurred+=new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);   
  48.                 // 把住址告訴人家,相冊做好之后,數碼沖印店送貨上門   
  49.                 System.Diagnostics.Debug.WriteLine("URI: {0}", Channel.ChannelUri.ToString());   
  50.             }   
  51.         }   
  52.         void Channel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)   
  53.         {   
  54.             // 向上級匯報一下錯誤   
  55.             Dispatcher.BeginInvoke(() =>   
  56.                 {   
  57.                     MessageBox.Show(e.Message);   
  58.                 });   
  59.         }   
  60.         void Channel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)   
  61.         {   
  62.             // 搬家了記得通知一下大家新住址   
  63.             Dispatcher.BeginInvoke(() =>   
  64.                 {   
  65.                     System.Diagnostics.Debug.WriteLine("URI: {0}", e.ChannelUri.ToString());   
  66.                 });   
  67.         }   
  68.     }   
  69. }   

先動行WP端,當然,同時運行兩個都可以了。

在“輸出”窗口中,把這個URI復制到服務器端的網頁上。

接著,按模擬器的“開始”按鈕,來到“開始”屏幕,向右滑動,看到應用程序列表,在本應用程序上長按,從彈出的菜單中選“固定到開始屏幕”.

然后,回到服務器端頁面,填好所有參數,點擊“發送”。看結果。

都看到效果了?

圖片可以自己準備,png格式,173*173,隨便用畫圖工具搞兩下就行了,只是為了測試,把圖片加到項目后,設置以下屬性就行了。

OK,大家照著上面的多練幾次,一定有感覺的,我不想講理論的東西,因為沒什么用。

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

2013-04-25 15:15:41

Windows PhoWindows PhoWindows Pho

2013-04-25 14:05:20

Windows PhoWindows PhoWindows Pho

2012-08-16 11:31:30

Windows Pho

2013-02-19 09:04:32

Windows 8開發

2021-06-11 07:30:30

并發高并發內存

2013-04-17 11:21:59

Windows PhoWindows Pho

2020-03-27 18:00:37

微軟Windows 10操作系統

2022-05-13 14:16:05

云計算

2018-03-26 21:31:30

深度學習

2013-07-31 13:13:50

Windows PhoMVVM模式

2021-06-16 09:49:14

Windows 11微軟動態磁貼

2011-08-03 16:45:09

iPhone APNS 推送通知

2022-03-18 12:27:21

Android開發者功能

2011-04-06 09:33:40

Push動互聯網

2013-04-24 13:31:59

Windows Pho動畫之ColorAni

2013-04-24 13:19:06

Windows Pho動畫DoubleAni

2011-09-21 17:07:57

WP7

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
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 精品国产乱码久久久久久88av | 91亚洲国产 | 成人av一区二区亚洲精 | 中文字幕亚洲区一区二 | 国产精品一区二区免费 | 在线观看日韩 | 中文字幕在线免费观看 | 国产aⅴ爽av久久久久久久 | aaaaaaa片毛片免费观看 | 久久男人| 精品一区二区三区四区视频 | 精品视频国产 | 亚洲麻豆| 91高清视频 | 91精品国产色综合久久 | 日韩精品一区二区三区在线观看 | 9191av| 91看片官网| 一级做a爰片性色毛片16 | 亚洲久久在线 | 亚洲一区在线日韩在线深爱 | 午夜激情小视频 | 日韩电影一区 | 夜色www国产精品资源站 | 久草资源 | 偷拍自拍第一页 | 狠狠干美女 | av官网在线 | 在线中文视频 | 91视频观看 | 97视频成人| 51ⅴ精品国产91久久久久久 | 欧美日韩三级视频 | 欧美在线综合 | 亚洲国内精品 | 欧美一区二区三区免费在线观看 | 国产精品一区二区福利视频 | 国产成人精品综合 | 日本一区二区不卡视频 | 亚洲一区二区在线播放 | 在线精品国产 |