• 一起学Windows Phone 7开发(十二.Push Notification)


     

    一.简介

    Push Notification windows phone 7中的特色功能之一,这个功能可以变相的让普通开发者实现多任务(尽管并不是真正的多任务)。它为手机端应用和webservice之间建立了一条专用的、持久的、稳定的通道来推送通知。当通道建立后,手机端应用可以接收webservice的任何信息。

    二.分类

    对于Push Notification主要有三种:

    1.       Tile Notification:

    是可以改变Quick Lanuch area内的图标内容(图片,文字等)的方式。不过这个需要把程序pin to start,才可以使用。

    2.       Toast Notification:

    是在屏幕上面可以显示一个提示栏的方式。当点击提示栏可以打开应用程序。

    3.       Raw Notification:

    是直接使用Http方式来接收(http polling)通知的方式。并且是不可见的,以后台方式传送通知。

    对于以上几种通知,都需要一个服务端以push notification方式来发送通知, 也就是说要使用push notification都需要一个服务端。

    三.创建服务器端

    对于服务器端来说,发送不同的通知,都是以Http方式发出去的,但是在发送时,需要配置相应的参数,来告诉Push Notification Service所发送的类型是什么。

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(channelUri);

         request.Method = WebRequestMethods.Http.Post;

         request.ContentType = "text/xml; charset=utf-8";

         request.ContentLength = notificationmessage.Length;

         request.Headers["X-MessageID"] = Guid.NewGuid().ToString();

         

     

         1.Toast notification:

             request.Headers["X-WindowsPhone-Target"] = "toast";

    request.Headers[X-NotificationClass]

     

    Message :

    "Content-Type: text/xml\r\nX-WindowsPhone-Target: token\r\n\r\n"

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

    <wp:Notification xmlns:wp="WPNotification">

         <wp:Tile>

           <wp:BackgroundImage>

                <background image path>

            </wp:BackgroundImage>

           <wp:Count>

                 <count>

            </wp:Count>

           <wp:Title>

              <title>

           </wp:Title>

        </wp:Tile>

    </wp:Notification>

     

          2.Token notification:

             request.Headers["X-WindowsPhone-Target"] = "token";

    request.Headers[X-NotificationClass]

     

    Message:

    “Content-Type: text/xml\r\nX-WindowsPhone-Target: toast\r\n\r\n”

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

    <wp:Notification xmlns:wp="WPNotification">

         <wp:Toast>

            <wp:Text1>

                   <string>

            </wp:Text1>

            <wp:Text2>

            <string>

            </wp:Text2>

        </wp:Toast>

    </wp:Notification>

    3.raw notification

    request.Headers[X-NotificationClass]

     

    request.BeginGetRequestStream();

    Stream requestStream = request.EndGetRequestStream();

    requestStream.BeginWrite(message);

     

    Response 数据

    response.StatusCode//Ok 表示成功,否则可以查下面相应的错误码表,同时也可以查表得到当前状态

    response.Headers[X-MessageID]

    response.Headers[X-DeviceConnectionStatus]

    response.Headers[X-SubscriptionStatus]

    response.Headers[X-NotificationStatus]

     

     

     

    四.创建客户端

    HttpNotificationChannel  httpChannel = HttpNotificationChannel.Find(ChannelName);

    httpChannel.Open();

    //绑定notification

    httpChannel.BindToShellToast();

    httpChannel.BindToShellTile(uris);

     

    //获取notification channel URI

    httpChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(httpChannel_ChannelUriUpdated);

    //获取Raw notification

    httpChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(httpChannel_HttpNotificationReceived);

              // 获取Toast notification

     httpChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(httpChannel_ShellToastNotificationReceived);

          // 获取Push notification error message

    httpChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(httpChannel_ExceptionOccurred);

    对于Tile notification是由系统来接收的,所以这里没有相应的Event.

    以上就是push notification的一些基本步骤,具体的实例在WP7TrainningKit里有。

  • 相关阅读:
    如何撤销Git操作?
    SpringBoot Controller接收参数的几种方式盘点
    全面解析Spring中@ModelAttribute注解的用法
    Java中将字符串转为驼峰格式
    如何将Map键值的下划线转为驼峰
    JS如何获取地址栏url后面的参数?
    解决wordpress 5.3更新后Uncaught Typeerror: $ is not a function
    小程序如何判断用户(后台使用Django)
    服务器 Web服务器 应用服务器区别联系
    C语言和Python语言在存储变量方面的不同
  • 原文地址:https://www.cnblogs.com/randylee/p/1780805.html
Copyright © 2020-2023  润新知