快速入门:使用 Azure CLI 创建 Azure 通知中心Quickstart: Create an Azure notification hub using the Azure CLI

Azure 通知中心提供易于使用且横向扩展的推送引擎,可用于从任何后端(云或本地)向任何平台(iOS、Android、Windows、Kindle、百度等)发送通知。Azure Notification Hubs provide an easy-to-use and scaled-out push engine that allows you to send notifications to any platform (iOS, Android, Windows, Kindle, Baidu, etc.) from any backend (cloud or on-premises). 有关此服务的详细信息,请参阅什么是 Azure 通知中心?For more information about the service, see What is Azure Notification Hubs?.

在本快速入门中,你将使用 Azure CLI 创建通知中心。In this quickstart, you create a notification hub using the Azure CLI. 第一部分提供创建通知中心命名空间的步骤。The first section gives you steps to create a Notification Hubs namespace. 第二部分提供在现有命名空间中创建通知中心的步骤。The second section gives you steps to create a notification hub in an existing namespace. 此外,介绍如何创建自定义访问策略。You also learn how to create a custom access policy.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

通知中心需要 Azure CLI 2.0.67 或更高版本。Notification Hubs requires version 2.0.67 or later of the Azure CLI. 运行 az --version 查看安装的版本和依赖库。Run az --version to find the version and dependent libraries that are installed. 若要安装或升级,请参阅安装 Azure CLITo install or upgrade, see Install Azure CLI.

准备环境Prepare your environment

  1. 如果使用的是 CLI 的本地安装,请使用 az login 命令登录。Sign in using the az login command if you're using a local install of the CLI.

    az cloud set -n AzureChinaCloud
    az login
    

    遵循终端中显示的步骤完成身份验证过程。Follow the steps displayed in your terminal to complete the authentication process.

  2. 使用 Azure CLI 的扩展引用时,必须先安装该扩展。When working with extension references for the Azure CLI, you must first install the extension. 借助 Azure CLI 扩展,可访问尚未在核心 CLI 中提供的试验性和预发布的命令。Azure CLI extensions give you access to experimental and pre-release commands that have not yet shipped as part of the core CLI. 若要详细了解包含更新和卸载的扩展,请参阅使用 Azure CLI 的扩展To learn more about extensions including updating and uninstalling, see Use extensions with Azure CLI.

    运行以下命令安装通知中心扩展Install the extension for Notification Hubs by running the following command:

    az extension add --name notification-hub
    
  3. 创建资源组。Create a resource group.

    与所有 Azure 资源一样,Azure 通知中心必须部署到资源组中。Azure Notification Hubs, like all Azure resources, must be deployed into a resource group. 使用资源组可以组织和管理相关的 Azure 资源。Resource groups allow you to organize and manage related Azure resources.

    对于本快速入门,请使用以下 az group create 命令在 chinaeast 位置创建名为“spnhubrg”的资源组:For this quickstart, create a resource group named spnhubrg in the chinaeast location with the following az group create command:

    az group create --name spnhubrg --location chinaeast
    

创建通知中心命名空间Create a Notification Hubs namespace

  1. 创建通知中心的命名空间。Create a namespace for your notification hubs.

    命名空间包含一个或多个中心,其名称在所有 Azure 订阅上必须保持唯一且长度至少为六个字符。A namespace contains one or more hubs, and the name must be unique across all Azure subscriptions and be at least six characters in length. 若要检查名称是否可用,请使用 az notification-hub namespace check-availability 命令。To check the availability of a name, use the az notification-hub namespace check-availability command.

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

    Azure CLI 通过显示以下控制台输出来响应你的可用性请求:Azure CLI responds to your request for availability by displaying the following console output:

    {
    "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": trueNotice the second line in the Azure CLI response, "isAvailable": true. 如果为命名空间指定的所需名称不可用,此行将读取 falseThis line reads false if the desired name you specified for the namespace is not available. 确认名称的可用性后,请运行 az notification-hub namespace create 命令来创建命名空间。Once you have confirmed availability of the name, run the az notification-hub namespace create command to create your namespace.

    az notification-hub namespace create --resource-group spnhubrg --name spnhubns  --location chinaeast --sku Free
    

    如果提供给 az notification-hub namespace create 命令的 --name 不可用,或不满足 Azure 资源的命名规则和限制,Azure CLI 将使用以下控制台输出进行响应:If the --name you provided to the az notification-hub namespace create command is not available, or does not meet the Naming rules and restrictions for Azure resources, Azure CLI responds with the following console output:

    #the name is not available
    The specified name is not available. For more information visit https://docs.azure.cn/event-hubs/resource-manager-exceptions.
    
    #the name is invalid
    The specified service namespace is invalid.
    

    如果尝试的第一个名称不成功,请为新命名空间选择其他名称,然后再次运行 az notification-hub namespace create 命令。If the first name you tried is not successful, select a different name for your new namespace and run the az notification-hub namespace create command again.

    备注

    在此步骤中,必须在从此快速入门中复制的每个 Azure CLI 命令中替换 --namespace 参数的值。From this step forward you must replace the value of the --namespace parameter in each Azure CLI command you copy from this quickstart.

  2. 获取命名空间的列表。Get a list of namespaces.

    若要查看有关新命名空间的详细信息,请使用 az notification-hub namespace list 命令。To see the details about your new namespace use the az notification-hub namespace list command. 如果要查看订阅的所有命名空间,则 --resource-group 参数为可选。The --resource-group parameter is optional if you want to see all namespaces for a subscription.

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

创建通知中心Create notification hubs

  1. 创建第一个通知中心。Create your first notification hub.

    现在,可在新的命名空间中创建一个或多个通知中心。One or more notification hubs can now be created in your new namespace. 运行 az notification-hub create 命令创建通知中心。Run the az notification-hub create command to create a notification hub.

    az notification-hub create --resource-group spnhubrg --namespace-name spnhubns --name spfcmtutorial1nhub --location chinaeast --sku Free
    
  2. 创建另一个通知中心。Create a second notification hub.

    可以在一个命名空间中创建多个通知中心。Multiple notification hubs can be created in a single namespace. 若要在同一个命名空间中创建另一个通知中心,请使用不同的中心名称再次运行 az notification-hub create 命令。To create a second notification hub in the same namespace, run the az notification-hub create command again using a different hub name.

    az notification-hub create --resource-group spnhubrg --namespace-name spnhubns --name mysecondnhub --location chinaeast --sku Free
    
  3. 获取通知中心的列表。Get a list of notification hubs.

    Azure CLI 会在执行每个命令后返回成功或错误消息;不过你一定能够查询通知中心的列表。Azure CLI returns either a success or error message with each executed command; however, being able to query for a list of notification hubs is reassuring. az notification-hub list 命令旨在实现此目的。The az notification-hub list command was designed for this purpose.

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

使用访问策略Work with access policies

  1. Azure 通知中心通过使用访问策略来获得共享访问签名安全性Azure Notification Hubs uses shared access signature security through the use of access policies. 创建通知中心时,会自动创建两个策略。Two policies are created automatically when you create a notification hub. 需要这些策略中的连接字符串来配置推送通知。The connection strings from these policies are needed to configure push notifications. az notification-hub authorization-rule list 命令提供策略名称及其各自资源组的列表。The az notification-hub authorization-rule list command provides a list of policy names and their respective resource groups.

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

    重要

    请不要在应用程序中使用 DefaultFullSharedAccessSignature 策略。Do not use the DefaultFullSharedAccessSignature policy in your application. 此策略只能在后端使用。This policy is meant to be used in your back-end only. 请仅在客户端应用程序中使用 Listen 访问策略。Use only Listen access policies in your client application.

  2. 如果要使用有意义的名称创建其他授权规则,可使用 az notification-hub authorization-rule create 命令来创建和自定义你自己的访问策略。If you want to create additional authorization rules with meaningful names, you can create and customize your own access policy by using the az notification-hub authorization-rule create command. --rights 参数是要分配的权限的空格分隔列表。The --rights parameter is a space delimited list of the permissions you want to assign.

    az notification-hub authorization-rule create --resource-group spnhubrg --namespace-name spnhubns --notification-hub-name spfcmtutorial1nhub --name spnhub1key --rights Listen Manage Send
    
  3. 每个访问策略有两组密钥和连接字符串。There are two sets of keys and connection strings for each access policy. 稍后在配置通知中心时需要它们。You'll need them later to configure a notification hub. 若要列出通知中心访问策略的密钥和连接字符串,请使用 az notification-hub authorization-rule list-keys 命令。To list the keys and connection strings for a Notification Hubs access policy, use the az notification-hub authorization-rule list-keys command.

    # 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
    

    备注

    通知中心命名空间通知中心具有不同的访问策略。A Notification Hubs namespace and a notification hub have separate access policies. 查询密钥和连接字符串时,请确保使用正确的 Azure CLI 参考内容。Make sure you are using the correct Azure CLI reference when querying for keys and connection strings.

清理资源Clean up resources

不再需要上述资源组和所有相关资源时,请使用 az group delete 命令将其删除。When no longer needed, use the az group delete command to remove the resource group, and all related resources.

az group delete --name spnhubrg

后续步骤Next steps