你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Azure CLI 创建 Azure 通知中心

Azure 通知中心提供易于使用和横向扩展的推送引擎,可用于从任何后端(云或本地)将通知发送到任何平台(iOS、Android、Windows、Kindle、Baidu 等)。 有关该服务的详细信息,请参阅 什么是 Azure 通知中心?

在本快速入门中,你将使用 Azure CLI 创建通知中心。 第一部分提供创建通知中心命名空间的步骤。 第二部分提供在现有命名空间中创建通知中心的步骤。 你还将了解如何创建自定义访问策略。

如果没有 Azure 订阅,请在开始之前创建一个免费帐户

先决条件

重要

通知中心需要 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

创建通知中心命名空间

  1. 为通知中心创建命名空间。

    命名空间包含一个或多个中心,该名称在所有 Azure 订阅中必须是唯一的,并且长度至少为 6 个字符。 若要检查名称的可用性,请使用 az notification-hub namespace check-availability 命令。

    az notification-hub namespace check-availability --name spnhubns
    

    Azure 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 参数值。

  2. 获取命名空间列表。

    若要查看有关新命名空间的详细信息,请使用 az notification-hub namespace list 命令。 如果要查看订阅的所有命名空间,则 --resource-group 参数是可选的。

    az notification-hub namespace list --resource-group spnhubrg
    

创建通知中心

  1. 创建第一个通知中心。

    现在可以在新命名空间中创建一个或多个通知中心。 运行 az notification-hub create 命令以创建通知中心。

    az notification-hub create --resource-group spnhubrg --namespace-name spnhubns --name spfcmtutorial1nhub --location eastus
    
  2. 创建第二个通知中心。

    可以在单个命名空间中创建多个通知中心。 若要在同一命名空间中创建第二个通知中心,请使用其他中心名称再次运行 az notification-hub create 该命令。

    az notification-hub create --resource-group spnhubrg --namespace-name spnhubns --name mysecondnhub --location eastus 
    
  3. 获取通知中心列表。

    Azure CLI 会在执行每个命令后返回成功或错误消息;不过你一定能够查询通知中心的列表。 az notification-hub list 命令旨在实现此目的。

    az notification-hub list --resource-group spnhubrg --namespace-name spnhubns --output table
    

使用访问策略

  1. 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 客户端应用程序中使用访问策略。

  2. 如果要创建具有有意义名称的其他授权规则,可以使用 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
    
  3. 每个访问策略都有两组密钥和连接字符串。 稍后需要用到它们来 配置通知中心。 若要列出通知中心访问策略的密钥和连接字符串,请使用 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
    

    注释

    通知中心命名空间通知中心具有单独的访问策略。 在查询密钥和连接字符串时,请确保使用正确的 Azure CLI 引用。

清理资源

如果不再需要资源组,请使用 az group delete 命令删除资源组和所有相关资源:

az group delete --name spnhubrg

后续步骤