WP开发-开启推送通知Notification工具类 附服务端设置

Posted by baicai on July 5, 2013
public class Notification
    {
        #region Property
        private string ChannelName { get; set; }
        private string ServiceName { get; set; }
        private string AddUrl { get; set; }//开启推送向服务器发送微软返回的地址
        private string DelUrl { get; set; }//关闭推送时通知自己的服务器
        private string UserAgent { get; set; }
        private HttpNotificationChannel NotificationChannel { get; set; }
        #endregion

        public Notification(string channelName, string seviceName, string addUrl, string delUrl, string userAgent)
        {
            ChannelName = channelName;
            ServiceName = seviceName;
            AddUrl = addUrl;
            DelUrl = delUrl;
            UserAgent = userAgent;
        }

        #region method
        public void Connect()
        {
            try
            {
                NotificationChannel = HttpNotificationChannel.Find(ChannelName);
                if (NotificationChannel != null)
                {
                    NotificationChannel.Close();
                    NotificationChannel.Dispose();
                }
                NotificationChannel = new HttpNotificationChannel(ChannelName, ServiceName);
                SetChannelEvents();
                NotificationChannel.Open();
                NotificationChannel.BindToShellToast();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public void Disconnect()
        {
            try
            {
                NotificationChannel = HttpNotificationChannel.Find(ChannelName);

                if (NotificationChannel != null)
                {
                    try
                    {
                        new HttpHelper(UserAgent).request(string.Format(DelUrl + "&url=" + Uri.EscapeDataString(NotificationChannel.ChannelUri.ToString())), null, null);
                    }
                    catch { }
                    if (NotificationChannel.IsShellToastBound)
                    {
                        NotificationChannel.UnbindToShellToast();
                    }
                    NotificationChannel.Close();
                    NotificationChannel.Dispose();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            finally
            {
                NotificationChannel = null;
            }
        }

        private void SetChannelEvents()
        {
            NotificationChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(httpChannel_ChannelUriUpdated);
            NotificationChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(httpChannel_ErrorOccurred);
            NotificationChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(httpChannel_HttpNotificationReceived); 
            NotificationChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(httpChannel_ShellToastNotificationReceived);
        }
        #endregion

        #region Privates

        //Toast推送
        private void httpChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
        {
        }
        //RAW推送
        private void httpChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
        {
        }
        //申请推送api错误
        private void httpChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
        {
        }
        //申请推送api成功
        private void httpChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
        {
            try
            {
                string url = string.Format(AddUrl + "?url=" + Uri.EscapeDataString(e.ChannelUri.ToString()));
                new HttpHelper(UserAgent).request(url, null, null);
            }
            catch { }
        }
        #endregion
HttpHelper类是一个HTTP请求的辅助类 参见:<a href="http://blog.liubaicai.com/?p=67">http://blog.liubaicai.com/?p=67</a>
服务器端设置参见Demo:<a href="http://files.cnblogs.com/liubaicai/WebCast20101203_Demo.zip">http://files.cnblogs.com/liubaicai/WebCast20101203_Demo.zip</a>