当前位置: 移动技术网 > IT编程>移动开发>WP > Windows Phone 7 开发 31 日谈——第19日:推送通知

Windows Phone 7 开发 31 日谈——第19日:推送通知

2018年10月10日  | 移动技术网IT编程  | 我要评论

家具材料,虐杀原形2s组完美破解补丁,华硕a450

 

第19日。

昨天,我介绍了webbrowser控件,以及如何在程序中使用它。今天,我们来谈谈可能是这个系列中最重要的话题:推送通知。

可能你对推送通知这个概念还不是很熟悉,其实很简单:不用强制你的应用程序每几分钟就去检查一下服务器,服务器在有新数据的时候可以通知你的手机。

为什么要使用推送通知?

第一个原因,节省用户的电池电量。检测服务器数据很耗电,而在手机中,电池绝对是稀缺资源。你永远不会有足够的电量,并且不论你的电池能撑多久,都要尽量避免会使电池续航时间变短的通知。

第二,你可以通过推送通知来告知你的用户在程序中发生了一些有趣的事,即使程序没有运行。你还可以提醒用户应该打开程序来看看你要告诉他们的事。

推送通知的过程

为了能让你理解我在下面抛出来的代码,我认为很有必要向你精确地解释一下这个过程中所发生的事。

用户在手机中首次运行程序时,应用程序会调用微软的推送通知服务,请求一个用于通信的定制uri。

当在你的web service中激发了一个事件时,你应该向那个uri传递信息(附带一个特定的负载),然后推送通知服务会以活动瓷砖更新,吐司(toast)通知或者程序中真实数据的方式将数据发送到用户的手机中。

本文就是要讲解上述要点如何来做。如果你想看一个可以按部就班构建的例子,请参见windows phone开发人员训练包。其中有一个非常出色的关于推送通知的教程。

从推送通知服务(push notification service)中获取定制的uri

非常感谢,微软将这部分内容做得非常简单。我们得使用microsoft.phone.notification程序集,不过我还是得用10行代码来从推送通知服务中(pns)获取一个定制的uri。首先,我得创建一个httpnotificationchannel。它将自动与pns通信(在另一个线程中),并且还得通过一个事件来捕获服务返回的内容。

代码

httpnotificationchannel channel;

 

void getpushchannel()

{

    channel = new httpnotificationchannel("blankensoft_" + datetime.now.ticks.tostring());

    channel.channeluriupdated += new eventhandler<notificationchannelurieventargs>(channel_channeluriupdated);

    channel.open();

}

 

void channel_channeluriupdated(object sender, notificationchannelurieventargs e)

{

    dispatcher.begininvoke(delegate

    {

        uriblock.text = channel.channeluri.tostring();

    });

}

 

在这个例子中我得到的uri是这样的:

http://sn1.notify.live.net/throttledthirdparty/01.00/aahslicyijgttaidbjosgmfiagaaaaadawaaaaquzm52ojlzoeq2ndjdrkl5mevfmeq

一旦你有了uri,就可以在web service中保存它了。web service会初始化将要发送到你手机上的信息,我们有3种方法来实现:瓷砖(tile)通知,吐司(toast)通知和原生通知。

不同的需求,不同的消息

我刚才提到了你可以向手机发送3种不同类型的消息。下面是一个概述:

原生通知(raw notification)- 原生通知用于设备中真正在运行的程序。它允许你在用户使用时实时更新用户界面。

吐司通知(toast notification)– 无论程序是否在运行都会收到这个消息,但在程序运行时弹出吐司消息可能会有点儿讨人厌。我会在下面的例子中演示。吐司通知不能更新你程序的数据。想做到这一点还是需要传递一个原生通知。

瓷砖通知(tile notification)– 如果你的程序被钉在了开始界面中,你可以用瓷砖通知来更新瓷砖。你可以改变背景图片以及一个0-99的整数。

发送一个吐司通知

一旦我们得到了推送uri,剩下的就是组合http消息的事了,然后将消息发送给这个uri。下面是代码示例:

代码

 httpwebrequest sendnotificationrequest = (httpwebrequest)webrequest.create(channel.channeluri.tostring());

            sendnotificationrequest.method = "post";

            //indicate that you'll send toast notifications!

            sendnotificationrequest.contenttype = "text/xml";

            sendnotificationrequest.headers = new webheadercollection();

            sendnotificationrequest.headers.add("x-notificationclass", "2");

            if (string.isnullorempty(txtmessage.text)) return;

 

            //create xml envelope

            string data = "x-windowsphone-target: toast\r\n\r\n" +

                    "<?xml version='1.0' encoding='utf-8'?>" +

                    "<wp:notification xmlns:wp='wpnotification'>" +

                    "<wp:toast>" +

                    "<wp:text1>{0}</wp:text1>" +

                    "</wp:toast>" +

                    "</wp:notification>";

 

            //wrap custom data into envelope

            string message = string.format(data, txtmessage.text);

            byte[] notificationmessage = encoding.default.getbytes(message);

 

            // set content length

            sendnotificationrequest.contentlength = notificationmessage.length;

 

            //push data to stream

            using (stream requeststream = sendnotificationrequest.getrequeststream())

            {

                requeststream.write(notificationmessage, 0, notificationmessage.length);

            }

 

            //get reponse for message sending

            httpwebresponse response = (httpwebresponse)sendnotificationrequest.getresponse();

            string notificationstatus = response.headers["x-notificationstatus"];

            string notificationchannelstatus = response.headers["x-subscriptionstatus"];

            string deviceconnectionstatus = response.headers["x-deviceconnectionstatus"];

 

 

正如你所见,这部分代码很长并且比较复杂。我建议你多花些时间在上,并且跟着推送通知的例子完整地走一遍。

这是一个演示推送通知(从头到尾)如何工作的绝佳例子,同时向你展示了这些更新可以让你的应用程序总是保持在用户眼前的强大力量。

下载示例代码

对于今天的例子来说,上面的代码由于缺少所需的环境所以对你没什么帮助。今天的下载内容实际上是windows phone开发人员训练包中推送通知例子的最终版。

原文地址:http://www.jeffblankenburg.com/post/31-days-of-windows-phone-7c-day-19-push-notifications.x

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网