你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
本教程基于前一教程:使用 Azure 通知中心向特定用户发送通知。 该教程介绍了如何向注册给经过身份验证的特定用户的所有设备推送通知。 该方法需要使用多个请求将通知发送到每个受支持的客户端平台。 Azure 通知中心支持模板,可以通过模板指定特定设备要如何接收通知。 此方法简化了发送跨平台通知的方式。
本文演示了如何利用模板发送针对所有平台的通知。 本文使用单个请求发送不区分平台的通知。 有关模板的更多详细信息,请参阅通知中心概述。
备注
通过通知中心,设备可使用同一标记注册多个模板。 在这种情况下,针对该标签的传入的邮件将导致系统向设备发送多个通知(每个通知对应一个模板)。 此过程可以在多个可视通知中显示同一消息,如显示为 Windows 应用商店应用中的徽章和 toast 通知。
备注
Microsoft 推送通知服务 (MPNS) 已弃用,不再受支持。
本部分使用了你在使用 Azure 通知中心向特定用户发送通知教程中构建的示例代码。 可以从 GitHub 下载完整示例。
若要使用模板发送跨平台通知,请执行以下步骤:
在 Visual Studio 的“解决方案资源管理器”中,展开“Controllers”文件夹,然后打开 RegisterController.cs 文件。
在 Put
方法中找到用于创建新注册的代码块,然后将 switch
内容替换为以下代码:
switch (deviceUpdate.Platform)
{
case "mpns":
var toastTemplate = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
"<wp:Toast>" +
"<wp:Text1>$(message)</wp:Text1>" +
"</wp:Toast> " +
"</wp:Notification>";
registration = new MpnsTemplateRegistrationDescription(deviceUpdate.Handle, toastTemplate);
break;
case "wns":
toastTemplate = @"<toast><visual><binding template=""ToastText01""><text id=""1"">$(message)</text></binding></visual></toast>";
registration = new WindowsTemplateRegistrationDescription(deviceUpdate.Handle, toastTemplate);
break;
case "apns":
var alertTemplate = "{\"aps\":{\"alert\":\"$(message)\"}}";
registration = new AppleTemplateRegistrationDescription(deviceUpdate.Handle, alertTemplate);
break;
case "fcm":
var messageTemplate = "{\"data\":{\"message\":\"$(message)\"}}";
registration = new FcmTemplateRegistrationDescription(deviceUpdate.Handle, messageTemplate);
break;
default:
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
此代码调用平台特定的方法来创建模板注册而非本机注册。 由于模板注册派生自本机注册,因此无需修改现有注册。
在“解决方案资源管理器”的“Controllers”文件夹中,打开“NotificationsController.cs”文件。 将 Post
方法替换为以下代码:
public async Task<HttpResponseMessage> Post()
{
var user = HttpContext.Current.User.Identity.Name;
var userTag = "username:" + user;
var notification = new Dictionary<string, string> { { "message", "Hello, " + user } };
await Notifications.Instance.Hub.SendTemplateNotificationAsync(notification, userTag);
return Request.CreateResponse(HttpStatusCode.OK);
}
此代码将通知同时发送到所有平台。 不指定本机有效负载。 通知中心会使用所提供的标记值(在注册的模板中指定)生成正确的负载并将它传递到每个设备。
重新发布 Web API 项目。
再次运行客户端应用来验证注册是否已成功。
可以选择将客户端应用部署到另一个设备,然后运行该应用。 通知会显示在每个设备上。
现在,你已完成本教程,可以查看以下文章来详细了解通知中心和模板: