using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Notification; namespace Tools.Notification { internal class NotificationBase { #region 参数 private string _channelName = "Baicai"; private string _serviceName = "BaicaiPushService"; internal HttpNotificationChannel NotificationChannel { get; private set; } #endregion #region 构造函数 private static NotificationBase _instance; internal NotificationBase CreateInstance(string channelName, string serviceName) { if (_instance==null) { _instance = new NotificationBase(); } _instance._channelName = channelName; _instance._serviceName = serviceName; return _instance; } #endregion #region 推送地址获取 internal event EventHandler<UriEventArgs> UriOpenOrUpdated; protected virtual void OnUriOpenOrUpdated(UriEventArgs e) { EventHandler<UriEventArgs> handler = UriOpenOrUpdated; if (handler != null) handler(this, e); } internal event EventHandler ChannelClosed; protected virtual void OnChannelClosed() { EventHandler handler = ChannelClosed; if (handler != null) handler(this, EventArgs.Empty); } #endregion #region 外部方法 /// <summary> /// 开启推送 /// </summary> internal void Connect() { NotificationChannel = HttpNotificationChannel.Find(_channelName); if (NotificationChannel != null) { if (NotificationChannel.ChannelUri != null) { OnUriOpenOrUpdated(new UriEventArgs(true, NotificationChannel.ChannelUri, null)); } } else { NotificationChannel = new HttpNotificationChannel(_channelName, _serviceName); } if (!NotificationChannel.IsShellTileBound) { NotificationChannel.BindToShellTile(); } if (!NotificationChannel.IsShellToastBound) { NotificationChannel.BindToShellToast(); } NotificationChannel.ChannelUriUpdated += (sender, args) => OnUriOpenOrUpdated(new UriEventArgs(true, args.ChannelUri, null)); NotificationChannel.Open(); } /// <summary> /// 关闭推送 /// </summary> internal void DisConnect() { if (NotificationChannel != null) { NotificationChannel.Close(); NotificationChannel.Dispose(); NotificationChannel = null; } OnChannelClosed(); } #endregion } public class UriEventArgs : EventArgs { public bool IsSuccess { get; set; } public Uri Uri { get; set; } public Exception Exception { get; set; } public UriEventArgs(bool isSuccess, Uri uri, Exception exception) { IsSuccess = isSuccess; Uri = uri; Exception = exception; } } }