你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Azure 通知中心提供易于使用和横向扩展的推送引擎,可用于从任何后端(云或本地)将通知发送到任何平台(iOS、Android、Windows、Kindle、Baidu 等)。 有关该服务的详细信息,请参阅 什么是 Azure 通知中心?。
在本快速入门中,你将使用 Azure CLI 创建通知中心。 第一部分提供创建通知中心命名空间的步骤。 第二部分提供在现有命名空间中创建通知中心的步骤。 你还将了解如何创建自定义访问策略。
如果没有 Azure 订阅,请在开始之前创建一个免费帐户。
先决条件
在 Azure Cloud Shell 中使用 Bash 环境。 有关详细信息,请参阅 Azure Cloud Shell 入门。
如需在本地运行 CLI 参考命令,请安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI。
如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅 使用 Azure CLI 向 Azure 进行身份验证。
出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展的详细信息,请参阅 使用和管理 Azure CLI 中的扩展。
运行az version命令,以查看已安装的版本和依赖库。 若要升级到最新版本,请运行az upgrade。
重要
通知中心需要 Azure CLI 2.0.67 或更高版本。 运行az version命令,以查看已安装的版本和依赖库。 若要升级到最新版本,请运行az upgrade。
创建资源组
Azure 通知中心(与所有 Azure 资源一样)必须部署到资源组中。 使用资源组可以组织和管理相关的 Azure 资源。 请参阅 什么是 Azure 资源管理器 ,了解有关资源组的详细信息。
在本快速入门中,使用以下 az group create 命令在 eastus 位置创建名为 spnhubrg 的资源组。
az group create --name spnhubrg --location eastus
创建通知中心命名空间
为通知中心创建命名空间。
命名空间包含一个或多个中心,该名称在所有 Azure 订阅中必须是唯一的,并且长度至少为 6 个字符。 若要检查名称的可用性,请使用 az notification-hub namespace check-availability 命令。
az notification-hub namespace check-availability --name spnhubnsAzure CLI 会通过显示以下控制台输出来响应您关于可用性的请求:
{ "id": "/subscriptions/yourSubscriptionID/providers/Microsoft.NotificationHubs/checkNamespaceAvailability", "isAvailable": true, "location": null, "name": "spnhubns", "properties": false, "sku": null, "tags": null, "type": "Microsoft.NotificationHubs/namespaces/checkNamespaceAvailability" }请注意 Azure CLI 响应
"isAvailable": true中的第二行。 如果您指定的命名空间所需名称不可用,此行将显示false。 确认名称的可用性后,请运行 az notification-hub namespace create 命令来创建命名空间。az notification-hub namespace create --resource-group spnhubrg --name spnhubns --location eastus --sku Free如果您提供给
az notification-hub namespace create命令的--name不可用,或者不符合 Azure 资源的命名规则和限制,Azure CLI 会显示以下控制台输出:#the name is not available The specified name is not available. For more information visit https://aka.ms/eventhubsarmexceptions. #the name is invalid The specified service namespace is invalid.如果尝试的第一个名称未成功,请为新命名空间选择其他名称,然后再次运行
az notification-hub namespace create该命令。注释
从此步骤开始,您必须替换从本快速入门复制的每个 Azure CLI 命令中的
--namespace参数值。获取命名空间列表。
若要查看有关新命名空间的详细信息,请使用 az notification-hub namespace list 命令。 如果要查看订阅的所有命名空间,则
--resource-group参数是可选的。az notification-hub namespace list --resource-group spnhubrg
创建通知中心
创建第一个通知中心。
现在可以在新命名空间中创建一个或多个通知中心。 运行 az notification-hub create 命令以创建通知中心。
az notification-hub create --resource-group spnhubrg --namespace-name spnhubns --name spfcmtutorial1nhub --location eastus创建第二个通知中心。
可以在单个命名空间中创建多个通知中心。 若要在同一命名空间中创建第二个通知中心,请使用其他中心名称再次运行
az notification-hub create该命令。az notification-hub create --resource-group spnhubrg --namespace-name spnhubns --name mysecondnhub --location eastus获取通知中心列表。
Azure CLI 会在执行每个命令后返回成功或错误消息;不过你一定能够查询通知中心的列表。 az notification-hub list 命令旨在实现此目的。
az notification-hub list --resource-group spnhubrg --namespace-name spnhubns --output table
使用访问策略
Azure 通知中心通过访问策略使用 共享访问签名安全性 。 创建通知中心时,会自动创建两个策略。 需要这些策略中的连接字符串才能配置推送通知。 az notification-hub authorization-rule list 命令提供策略名称及其各自的资源组的列表。
az notification-hub authorization-rule list --resource-group spnhubrg --namespace-name spnhubns --notification-hub-name spfcmtutorial1nhub --output table重要
请不要在应用程序中使用 DefaultFullSharedAccessSignature 策略。 此策略仅用于后端。 仅在
Listen客户端应用程序中使用访问策略。如果要创建具有有意义名称的其他授权规则,可以使用 az notification-hub authorization-rule create 命令创建和自定义自己的访问策略。
--rights参数是一个通过空格分隔的、要分配的权限列表。az notification-hub authorization-rule create --resource-group spnhubrg --namespace-name spnhubns --notification-hub-name spfcmtutorial1nhub --name spnhub1key --rights Listen Manage Send每个访问策略都有两组密钥和连接字符串。 稍后需要用到它们来 配置通知中心。 若要列出通知中心访问策略的密钥和连接字符串,请使用 az notification-hub authorization-rule list-keys 命令。
# query the keys and connection strings for DefaultListenSharedAccessSignature az notification-hub authorization-rule list-keys --resource-group spnhubrg --namespace-name spnhubns --notification-hub-name spfcmtutorial1nhub --name DefaultListenSharedAccessSignature --output table# query the keys and connection strings for a custom policy az notification-hub authorization-rule list-keys --resource-group spnhubrg --namespace-name spnhubns --notification-hub-name spfcmtutorial1nhub --name spnhub1key --output table
清理资源
如果不再需要资源组,请使用 az group delete 命令删除资源组和所有相关资源:
az group delete --name spnhubrg
后续步骤
在本快速入门中,你创建了一个通知中心。 若要了解如何使用平台通知系统(PNS)设置配置中心,请参阅 在通知中心设置推送通知
了解使用 Azure CLI 管理通知中心的广泛功能: