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通知 | 以自定义的格式将信息直接发送到您的应用程序。 |
推送通知的工作流
- Window Phone客户端应用程序请求与微软推送通知服务(Microsoft Push Notification Services)建立通道连接,微软推送通知服务(Microsoft Push Notification Services)使用通道URI响应。
- Window Phone客户端应用程序向监视服务(Web Service或者Cloud Application)发送包含推送通知服务通道URI以及负载的消息。
- 当监视服务检测到信息更改时(如航班取消、航班延期或天气警报),它会向微软推送通知服务(Microsoft Push Notification Services)发送消息。
- 微软推送通知服务(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类,以及三个子类:RawPushNotificationMessage、TilePushNotificationMessage和ToastPushNotificationMessage。如图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();