为什么使用推送通知服务

Windows Phone执行模型决定只有一个第三方的应用程序可以在前台运行,应用程序不能再后台运行,不断的往Cloud拉数据。微软提供推送通知服务(Push Notification)给第三方应用程序取得更新通知的消息。由于服务器能够主动的发起通信,因此可以有效的降低手机电池的消耗。

Windows Phone 的推送通知的完整权威描述见MSDN文档描述见:http://msdn.microsoft.com/zh-cn/library/ff402537(v=vs.92).aspx。

本节内容

  • Windows Phone 的推送通知概述

  • 接收 Windows Phone 的推送通知

  • 发送 Windows Phone 的推送通知

  • Windows Phone 的推送通知服务响应代码

  • 如何设置 Windows Phone 的回调注册请求

  • 设置已验证的 Web 服务以发送 Windows Phone 的推送通知

  • 如何发送和接收 Windows Phone 的 Toast 通知

  • 如何发送和接收 Windows Phone 的磁贴通知

  • 如何发送和接收 Windows Phone 的 Raw 通知

推送消息的过程MS的描述是:

上图显示了手机上运行的客户端应用程序如何从推送客户端服务 (1) 请求推送通知 URI。然后,推送客户端服务与 Microsoft 推送通知服务 (MPNS) 协商并向客户端应用程序(2 和 3)返回一个通知 URI。之后,客户端应用程序将此 URI 发送给云服务 (4)。当 Web 服务有要发送到客户端应用程序的信息时,该服务使用此 URI 向 Microsoft 推送通知服务 (5) 发送推送通知,Microsoft 推送通知服务又将此推送通知发送给在 Windows Phone 设备 (6) 上运行的应用程序。

根据推送通知的格式以及连接到通知的负载,信息作为原始数据发送到应用程序、应用程序的磁贴在视觉上得到更新或显示 Toast 通知。发送推送通知之后,Microsoft 推送通知服务向您的 Web 服务发送一个响应代码,指示此通知已接收并且下次有机会会发送到设备。但是,Microsoft 推送通知服务不提供将推送通知从 Web 服务发送到设备的端到端通信。

Jake Lin的描述是:

使用规范:

windows phone 7目前只允许15个第三方应用程序使用推送通知服务;

询问用户是否使用推送通知服务;

为用户提供取消订阅的选项。

s消息类型:

Raw Notification:

可以发送任何格式的数据;

应用程序可以根据需要加工数据;

应用程序相关的通知消息;

★只有在应用程序运行时才发送。

Toast Notification:

发送的数据为指定的xml格式;

★如果应用程序正在运行,内容发送到应用程序中;

★如果应用程序不在运行,弹出toast消息框显示消息:

App图标加上两个文本描述;

打断用户当前操作,但是是临时的;

用户可以点击进行跟踪。

Tile Notification:

发送的数据为指定的xml格式;

★不会往应用程序进行发送;

★如果用户把应用程序Pin to Start,那么更新数据会发送到start screen 的tile里面:

包含三个属性,背景、标题和计数器;

每个属性都有固定的格式和位置;

可以使用其中的属性,不一定三个属性一起用。

示例程序示例(Raw):手机客户端代码:

View Code

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 推送通知服务{public partial class MainPage : PhoneApplicationPage    {// Constructor        public MainPage()        {            InitializeComponent();

//Holds the push channel that is created or found.            HttpNotificationChannel pushchannel;//The name of our push channel.            string channelName = "RawSampleChannel";//Try to find the push channel            pushchannel = HttpNotificationChannel.Find(channelName);// If the channel was not found, then create a new connection to the push service.            if (pushchannel == null)            {                pushchannel = new HttpNotificationChannel(channelName);// Register for all the events before attempting to open the channel.                pushchannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(pushchannel_ChannelUriUpdated);                pushchannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(pushchannel_ErrorOccurred);                pushchannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(pushchannel_HttpNotificationReceived);                pushchannel.Open();            }else            {// The channel was already open, so just register for all the events.                pushchannel.ChannelUriUpdated+=new EventHandler<NotificationChannelUriEventArgs>(pushchannel_ChannelUriUpdated);                pushchannel.ErrorOccurred+=new EventHandler<NotificationChannelErrorEventArgs>(pushchannel_ErrorOccurred);                pushchannel.HttpNotificationReceived+=new EventHandler<HttpNotificationEventArgs>(pushchannel_HttpNotificationReceived);// Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.                System.Diagnostics.Debug.WriteLine(pushchannel.ChannelUri.ToString());                MessageBox.Show(String.Format("Channel Uri is {0}",                    pushchannel.ChannelUri.ToString()));            }        }

void pushchannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)        {string message;

using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body))            {                message = reader.ReadToEnd();            }

            Dispatcher.BeginInvoke(() =>                MessageBox.Show(String.Format("Received Notification {0}:\n{1}",                    DateTime.Now.ToShortTimeString(), message))                    );        }

void pushchannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)        {// Error handling logic for your particular application would be here.            Dispatcher.BeginInvoke(() =>                MessageBox.Show(String.Format("A push notification {0} error occurred.  {1} ({2}) {3}",                    e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData))                    );        }

void pushchannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)        {                        Dispatcher.BeginInvoke(() =>            {// Display the new URI for testing purposes. Normally, the URI would be passed back to your web service at this point.                System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());                MessageBox.Show(String.Format("Channel Uri is {0}",                    e.ChannelUri.ToString()));            });        }    }}

云端:

View Code

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.Text;using System.IO;

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

        }

protected void ButtonSendRaw_Click(object sender, EventArgs e)        {try            {// Get the URI that the Microsoft Push Notification Service returns to the push client when creating a notification channel.// Normally, a web service would listen for URIs coming from the web client and maintain a list of URIs to send// notifications out to.                string subscriptionUri = TextBoxUri.Text.ToString();                HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);// Create an HTTPWebRequest that posts the raw notification to the Microsoft Push Notification Service.// HTTP POST is the only method allowed to send the notification.                sendNotificationRequest.Method = "POST";// Create the raw message.                string rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +"<root>" +"<Value1>" + TextBoxValue1.Text.ToString() + "<Value1>" +"<Value2>" + TextBoxValue2.Text.ToString() + "<Value2>" +"</root>";// Set the notification payload to send.                byte[] notificationMessage = Encoding.Default.GetBytes(rawMessage);

// Set the web request content length.                sendNotificationRequest.ContentLength = notificationMessage.Length;                sendNotificationRequest.ContentType = "text/xml";                sendNotificationRequest.Headers.Add("X-NotificationClass", "3");//Possible batching interval values://"3":The message is delivered by the push notification service immediately.//"13":The message is delivered by the push notification service within 450 seconds.//"23":The message is delivered by the push notification service within 900 seconds.                using (Stream requestStream = sendNotificationRequest.GetRequestStream())                {                    requestStream.Write(notificationMessage, 0, notificationMessage.Length);                }

// Send the notification and get the response.                HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();string notificationStatus = response.Headers["X-NotificationStatus"];string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];

// Display the response from the Microsoft Push Notification Service.  // Normally, error handling code would be here. In the real world, because data connections are not always available,// notifications may need to be throttled back if the device cannot be reached.                TextBoxResponse.Text = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;            }catch (Exception ex)            {                TextBoxResponse.Text = "Exception caught sending update: " + ex.ToString();            }        }

    }}

运行效果截图:

Channel Uri:

手机端:

云端:

转载于:https://www.cnblogs.com/DebugLZQ/archive/2012/03/14/2396600.html

推送通知服务【WP7学习札记之十三】相关推荐

  1. Windows Phone 7 不温不火学习之《推送通知服务》

    Windows Phone 中的 Microsoft Push Notification Service 向第三方开发人员提供了一个弹性,专注,而且持续的渠道,使得开发人员可以从Web Service ...

  2. android自定义push通知_20个海外Web和App推送通知服务工具

    在App和网站中使用推送通知有不同的原因,并且在提高流量和与客户互动方面有很多好处.推送通知是一种交互式可点击消息,可将访问者直接引导至你的网站.它们可以帮助你以指数方式增加流量和参与率.因此,营销人 ...

  3. 苹果推送通知服务(APNs)编程(转)详细步骤

    iPhone 对于应用程序在后台运行有诸多限制(除非你越狱).因此,当用户切换到其他程序后,原先的程序无法保持运行状态.对于那些需要保持持续连接状态的应用程序(比如社区网络应用),将不能收到实时的信息 ...

  4. wns服务器没有响应,如何使用 Windows 推送通知服务 (WNS) 进行验证(Windows 运行时应用)...

    如何使用 Windows 推送通知服务 (WNS) 进行验证(Windows 运行时应用) 12/11/2015 本文内容 [ 本文适用于编写 Windows 运行时应用的 Windows 8.x 和 ...

  5. go gorilla_使用gorilla websocket构建浏览器推送通知服务的低级设计

    go gorilla Singhania AdityaSinghania Aditya Follow跟随 Aug 31 8月31 gopher leaving everyone awestruck w ...

  6. 20个海外Web和App推送通知服务工具(一)

    在App和网站中使用推送通知有不同的原因,并且在提高流量和与客户互动方面有很多好处.推送通知是一种交互式可点击消息,可将访问者直接引导至你的网站.它们可以帮助你以指数方式增加流量和参与率.因此,营销人 ...

  7. 【苹果推送Imessage Apple】摘要Apple推送通知服务更新

    苹果基于bug原因,停用了服务器端的SSL3.0连接方式.目前只支持TLS连接. 1. 如果推送的时候deviceToken对应的机器在APNS服务器上是离线状态,苹果会保存推送信息"一段时 ...

  8. 手把手教你开发安卓推送通知服务(使用阿里云 emas)

    0 前言 0.1 痛点:安卓推送通知无统一标准 安卓是开源的手机操作系统,各大手机硬件厂商都会在它的基础上定制自己的操作系统. 在中国,用户因为各种原因无法使用 Google 官方服务框架,所以,在中 ...

  9. Apple推送通知服务教程

    Apple推送通知服务教程 生成APP ID和SSL证书 登录iOS Provisioning Portal页面 首先,我们将要新建一个App ID. 每一个推送APP都需要一个唯一的对应的App I ...

最新文章

  1. 文件标识符必须为双精度类型的整数值标量_【翻译】VTK官方文档 - vtk文件格式
  2. 修改点击cell时显示的颜色
  3. 从零开始入门 K8s | 应用存储和持久化数据卷:核心知识
  4. GitHub最热!码代码不得不知的所有定律法则
  5. 从开源自治,到微服务云化,阿里云的这款产品给了一剂提升微服务幸福感的良药
  6. 嵌入式烤箱能不能放台面上_2021年开放式厨房怎么设计?先来做做嵌入式家电的功课吧!...
  7. 在WinCE5.0和WinCE6.0下,编译选项介绍
  8. 屌丝程序员的那些事(一)-毕业那年
  9. Java基础03 字符串连接符+
  10. app头像上传vue_当前GitHub上排名前十的热门Vue项目
  11. 量子计算机张庆瑞讲座报告,燕山大学彭秋明、张庆瑞教授来我校开展学术交流...
  12. mysql优化概述2
  13. java楼宇报警器_智能楼宇包含哪些安防子系统
  14. 华为NP课程笔记2-OSPF2
  15. TransE代码实践(很详细)
  16. java随机数种子_使用种子的Java随机数
  17. lte tm模式_请教大家个问题,LTE传输模式TM1-TM8中哪种属于MIM.. - 通信技术你问我答 - 纯技术讨论者的天地 - Powered by C114...
  18. MFC 高速绘图坐标轴内添加纵向基准线的方法
  19. 新闻发布系统之浅谈分页技术
  20. python识别图片中的二维码_python3+pyzbar+Image 进行图片二维码识别

热门文章

  1. Python 基础语法(三)
  2. 【mysql问题】foreign key without name 解决方法
  3. 【PyTorch】Tricks 集锦
  4. 引入 ServletContextListener @Autowired null 解决办法
  5. AttributeError: 'str' object has no attribute 'decode' django问题
  6. 防盗链测试01 - Jwplayer+Tengine2.3.1 mp4模块打造流媒体测试服务器
  7. laravel数据迁移的时候遇到的字符串长度的问题
  8. HTTP-GET, HTTP-POST and SOAP的比较
  9. 物理光学 计算倏逝波/渐逝波在界面上存在的范围
  10. 百度地图API公交检索示例 - 标绘结果路线、返回结果集