news 2026/7/4 19:13:30

Windows Phone推送通知类型

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Windows Phone推送通知类型

Windows Phone中存在三种默认通知类型:Tile、Push 和 Toast 通知。

Tile通知

每个应用程序可设置Tile—应用程序内容的可视化、 动态的表示形式。当应用程序被固定显示在启动屏幕(Start Screen)时,我们就可以看到Tile的信息。Tile可以修改的三个元素包括:计数(Count)、标题(Title)和背景图像(Background)。

Toast通知

Toast通知是Windows Phone系统通知,且不破坏用户的工作流,十秒钟后自动消失。Toast通知显示在屏幕的顶部。

Toast通知的两个文本元素:标题和副标题。标题为粗体字显示的字符串,副标题为非粗体字显示的字符串。

重要说明:

您必须要求用户授权方可接收Toast通知,且在应用程序中必须具有允许用户禁用的Toast通知的功能。

Raw通知

Raw通知的格式可以任意设定。如果当前没有运行您的应用程序,Raw通知将被微软推通知服务丢弃,不会传递到Windows Phone设备。Raw通知的有效载荷的最大为 1 KB。

推送通知类型选择

通知是用户体验中的重要组成部分,您需要仔细考虑它的使用方式。重复通知或侵入式通知会降低您的应用程序及设备上运行的其他程序的性能。这些通知还会打扰用户。

请考虑发送通知的频率以及您希望引起用户注意的事件类型。

推式通知类型

应用示例

Tile通知

如天气应用温度变化的信息性通知。

Toast通知

立即查看,如突发新闻的重要通知。

Raw通知

以自定义的格式将信息直接发送到您的应用程序。

推送通知的工作流

  1. Window Phone客户端应用程序请求与微软推送通知服务(Microsoft Push Notification Services)建立通道连接,微软推送通知服务(Microsoft Push Notification Services)使用通道URI响应。
  2. Window Phone客户端应用程序向监视服务(Web Service或者Cloud Application)发送包含推送通知服务通道URI以及负载的消息。
  3. 当监视服务检测到信息更改时(如航班取消、航班延期或天气警报),它会向微软推送通知服务(Microsoft Push Notification Services)发送消息。
  4. 微软推送通知服务(Microsoft Push Notification Services)将消息中继到Windows Phone设备,由Window Phone客户端应用程序处理收到的消息。

深度剖析推送通知实现架构

Windows Phone应用程序的推送通知的实现方式中,程序员几乎不需要编写代码就可以实现在Windows Phone的三种推送通知响应,原因是系统本身已经替我们做好了。如果应用程序使用推送通知功能的话,需要开发者关注的主要有两个方面,第一是启用和关闭应用程序推送通知的设定,因为这是MarketPlace要求应用程序必须具备的功能;第二是Web Service的设计和代码实现,因为实现推送通知消息内容的逻辑都是在Web Service端完成的,然后通知MPNS将消息推送至Windows Phone应用程序。Web Service既可以是云端的Cloud Application,也可以是其他的Web应用程序,只要能和MPNS通讯即可。

本节中,我们参考微软官方博客--The Windows Blog上的文章Windows Push Notification Server Side Helper Library,深度解析推送通知实现架构中的需要开发者重点关注的两个方面。

代码下载

推送通知消息类

推送通知消息基础类:PushNotificationMessage类,以及三个子类:RawPushNotificationMessageTilePushNotificationMessageToastPushNotificationMessage。如图Push Messages Class Diagram

  • RawPushNotificationMessage– 当Windows Phone应用程序运行时,可以接收到来自Web Service的Raw通知消息。
  • TilePushNotificationMessage–当Windows Phone应用程序被固定显示在启动页面,Windows Phone将呈现Tile通知消息的内容。
  • ToastPushNotificationMessage–发送Toast"警告"消息至Windows Phone。

图 Push Messages Class Diagram

推送通知消息的示例

发送Tile通知

下面的代码片段演示了如何使用Windows Phone的推送通知类库以同步和异步的方式发送Tile通知。

// Prepare a tile push notification message.
var tile =new TilePushNotificationMessage
{
BackgroundImageUri = tileImageUri, // Remote or phone-local tile image uri.
Count = tileCount, // Counter between 1 to 99 should be displayed on the tile.
Title = “Tile Title” // Title to be displayed on the tile.
};

// Send the message synchronously.
try
{
var sendResult = tile.Send(phoneChannelUri);
// Check the send result.
}
catch (Exception ex)
{
// Log the error.
}

// Send the message asynchronously.
tile.SendAsync(
phoneChannelUri,
result => {/* Check the send result */},
exception => {/* Log the error */});

从上面的代码可以看出,发送Tile通知到Windows Phone应用多么简单,仅仅是创建一个新的TilePushNotificationMessage,并设置相关的属性,然后调用同步Send和异步SendAsync的方法即可。

发送Toast通知

下面的代码片段演示了如何使用Windows Phone的推送通知类库以同步和异步的方式发送Toast通知。

// Prepare a toast push notification message.
var toast =new ToastPushNotificationMessage
{
Title = “Title”, // Title to be displayed as the toast header.
Subtitle = “Sub Title” // Message to be displayed next to the toast header.
};

// Send the message synchronously.
try
{
var sendResult = toast.Send(phoneChannelUri);
// Check the send result.
}
catch (Exception ex)
{
// Log the error.
}

// Send the message asynchronously.
toast.SendAsync(
phoneChannelUri,
result => { /* Check the send result */ },
exception => { /* Log the error */ });

发送Raw通知

下面的代码片段演示了如何使用Windows Phone的推送通知类库以同步和异步的方式发送Raw通知。

C#

// Prepare a raw push notification message.
byte[] rawData = {};
var raw =new RawPushNotificationMessage
{
RawData = rawData, // Raw data to be sent with the message.
};

// Send the message synchronously.
try
{
var sendResult = raw.Send(phoneChannelUri);
// Check the send result.
}
catch (Exception ex)
{
// Log the error.
}

// Send the message asynchronously.
raw.SendAsync(
phoneChannelUri,
result => { /* Check the send result */ },
exception => { /* Log the error */ });

Windows Phone客户端设定启动推送通知

我们使用简单的Silverlight应用说明如何将推送通知消息发送到Windows Phone智能手机。

注册推送通知服务

为了接收推送通知消息,Windows Phone应用程序需要向微软推送通知服务MPNS发送注册请求,MPNS返回的Windows Phone智能手机的URI(统一资源标识符)。

在我们的示例中,推送通知PN功能封装在PushContext类。强烈建议您每次启动应用程序时将PN通道URI更新到Web Service,确保您的Web Service中保存的是智能手机最新的URI。

推送通知设置页面

每个Windows Phone应用,如果使用推送通知都必须允许用户设置推送通知功能的开启和关闭。 即程序开启推送通知PN通道时需要得到用户的许可,且用户也可以选择关闭推送通知PN通道。下面的画面(Project : WindowsPhone.Recipes.Push.Client File : Views/PushSettingControl.XAML)显示了示例应用程序的推送通知设定页面。

图 push setting

Project : WindowsPhone.Recipes.Push.Client File : Views/PushSettingControl.XAML

<UserControl x:Class="WindowsPhone.Recipes.Push.Client.Controls.PushSettingsControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tk="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:converters="clr-namespace:WindowsPhone.Recipes.Push.Client.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="640" d:DesignWidth="480">

<UserControl.Resources>

<converters:BoolBrushConverter x:Key="BoolBrushConverter"/>

<Style x:Key="DescTextStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="14"/>
<Setter Property="Foreground" Value="Silver"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="Margin" Value="16,-38,16,24"/>
</Style>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">

<StackPanel>
<StackPanel>
<tk:ToggleSwitch Header="Push Notifications"
IsChecked="{Binding IsPushEnabled, Mode=TwoWay}"/>
<TextBlock Style="{StaticResource DescTextStyle}"
Text="Turn on/off push notifications."/>
</StackPanel>

<Grid>
<StackPanel Margin="16,0,0,0">
<tk:ToggleSwitch Header="Tile Notifications"
IsChecked="{Binding IsTileEnabled, Mode=TwoWay}"/>
<TextBlock Style="{StaticResource DescTextStyle}"
Text="Tile push notifications update the application's tile displayed in the Start Screen. The application must be pinned by the user first."/>

<tk:ToggleSwitch Header="Toast Notifications"
IsChecked="{Binding IsToastEnabled, Mode=TwoWay}"/>
<TextBlock Style="{StaticResource DescTextStyle}"
Text="Toast push notifications are system-wide notifications that do not disrupt the user workflow or require intervention to resolve and are displayed in the top of the screen for ten seconds."/>

<tk:ToggleSwitch Header="Raw Notifications"
IsChecked="{Binding IsRawEnabled, Mode=TwoWay}"/>
<TextBlock Style="{StaticResource DescTextStyle}"
Text="Raw push notifications are used to send application specific information. The application must be running first."/>
</StackPanel>

<Border Background="{Binding IsPushEnabled, Converter={StaticResource BoolBrushConverter}}"/>
</Grid>

</StackPanel>
</Grid>
</UserControl>

当用户登录时,注册PN推送通知的通道。

Project: WindowsPhone.Recipes.Push.Client File: Views/ UserLoginView.xaml.cs

privatevoid InternalLogin()
{
login.Visibility = Visibility.Collapsed;
progress.Visibility = Visibility.Visible;

var pushContext = PushContext.Current;
pushContext.Connect(c => RegisterClient(c.ChannelUri));
}

privatevoid RegisterClient(Uri channelUri)
{
// Register the URI with 3rd party web service.
try
{
var pushService =new PushServiceClient();
pushService.RegisterCompleted += (s, e) =>
{
pushService.CloseAsync();

Completed(e.Error);
};

pushService.RegisterAsync(UserName, channelUri);
}
catch (Exception ex)
{
Completed(ex);
}
}

向Web Service提交订阅,创建PN通道

Project: WindowsPhone.Recipes.Push.Client File: PushContext.cs

publicvoid Connect(Action<HttpNotificationChannel> prepared)
{
if (IsConnected)
{
prepared(NotificationChannel);
return;
}

try
{
// First, try to pick up an existing channel.
NotificationChannel = HttpNotificationChannel.Find(ChannelName);

if (NotificationChannel ==null)
{
// Create new channel and subscribe events.
CreateChannel(prepared);
}
else
{
// Channel exists, no need to create a new one.
SubscribeToNotificationEvents();
PrepareChannel(prepared);
}

IsConnected =true;
}
catch (Exception ex)
{
OnError(ex);
}
}

publicvoid Disconnect()
{
if (!IsConnected)
{
return;
}

try
{
if (NotificationChannel !=null)
{
UnbindFromTileNotifications();
UnbindFromToastNotifications();
NotificationChannel.Close();
}
}
catch (Exception ex)
{
OnError(ex);
}
finally
{
NotificationChannel =null;
IsConnected =false;
}
}

创建PN通道的具体函数。

Project: WindowsPhone.Recipes.Push.Client File: PushContext.cs

///<summary>
/// Create channel, subscribe to channel events and open the channel.
///</summary>
privatevoid CreateChannel(Action<HttpNotificationChannel> prepared)
{
// Create a new channel.
NotificationChannel =new HttpNotificationChannel(ChannelName, ServiceName);

// Register to UriUpdated event. This occurs when channel successfully opens.
NotificationChannel.ChannelUriUpdated += (s, e) => Dispatcher.BeginInvoke(() => PrepareChannel(prepared));

SubscribeToNotificationEvents();

// Trying to Open the channel.
NotificationChannel.Open();
}

绑定和解除绑定Raw、Tile和Toast通知消息的函数。

Project: WindowsPhone.Recipes.Push.Client File: PushContext.cs

private void BindToTileNotifications()

{

try

{

if (NotificationChannel != null && !NotificationChannel.IsShellTileBound)

{

var listOfAllowedDomains = new Collection<Uri>(AllowedDomains);

NotificationChannel.BindToShellTile(listOfAllowedDomains);

}

}

catch (Exception ex)

{

OnError(ex);

}

}

private void BindToToastNotifications()

{

try

{

if (NotificationChannel != null && !NotificationChannel.IsShellToastBound)

{

NotificationChannel.BindToShellToast();

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/4 19:13:01

crypto-js 加密解密报错调试实战指南:从原理到排查

1. 项目概述&#xff1a;从“加密报错”到“调试实战”如果你在前端或者Node.js项目里用过crypto-js&#xff0c;大概率见过一些让人摸不着头脑的报错。比如&#xff0c;明明代码是从官方文档抄的&#xff0c;一运行却蹦出来一个TypeError: Cannot read property toString of u…

作者头像 李华
网站建设 2026/7/4 19:10:08

虚幻引擎蓝图调试与跨设备迁移实战指南

1. 蓝图拷贝与打印信息基础在虚幻引擎&#xff08;UE&#xff09;开发中&#xff0c;蓝图系统作为可视化脚本工具&#xff0c;其复用性和调试能力直接影响开发效率。很多开发者常遇到两个核心问题&#xff1a;蓝图资源能否跨设备复用&#xff1f;如何有效输出调试信息&#xff…

作者头像 李华
网站建设 2026/7/4 19:09:50

独立游戏开发全流程实战:从原型到发布的10个关键阶段

1. 项目概述&#xff1a;独立游戏开发的魅力与挑战十年前我第一次用RPG Maker做了个简陋的冒险游戏&#xff0c;从此掉进了独立开发的兔子洞。现在回头看&#xff0c;从48小时Game Jam到Steam上架作品&#xff0c;这条路上最宝贵的不是最终成品&#xff0c;而是那些深夜调试碰撞…

作者头像 李华
网站建设 2026/7/4 19:07:59

Unity背包系统Tooltip裁剪问题解决方案

1. 问题现象与背景分析在Unity游戏开发中&#xff0c;背包系统是最常见的UI组件之一。当背包中的道具数量较多时&#xff0c;通常会采用滑动列表&#xff08;Scroll View&#xff09;来展示道具。这时开发者经常会遇到一个典型问题&#xff1a;当鼠标悬停在滑动区域边缘的道具上…

作者头像 李华
网站建设 2026/7/4 19:06:39

AI辅助游戏开发工具链实战与优化

1. 游戏开发工具链的AI辅助实践在3D游戏开发领域&#xff0c;工具链建设往往决定了团队的生产效率上限。作为从业十余年的技术负责人&#xff0c;我深刻体会到&#xff1a;优秀的工具能让团队效率提升3-5倍&#xff0c;而AI辅助开发正在将这个差距进一步拉大。本系列第二弹聚焦…

作者头像 李华
网站建设 2026/7/4 19:03:09

PSO优化LSSVM参数:工业预测模型调参实战

1. 项目背景与核心价值 粒子群优化&#xff08;PSO&#xff09;算法在参数优化领域的表现一直让我印象深刻&#xff0c;特别是遇到像LSSVM&#xff08;最小二乘支持向量机&#xff09;这种对参数极度敏感的模型时。传统网格搜索不仅耗时费力&#xff0c;还容易陷入局部最优。而…

作者头像 李华