推送通知服务【WP7学习札记之十三】
??? 為什么使用推送通知服務
????Windows Phone執行模型決定只有一個第三方的應用程序可以在前臺運行,應用程序不能再后臺運行,不斷的往Cloud拉數據。微軟提供推送通知服務(Push Notification)給第三方應用程序取得更新通知的消息。由于服務器能夠主動的發起通信,因此可以有效的降低手機電池的消耗。
??? Windows Phone 的推送通知的完整權威描述見MSDN文檔描述見:http://msdn.microsoft.com/zh-cn/library/ff402537(v=vs.92).aspx。
本節內容
-
Windows Phone 的推送通知概述
-
接收 Windows Phone 的推送通知
-
發送 Windows Phone 的推送通知
-
Windows Phone 的推送通知服務響應代碼
-
如何設置 Windows Phone 的回調注冊請求
-
設置已驗證的 Web 服務以發送 Windows Phone 的推送通知
-
如何發送和接收 Windows Phone 的 Toast 通知
-
如何發送和接收 Windows Phone 的磁貼通知
-
如何發送和接收 Windows Phone 的 Raw 通知
??? 上圖顯示了手機上運行的客戶端應用程序如何從推送客戶端服務 (1) 請求推送通知 URI。然后,推送客戶端服務與 Microsoft 推送通知服務 (MPNS) 協商并向客戶端應用程序(2 和 3)返回一個通知 URI。之后,客戶端應用程序將此 URI 發送給云服務 (4)。當 Web 服務有要發送到客戶端應用程序的信息時,該服務使用此 URI 向 Microsoft 推送通知服務 (5) 發送推送通知,Microsoft 推送通知服務又將此推送通知發送給在 Windows Phone 設備 (6) 上運行的應用程序。
????? 根據推送通知的格式以及連接到通知的負載,信息作為原始數據發送到應用程序、應用程序的磁貼在視覺上得到更新或顯示 Toast 通知。發送推送通知之后,Microsoft 推送通知服務向您的 Web 服務發送一個響應代碼,指示此通知已接收并且下次有機會會發送到設備。但是,Microsoft 推送通知服務不提供將推送通知從 Web 服務發送到設備的端到端通信。
Jake Lin的描述是:
使用規范:
?? windows phone 7目前只允許15個第三方應用程序使用推送通知服務;
?? 詢問用戶是否使用推送通知服務;
?? 為用戶提供取消訂閱的選項。
s消息類型:
?? Raw Notification:
????? 可以發送任何格式的數據;
????? 應用程序可以根據需要加工數據;
????? 應用程序相關的通知消息;
????? ★只有在應用程序運行時才發送。
?? Toast Notification:
????? 發送的數據為指定的xml格式;
????? ★如果應用程序正在運行,內容發送到應用程序中;
????? ★如果應用程序不在運行,彈出toast消息框顯示消息:
???????? App圖標加上兩個文本描述;
???????? 打斷用戶當前操作,但是是臨時的;
???????? 用戶可以點擊進行跟蹤。
??? Tile Notification:
????? 發送的數據為指定的xml格式;
????? ★不會往應用程序進行發送;
????? ★如果用戶把應用程序Pin to Start,那么更新數據會發送到start screen 的tile里面:
???????? 包含三個屬性,背景、標題和計數器;
???????? 每個屬性都有固定的格式和位置;
???????? 可以使用其中的屬性,不一定三個屬性一起用。
示例程序示例(Raw):手機客戶端代碼:
View Code using System;using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Notification;
namespace 推送通知服務
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
//Holds the push channel that is created or found.
HttpNotificationChannel pushchannel;
//The name of our push channel.
string channelName = "RawSampleChannel";
//Try to find the push channel
pushchannel = HttpNotificationChannel.Find(channelName);
// If the channel was not found, then create a new connection to the push service.
if (pushchannel == null)
{
pushchannel = new HttpNotificationChannel(channelName);
// Register for all the events before attempting to open the channel.
pushchannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(pushchannel_ChannelUriUpdated);
pushchannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(pushchannel_ErrorOccurred);
pushchannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(pushchannel_HttpNotificationReceived);
pushchannel.Open();
}
else
{
// The channel was already open, so just register for all the events.
pushchannel.ChannelUriUpdated+=new EventHandler<NotificationChannelUriEventArgs>(pushchannel_ChannelUriUpdated);
pushchannel.ErrorOccurred+=new EventHandler<NotificationChannelErrorEventArgs>(pushchannel_ErrorOccurred);
pushchannel.HttpNotificationReceived+=new EventHandler<HttpNotificationEventArgs>(pushchannel_HttpNotificationReceived);
// Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
System.Diagnostics.Debug.WriteLine(pushchannel.ChannelUri.ToString());
MessageBox.Show(String.Format("Channel Uri is {0}",
pushchannel.ChannelUri.ToString()));
}
}
void pushchannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
{
string message;
using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body))
{
message = reader.ReadToEnd();
}
Dispatcher.BeginInvoke(() =>
MessageBox.Show(String.Format("Received Notification {0}:\n{1}",
DateTime.Now.ToShortTimeString(), message))
);
}
void pushchannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
{
// Error handling logic for your particular application would be here.
Dispatcher.BeginInvoke(() =>
MessageBox.Show(String.Format("A push notification {0} error occurred. {1} ({2}) {3}",
e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData))
);
}
void pushchannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
// Display the new URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());
MessageBox.Show(String.Format("Channel Uri is {0}",
e.ChannelUri.ToString()));
});
}
}
}
云端:
View Code using System;using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
namespace SendRaw
{
public partial class SendRaw : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ButtonSendRaw_Click(object sender, EventArgs e)
{
try
{
// Get the URI that the Microsoft Push Notification Service returns to the push client when creating a notification channel.
// Normally, a web service would listen for URIs coming from the web client and maintain a list of URIs to send
// notifications out to.
string subscriptionUri = TextBoxUri.Text.ToString();
HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);
// Create an HTTPWebRequest that posts the raw notification to the Microsoft Push Notification Service.
// HTTP POST is the only method allowed to send the notification.
sendNotificationRequest.Method = "POST";
// Create the raw message.
string rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<root>" +
"<Value1>" + TextBoxValue1.Text.ToString() + "<Value1>" +
"<Value2>" + TextBoxValue2.Text.ToString() + "<Value2>" +
"</root>";
// Set the notification payload to send.
byte[] notificationMessage = Encoding.Default.GetBytes(rawMessage);
// Set the web request content length.
sendNotificationRequest.ContentLength = notificationMessage.Length;
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-NotificationClass", "3");
//Possible batching interval values:
//"3":The message is delivered by the push notification service immediately.
//"13":The message is delivered by the push notification service within 450 seconds.
//"23":The message is delivered by the push notification service within 900 seconds.
using (Stream requestStream = sendNotificationRequest.GetRequestStream())
{
requestStream.Write(notificationMessage, 0, notificationMessage.Length);
}
// Send the notification and get the response.
HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
string notificationStatus = response.Headers["X-NotificationStatus"];
string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
// Display the response from the Microsoft Push Notification Service.
// Normally, error handling code would be here. In the real world, because data connections are not always available,
// notifications may need to be throttled back if the device cannot be reached.
TextBoxResponse.Text = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;
}
catch (Exception ex)
{
TextBoxResponse.Text = "Exception caught sending update: " + ex.ToString();
}
}
}
}
運行效果截圖:
Channel Uri:
手機端:
云端:
?
?
轉載于:https://www.cnblogs.com/DebugLZQ/archive/2012/03/14/2396600.html
總結
以上是生活随笔為你收集整理的推送通知服务【WP7学习札记之十三】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windowserver2008官方不提
- 下一篇: 跨境电商概念上市公司一览 有业绩支撑的行