日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

推送通知服务【WP7学习札记之十三】

發(fā)布時(shí)間:2025/4/5 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 推送通知服务【WP7学习札记之十三】 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

??? 為什么使用推送通知服務(wù)

????Windows Phone執(zhí)行模型決定只有一個(gè)第三方的應(yīng)用程序可以在前臺(tái)運(yùn)行,應(yīng)用程序不能再后臺(tái)運(yùn)行,不斷的往Cloud拉數(shù)據(jù)。微軟提供推送通知服務(wù)(Push Notification)給第三方應(yīng)用程序取得更新通知的消息。由于服務(wù)器能夠主動(dòng)的發(fā)起通信,因此可以有效的降低手機(jī)電池的消耗。

??? Windows Phone 的推送通知的完整權(quán)威描述見(jiàn)MSDN文檔描述見(jiàn):http://msdn.microsoft.com/zh-cn/library/ff402537(v=vs.92).aspx。

本節(jié)內(nèi)容

  • Windows Phone 的推送通知概述

  • 接收 Windows Phone 的推送通知

  • 發(fā)送 Windows Phone 的推送通知

  • Windows Phone 的推送通知服務(wù)響應(yīng)代碼

  • 如何設(shè)置 Windows Phone 的回調(diào)注冊(cè)請(qǐng)求

  • 設(shè)置已驗(yàn)證的 Web 服務(wù)以發(fā)送 Windows Phone 的推送通知

  • 如何發(fā)送和接收 Windows Phone 的 Toast 通知

  • 如何發(fā)送和接收 Windows Phone 的磁貼通知

  • 如何發(fā)送和接收 Windows Phone 的 Raw 通知

推送消息的過(guò)程MS的描述是:

??? 上圖顯示了手機(jī)上運(yùn)行的客戶端應(yīng)用程序如何從推送客戶端服務(wù) (1) 請(qǐng)求推送通知 URI。然后,推送客戶端服務(wù)與 Microsoft 推送通知服務(wù) (MPNS) 協(xié)商并向客戶端應(yīng)用程序(2 和 3)返回一個(gè)通知 URI。之后,客戶端應(yīng)用程序?qū)⒋?URI 發(fā)送給云服務(wù) (4)。當(dāng) Web 服務(wù)有要發(fā)送到客戶端應(yīng)用程序的信息時(shí),該服務(wù)使用此 URI 向 Microsoft 推送通知服務(wù) (5) 發(fā)送推送通知,Microsoft 推送通知服務(wù)又將此推送通知發(fā)送給在 Windows Phone 設(shè)備 (6) 上運(yùn)行的應(yīng)用程序。

????? 根據(jù)推送通知的格式以及連接到通知的負(fù)載,信息作為原始數(shù)據(jù)發(fā)送到應(yīng)用程序、應(yīng)用程序的磁貼在視覺(jué)上得到更新或顯示 Toast 通知。發(fā)送推送通知之后,Microsoft 推送通知服務(wù)向您的 Web 服務(wù)發(fā)送一個(gè)響應(yīng)代碼,指示此通知已接收并且下次有機(jī)會(huì)會(huì)發(fā)送到設(shè)備。但是,Microsoft 推送通知服務(wù)不提供將推送通知從 Web 服務(wù)發(fā)送到設(shè)備的端到端通信。

Jake Lin的描述是:

使用規(guī)范:

?? windows phone 7目前只允許15個(gè)第三方應(yīng)用程序使用推送通知服務(wù);

?? 詢問(wèn)用戶是否使用推送通知服務(wù);

?? 為用戶提供取消訂閱的選項(xiàng)。

s消息類(lèi)型:

?? Raw Notification:

????? 可以發(fā)送任何格式的數(shù)據(jù);

????? 應(yīng)用程序可以根據(jù)需要加工數(shù)據(jù);

????? 應(yīng)用程序相關(guān)的通知消息;

????? ★只有在應(yīng)用程序運(yùn)行時(shí)才發(fā)送。

?? Toast Notification:

????? 發(fā)送的數(shù)據(jù)為指定的xml格式;

????? ★如果應(yīng)用程序正在運(yùn)行,內(nèi)容發(fā)送到應(yīng)用程序中;

????? ★如果應(yīng)用程序不在運(yùn)行,彈出toast消息框顯示消息:

???????? App圖標(biāo)加上兩個(gè)文本描述;

???????? 打斷用戶當(dāng)前操作,但是是臨時(shí)的;

???????? 用戶可以點(diǎn)擊進(jìn)行跟蹤。

??? Tile Notification:

????? 發(fā)送的數(shù)據(jù)為指定的xml格式;

????? ★不會(huì)往應(yīng)用程序進(jìn)行發(fā)送;

????? ★如果用戶把應(yīng)用程序Pin to Start,那么更新數(shù)據(jù)會(huì)發(fā)送到start screen 的tile里面:

???????? 包含三個(gè)屬性,背景、標(biāo)題和計(jì)數(shù)器

???????? 每個(gè)屬性都有固定的格式和位置;

???????? 可以使用其中的屬性,不一定三個(gè)屬性一起用。

示例程序示例(Raw):手機(jī)客戶端代碼:

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 推送通知服務(wù)
{
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();
}
}

}
}

運(yùn)行效果截圖:

Channel Uri:

手機(jī)端:

云端:

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/DebugLZQ/archive/2012/03/14/2396600.html

總結(jié)

以上是生活随笔為你收集整理的推送通知服务【WP7学习札记之十三】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。