这篇文章是从自己的个人网站转过来的,因为3月份个人网站就要关了
//创建Channel
httpChannel = new HttpNotificationChannel(channelName, "NotificationServers");
//注册channel事件
httpChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(httpChannel_ChannelUriUpdated);
在 ChannelUriUpdated 事件信息的 e中获取地址:
void httpChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
Debug.WriteLine(e.ChannelUri);
}
![](http://hi.csdn.net/attachment/201202/8/0_1328710206yNtG.gif)
剩下的就是注册 Raw Toast Tile 以及Toast 和Tile的绑定(当程序没有运行的时候,在手机的主界面显示推送信息)
httpChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(httpChannel_ErrorOccurred);
httpChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(httpChannel_HttpNotificationReceived);
httpChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(httpChannel_ShellToastNotificationReceived);
httpChannel.Open();
//程序如果没有运行 则绑定消息 弹出对话框
httpChannel.BindToShellToast();
httpChannel.BindToShellTile();
}
void httpChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
{
string msg = "";
foreach (var key in e.Collection.Keys)
{
msg += key + ":" + e.Collection[key] + Environment.NewLine;
}
Dispatcher.BeginInvoke(() =>
{
txt_msg.Text = msg;
});
}
void httpChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
{
using (var reader=new StreamReader(e.Notification.Body))
{
string msg = reader.ReadToEnd();
Dispatcher.BeginInvoke(() =>
{
txt_msg.Text = msg;
});
}
}
void httpChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
txt_msg.Text = e.Message;
});
}
模拟一个服务器 像microsoft发送信息
switch (type)
{
case "Raw":
msg = Encoding.UTF8.GetBytes(txtSendMsg.Text);
break;
case "Tile":
{
string tileMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">"+
"<wp:Tile>" +
"<wp:BackgroundImage>/Images/Cloudy.png</wp:BackgroundImage>" +
"<wp:Count>28</wp:Count>"+
"<wp:Title>AnthemSord</wp:Title>" +
"</wp:Tile>"+
"</wp:Notification>";
msg = Encoding.UTF8.GetBytes(tileMessage);
notificationType = NotificationType.Tile;
}
break;
case "Toast":
{
string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
"<wp:Toast>" +
"<wp:Text1>AnthemSord:</wp:Text1>" +
"<wp:Text2>"+txtSendMsg.Text+"</wp:Text2>" +
"</wp:Toast>" +
"</wp:Notification>";
msg = Encoding.UTF8.GetBytes(toastMessage);
notificationType = NotificationType.Toast;
}
break;
default:
break;
}
SendNotifications(msg,notificationType);
下面是post数据对request的一些格式参考,我直接把SendNotifications 函数的代码全部写上吧,不难看,很简单
private void SendNotifications(byte[] msg,NotificationType type)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtUri.Text);
request.Method = WebRequestMethods.Http.Post;
request.Headers["X-MessageID"] = Guid.NewGuid().ToString();
request.ContentType = "text/xml;charset=utf-8";
switch (type)
{
case NotificationType.Raw:
{
//3:立即发送
//13:等待450秒后发送
//23:等待900秒后发送
request.Headers.Add("X-NotificationClass", "3");
}
break;
case NotificationType.Tile:
{
//1:立即发送
//11:等待450秒后发送
//21:等待900秒后发送
request.Headers.Add("X-WindowsPhone-Target", "token");
request.Headers.Add("X-NotificationClass", "1");
}
break;
case NotificationType.Toast:
{
//2:立即发送
//12:等待450秒后发送
//22:等待900秒后发送
request.Headers.Add("X-WindowsPhone-Target", "toast");
request.Headers.Add("X-NotificationClass", "2");
}
break;
default:
break;
}
using (Stream stream=request.GetRequestStream())
{
stream.Write(msg, 0, msg.Length);
}
HttpWebResponse respones = (HttpWebResponse)request.GetResponse();
string notificationStatus = respones.Headers["X-NotificationStatus"];
string notificationChannelStatus = respones.Headers["X-SubscriptionStatus"];
string deviceConnectionStatus = respones.Headers["X-DeviceConnectionStatus"];
MessageBox.Show(string.Format("通知状态:{0},管道状态:{1},连接状态:{2}",notificationStatus,notificationChannelStatus,deviceConnectionStatus));
}
看过林永坚哥哥的视频应该很熟悉这个,我对推送这个东西非常有感觉,所以也自己动手试一试,不知道我为什么就对推送这么“兴奋” --!可能受iPhone的影响吧~当然这篇文章是作为笔记发表的,哈哈:)
个人网站的水印我现在发现很恶心啊~
现在楼主再用WPF开发项目,所以都木有很多时间来了解WP7的东东,很希望和大家交流和请教大家额~:)