使用 FCMv1 凭据更新 Azure 通知中心

本指南介绍如何使用适用于 .NET 的 Azure 管理 SDK 使用 FCMv1 凭据更新 Azure 通知中心。 这对于通过 Firebase Cloud Messaging (FCMv1) 向 Android 设备启用推送通知至关重要。

先决条件

  • 命名空间中的现有 Azure 通知中心。
  • FCMv1 凭据,包括 clientEmailprivateKeyprojectId

步骤 1:设置和检索通知中心

在更新通知中心之前,请确保已设置 ArmClient 并检索到相关的通知中心资源。

ArmClient client = new ArmClient(new DefaultAzureCredential());
SubscriptionResource subscription = client.GetSubscriptionResource(new ResourceIdentifier($"/subscriptions/{subscriptionId}"));
ResourceGroupResource resourceGroup = subscription.GetResourceGroups().Get(resourceGroupName);
NotificationHubNamespaceResource notificationHubNamespaceResource = resourceGroup.GetNotificationHubNamespaces().Get(namespaceName);
NotificationHubResource notificationHubResource = notificationHubNamespaceResource.GetNotificationHubs().Get(notificationHubName);

步骤 2:定义和更新 FCMv1 凭据

接下来,使用 FCMv1 详细信息创建 FcmV1Credential 对象,并使用它更新通知中心。

NotificationHubUpdateContent updateContent = new()
{
    FcmV1Credential = new FcmV1Credential("clientEmail", "privateKey", "projectid")
};

NotificationHubResource updatedNotificationHub = await notificationHubResource.UpdateAsync(updateContent);
Console.WriteLine($"Notification Hub '{notificationHubName}' updated successfully with FCMv1 credentials.");

步骤 3:验证更新

更新后,可以通过检索和打印凭据来验证凭据。

var notificationHubCredentials = updatedNotificationHub.GetPnsCredentials().Value;
Console.WriteLine($"FCMv1 Credentials Email: '{notificationHubCredentials.FcmV1Credential.ClientEmail}'");

此步骤确认通知中心已使用正确的 FCMv1 凭据进行更新。

完整代码示例

下面是完整的代码示例,其中包括通知中心的设置、创建、更新和验证。

using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.NotificationHubs;
using Azure.ResourceManager.NotificationHubs.Models;
using Azure.ResourceManager.Resources;

class Program
{
    static async Task Main(string[] args)
    {
        string subscriptionId = "<Replace with your subscriptionid>";
        string resourceGroupName = "<Replace with your resourcegroupname>";
        string location = "<Replace with your location>";
        string namespaceName = "<Replace with your notificationhubnamespacename>";
        string notificationHubName = "<Replace with your notificationhubname>";

        Console.WriteLine("Started Program");
        ArmClient client = new ArmClient(new DefaultAzureCredential());
        SubscriptionResource subscription = client.GetSubscriptionResource(new ResourceIdentifier($"/subscriptions/{subscriptionId}"));

        // Create or get the resource group
        ResourceGroupCollection resourceGroups = subscription.GetResourceGroups();
        ResourceGroupResource? resourceGroup = null;
        bool resourceGroupExists = resourceGroups.Exists(resourceGroupName);
        if (!resourceGroupExists)
        {
            ArmOperation<ResourceGroupResource> operation = await resourceGroups.CreateOrUpdateAsync(WaitUntil.Completed, resourceGroupName, new ResourceGroupData(location));
            resourceGroup = operation.Value;
            Console.WriteLine($"ResourceGroup '{resourceGroupName}' created successfully.");
        }
        else
        {
            resourceGroup = resourceGroups.Get(resourceGroupName);
            Console.WriteLine($"ResourceGroup '{resourceGroupName}' already exists.");
        }

        // Create or get a Notification Hub namespace with the required SKU
        NotificationHubNamespaceData namespaceData = new NotificationHubNamespaceData(location)
        {
            Sku = new NotificationHubSku(NotificationHubSkuName.Standard)
        };

        NotificationHubNamespaceCollection notificationHubNamespaces = resourceGroup.GetNotificationHubNamespaces();
        NotificationHubNamespaceResource? notificationHubNamespaceResource = null;
        bool notificationHubNamespaceResourceExists = notificationHubNamespaces.Exists(namespaceName);
        if (!notificationHubNamespaceResourceExists)
        {
            ArmOperation<NotificationHubNamespaceResource> namespaceOperation = await notificationHubNamespaces.CreateOrUpdateAsync(WaitUntil.Completed, namespaceName, namespaceData);
            notificationHubNamespaceResource = namespaceOperation.Value;
            Console.WriteLine($"Notification Hub Namespace '{namespaceName}' created successfully.");
        }
        else
        {
            notificationHubNamespaceResource = notificationHubNamespaces.Get(namespaceName);
            Console.WriteLine($"NotificationHubNamespace '{namespaceName}' already exists.");
        }

        // Create or get a Notification Hub in the namespace
        NotificationHubCollection notificationHubs = notificationHubNamespaceResource.GetNotificationHubs();
        NotificationHubResource? notificationHubResource = null;
        bool notificationHubResourceExists = notificationHubs.Exists(notificationHubName);
        if (!notificationHubResourceExists)
        {
            ArmOperation<NotificationHubResource> hubOperation = await notificationHubs.CreateOrUpdateAsync(WaitUntil.Completed, notificationHubName, new NotificationHubData(location));
            notificationHubResource = hubOperation.Value;
            Console.WriteLine($"Notification Hub '{notificationHubName}' created successfully in Namespace '{namespaceName}'.");
        }
        else
        {
            notificationHubResource = notificationHubs.Get(notificationHubName);
            Console.WriteLine($"NotificationHub '{notificationHubName}' already exists.");
        }

        // Update the Notification Hub with FCMv1 credentials
        NotificationHubUpdateContent updateContent = new()
        {
            FcmV1Credential = new FcmV1Credential("<Replace with your clientEmail>", "<Replace with your privateKey>", "<Replace with your projectid>")
        };

        NotificationHubResource notificationHubResource = await notificationHubResource.UpdateAsync(updateContent);
        Console.WriteLine($"Notification Hub '{notificationHubName}' updated successfully with FCMv1 credentials.");

        // Get Notification Hub Credentials
        var notificationHubCredentials = notificationHubResource.GetPnsCredentials().Value;
        Console.WriteLine($"FCMv1 Credentials Email '{notificationHubCredentials.FcmV1Credential.ClientEmail}'");
    }
}