Windows Phone开发(44):推送通知第二集——磁贴通知

简介: 原文: Windows Phone开发(44):推送通知第二集——磁贴通知 前面我们说了第一个类型——Toast通知,这玩意儿不知大家是不是觉得很新鲜,以前玩.NET编程应该没接触过吧? 其实这东西绝对不复杂,只是刚接触的时候会有点莫名罢了,Toast通知和今天要说的磁贴通知,都有一个共同点,那就是格式都规定死了D。
原文: Windows Phone开发(44):推送通知第二集——磁贴通知

前面我们说了第一个类型——Toast通知,这玩意儿不知大家是不是觉得很新鲜,以前玩.NET编程应该没接触过吧?

其实这东西绝对不复杂,只是刚接触的时候会有点莫名罢了,Toast通知和今天要说的磁贴通知,都有一个共同点,那就是格式都规定死了D。

本质就是向特定的URI地址POST一个XML文档罢了,相信很多人都会,如果你还不会,真的,要补一补基础课了。

 多说无益,还是快点切入主题,开门见水吧。

首先,我们要知道我们在服务器端要POST什么样的XML文档,来,一起来看看。

<?xml version="1.0" encoding="utf-8" ?>
<wp:Notification xmlns:wp="WPNotification">
  <wp:Tile ID="导航URI">
    <wp:BackgroundImage>正面背景图片</wp:BackgroundImage>
    <wp:Count>计数器</wp:Count>
    <wp:Title>正面标题</wp:Title>
    <wp:BackBackgroundImage>背面背景图片</wp:BackBackgroundImage>
    <wp:BackTitle>背面标题</wp:BackTitle>
    <wp:BackContent>背面内容</wp:BackContent>
  </wp:Tile>
</wp:Notification>


前面关于磁贴的内容,大家有印象吧?

磁帖者,有正面的标题、背景图、计数器;背面有标题、背景图和正文。有印象就好,不用我打水口枪。

 

来吧,我们通过一个现场演练来体会体会吧。

 

先做服务器端,这回我选择用ASP.NET,不要告诉我你不会。

启动VS,建一个ASP.NET网站,然后,把default.aspx改造一下,如果你嫌生成的代码不好看,可以把文件删除,然后新建一个页面。

好了,页面布局嘛,我贴一下HTML就行了。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            目标URI:
            <asp:TextBox ID="txtURI" runat="server" Width="911px"></asp:TextBox>
        </div>
        <div>
            <table border="0">
                <tr>
                    <td>正面背景:</td>
                    <td>
                        <asp:TextBox ID="txtBackImg" runat="server" Width="316px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>正面标题:</td>
                    <td>
                        <asp:TextBox ID="txtTitle" runat="server" Width="316px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>计数:</td>
                    <td>
                        <asp:TextBox ID="txtCount" runat="server" Width="313px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>背面背景:</td>
                    <td>
                        <asp:TextBox ID="txtBackBackImg" runat="server" Width="316px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>背面标题:</td>
                    <td>
                        <asp:TextBox ID="txtBackTitle" runat="server" Width="321px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>背面正文:</td>
                    <td>
                        <asp:TextBox ID="txtBackContent" runat="server" Width="309px"></asp:TextBox>
                    </td>
                </tr>
            </table>
            <div style="margin-left:20px; margin-top:10px;">
                <asp:Button ID="btnSend" runat="server" Text="发送" onclick="btnSend_Click" /></div>
        </div>
        <div style=" margin-top:20px;">
            <asp:TextBox ID="txtRes" runat="server" Height="155px" TextMode="MultiLine" 
                Width="729px"></asp:TextBox>
        </div>
    </div>
    </form>
</body>
</html>


 

还是别少了后台代码。

/*
 <?xml version="1.0" encoding="utf-8" ?>
<wp:Notification xmlns:wp="WPNotification">
  <wp:Tile ID="导航URI">
    <wp:BackgroundImage>正面背景图片</wp:BackgroundImage>
    <wp:Count>计数器</wp:Count>
    <wp:Title>正面标题</wp:Title>
    <wp:BackBackgroundImage>背面背景图片</wp:BackBackgroundImage>
    <wp:BackTitle>背面标题</wp:BackTitle>
    <wp:BackContent>背面内容</wp:BackContent>
  </wp:Tile>
</wp:Notification>

 * 清除磁贴的属性值
 <?xml version="1.0" encoding="utf-8" ?>
<wp:Notification xmlns:wp="WPNotification">
  <wp:Tile ID="导航URI">
    <wp:BackgroundImage></wp:BackgroundImage>
    <wp:Count Action="Clear"></wp:Count>
    <wp:Title Action="Clear"></wp:Title>
    <wp:BackBackgroundImage Action="Clear"></wp:BackBackgroundImage>
    <wp:BackTitle Action="Clear"></wp:BackTitle>
    <wp:BackContent Action="Clear"></wp:BackContent>
  </wp:Tile>
</wp:Notification>

 * HTTP标头
 X-WindowsPhone-Target: token
X-NotificationClass:1
1 立即发送
11 450秒发送
21  900秒发送
 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net;
using System.Net.Mime;
using System.IO;
using System.Text;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtURI.Text);
        request.Method = WebRequestMethods.Http.Post;
        // 加上HTTP标头
        request.Headers.Add("X-WindowsPhone-Target", "token");
        request.Headers.Add("X-NotificationClass", "1");
        // 拼接内容,XML文档
        string Msg = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                     "<wp:Notification xmlns:wp=\"WPNotification\">" +
                        "<wp:Tile>" +
                            "<wp:BackgroundImage>" + txtBackImg.Text + "</wp:BackgroundImage>" +
                            "<wp:Count>" + txtCount.Text + "</wp:Count>" +
                            "<wp:Title>" + txtTitle.Text + "</wp:Title>" +
                            "<wp:BackBackgroundImage>" + txtBackBackImg.Text + "</wp:BackBackgroundImage>" +
                            "<wp:BackTitle>" + txtBackTitle.Text + "</wp:BackTitle>" +
                            "<wp:BackContent>" + txtBackContent.Text + "</wp:BackContent>" +
                        "</wp:Tile>" +
                      "</wp:Notification>";
        byte[] buffer = Encoding.UTF8.GetBytes(Msg);
        request.ContentType = MediaTypeNames.Text.Xml;
        // POST数据要记得设置内容长度
        request.ContentLength = buffer.Length;
        // 写入流
        using (Stream stream = request.GetRequestStream())
        {
            stream.Write(buffer, 0, buffer.Length);
        }
        // 接收回应
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        // 读出响应的HTTP头
        string headers = "";
        foreach (string key in response.Headers.AllKeys)
        {
            headers += key + " : " + response.Headers.Get(key) + "\r\n";
        }
        txtRes.Text = headers;
    }
}


 

补充一下,上面代码中,前面的注释我已经写上了,其实MSDN上都有,我想很多人不看,我说一下,如果你打算清除磁贴某些属性的值,如标题等,这可以用以下的XML文档。

<?xml version="1.0" encoding="utf-8" ?>
<wp:Notification xmlns:wp="WPNotification">
  <wp:Tile ID="导航URI">
    <wp:BackgroundImage></wp:BackgroundImage>
    <wp:Count Action="Clear"></wp:Count>
    <wp:Title Action="Clear"></wp:Title>
    <wp:BackBackgroundImage Action="Clear"></wp:BackBackgroundImage>
    <wp:BackTitle Action="Clear"></wp:BackTitle>
    <wp:BackContent Action="Clear"></wp:BackContent>
  </wp:Tile>
</wp:Notification>

重点就是,Action="Clear",但要注意,磁贴正面的背景图不能清除。

 

好,再来新建一个WP应用,这回要做客户端。

直接新建即可,XAML文档不用改,因为我们不需要界面设计了,直打开后台代码吧。

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 Channel_Name = "TileNoftification";
            // 在现有的通道里面找找,看能不能找着?
            Channel = HttpNotificationChannel.Find(Channel_Name);
            if (Channel == null)
            {
                // 找不到,那就新建一个呗
                Channel = new HttpNotificationChannel(Channel_Name);
                // 打开通道前要先注册事件处理,为什么?自己想一下吧
                // 就是因为ChannelUriUpdated事件,如不这样,
                // 当第一次获取URI时你就得不到更新通知
                Channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);
                // 出事了,总得向上级汇报一下吧?
                Channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);
                // 登记注册完毕,着手办喜事
                Channel.Open();
                // 办喜事别忘记了发请帖啊,调用BindToShellTile
                Channel.BindToShellTile();
            }
            else
            {
                // 如果找到了通道,还是要注册一下事件
                // 老夫妻也可以再拍一回婚纱照吧?
                Channel.ChannelUriUpdated+=new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);
                Channel.ErrorOccurred+=new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);
                // 把住址告诉人家,相册做好之后,数码冲印店送货上门
                System.Diagnostics.Debug.WriteLine("URI: {0}", Channel.ChannelUri.ToString());
            }
        }

        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());
                });
        }
    }
}


 

先动行WP端,当然,同时运行两个都可以了。

在“输出”窗口中,把这个URI复制到服务器端的网页上。

 

接着,按模拟器的“开始”按钮,来到“开始”屏幕,向右滑动,看到应用程序列表,在本应用程序上长按,从弹出的菜单中选“固定到开始屏幕”.

 

然后,回到服务器端页面,填好所有参数,点击“发送”。看结果。

 

 

都看到效果了?

图片可以自己准备,png格式,173*173,随便用画图工具搞两下就行了,只是为了测试,把图片加到项目后,设置以下属性就行了。

 

OK,大家照着上面的多练几次,一定有感觉的,我不想讲理论的东西,因为没什么用。

目录
相关文章
|
IDE 关系型数据库 开发工具
使用Visual Basic进行Windows窗体开发
【4月更文挑战第27天】本文介绍了使用Visual Basic进行Windows窗体(WinForms)开发的步骤,从搭建开发环境到创建、设计用户界面,再到编写事件驱动的代码和数据绑定。Visual Basic结合WinForms提供了一种易学易用的桌面应用开发方案。通过调试、优化、部署和维护,开发者可以构建专业应用程序。随着技术发展,掌握最新UI设计和开发工具对于保持竞争力至关重要。本文为初学者提供了基础指导,鼓励进一步探索和学习。
420 0
|
11月前
|
Ubuntu Linux Python
如何利用wsl-Ubuntu里conda用来给Windows的PyCharm开发
如何在WSL(Windows Subsystem for Linux)的Ubuntu环境中使用conda虚拟环境来为Windows上的PyCharm开发设置Python解释器。
1254 1
|
11月前
|
监控 关系型数据库 MySQL
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
441 0
|
Linux Apache C++
FFmpeg开发笔记(三十五)Windows环境给FFmpeg集成libsrt
该文介绍了如何在Windows环境下为FFmpeg集成SRT协议支持库libsrt。首先,需要安装Perl和Nasm,然后编译OpenSSL。接着,下载libsrt源码并使用CMake配置,生成VS工程并编译生成srt.dll和srt.lib。最后,将编译出的库文件和头文件按照特定目录结构放置,并更新环境变量,重新配置启用libsrt的FFmpeg并进行编译安装。该过程有助于优化直播推流的性能,减少卡顿问题。
333 2
FFmpeg开发笔记(三十五)Windows环境给FFmpeg集成libsrt
|
12月前
|
存储 安全 程序员
Windows任务管理器开发原理与实现
Windows任务管理器开发原理与实现
|
开发者 C# Windows
WPF与游戏开发:当桌面应用遇见游戏梦想——利用Windows Presentation Foundation打造属于你的2D游戏世界,从环境搭建到代码实践全面解析新兴开发路径
【8月更文挑战第31天】随着游戏开发技术的进步,WPF作为.NET Framework的一部分,凭借其图形渲染能力和灵活的UI设计,成为桌面游戏开发的新选择。本文通过技术综述和示例代码,介绍如何利用WPF进行游戏开发。首先确保安装最新版Visual Studio并创建WPF项目。接着,通过XAML设计游戏界面,并在C#中实现游戏逻辑,如玩家控制和障碍物碰撞检测。示例展示了创建基本2D游戏的过程,包括角色移动和碰撞处理。通过本文,WPF开发者可更好地理解并应用游戏开发技术,创造吸引人的桌面游戏。
686 0
|
开发者 iOS开发 C#
Uno Platform 入门超详细指南:从零开始教你打造兼容 Web、Windows、iOS 和 Android 的跨平台应用,轻松掌握 XAML 与 C# 开发技巧,快速上手示例代码助你迈出第一步
【8月更文挑战第31天】Uno Platform 是一个基于 Microsoft .NET 的开源框架,支持使用 C# 和 XAML 构建跨平台应用,适用于 Web(WebAssembly)、Windows、Linux、macOS、iOS 和 Android。它允许开发者共享几乎全部的业务逻辑和 UI 代码,同时保持原生性能。选择 Uno Platform 可以统一开发体验,减少代码重复,降低开发成本。安装时需先配置好 Visual Studio 或 Visual Studio for Mac,并通过 NuGet 或官网下载工具包。
1388 0
|
算法 Linux Windows
FFmpeg开发笔记(十七)Windows环境给FFmpeg集成字幕库libass
在Windows环境下为FFmpeg集成字幕渲染库libass涉及多个步骤,包括安装freetype、libxml2、gperf、fontconfig、fribidi、harfbuzz和libass。每个库的安装都需要下载源码、配置、编译和安装,并更新PKG_CONFIG_PATH环境变量。最后,重新配置并编译FFmpeg以启用libass及相关依赖。完成上述步骤后,通过`ffmpeg -version`确认libass已成功集成。
419 1
FFmpeg开发笔记(十七)Windows环境给FFmpeg集成字幕库libass
|
网络安全 C++ Windows
【Windows驱动开发】(主机)VS2017+(虚拟机)win10系统------双机调试
【Windows驱动开发】(主机)VS2017+(虚拟机)win10系统------双机调试
|
编解码 Linux Windows
FFmpeg开发笔记(十一)Windows环境给FFmpeg集成vorbis和amr
在Windows环境下,为FFmpeg集成音频编解码库,包括libogg、libvorbis和opencore-amr,涉及下载源码、配置、编译和安装步骤。首先,安装libogg,通过配置、make和make install命令完成,并更新PKG_CONFIG_PATH。接着,安装libvorbis,同样配置、编译和安装,并修改pkgconfig文件。之后,安装opencore-amr。最后,重新配置并编译FFmpeg,启用ogg和amr支持,通过ffmpeg -version检查是否成功。整个过程需确保环境变量设置正确,并根据路径添加相应库。
372 1
FFmpeg开发笔记(十一)Windows环境给FFmpeg集成vorbis和amr