Skip to content

Commit a06be2b

Browse files
authored
Merge pull request #286164 from sreehari-ms/main
add new page for updating fcm v1.0 credentials using ANH
2 parents b473207 + 99a3577 commit a06be2b

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

articles/notification-hubs/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106
href: firebase-migration-rest.md
107107
- name: FCM migration using Azure SDKs
108108
href: firebase-migration-sdk.md
109+
- name: Updating ANH with FCMv1 Credentials
110+
href: firebase-migration-update-sdk.md
109111
- name: Change pricing tier
110112
href: change-pricing-tier.md
111113
- name: Send cross-platform notifications
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
---
2+
title: Updating Azure notification hub with FCMv1 credentials
3+
description: Describes how Azure Notification Hubs can be updated using FCMv1 credentials
4+
author: sreeharir
5+
manager: nanooka
6+
ms.service: azure-notification-hubs
7+
ms.topic: article
8+
ms.date: 09/12/2024
9+
ms.author: sreeharir
10+
ms.reviewer: sethm
11+
ms.lastreviewed: 09/12/2024
12+
---
13+
14+
# Updating Azure Notification Hub with FCMv1 Credentials
15+
16+
This guide explains how to update an Azure notification hub with FCMv1 credentials using the Azure Management SDK for .NET. This is essential for enabling push notifications to Android devices via Firebase Cloud Messaging (FCMv1).
17+
18+
## Prerequisites
19+
- An existing Azure Notification Hub within a namespace.
20+
- FCMv1 credentials including `clientEmail`, `privateKey`, and `projectId`.
21+
22+
### Step 1: Set up and retrieve the Notification Hub
23+
Before you can update the Notification Hub, ensure that you have set up the `ArmClient` and retrieved the relevant Notification Hub resource.
24+
25+
```csharp
26+
ArmClient client = new ArmClient(new DefaultAzureCredential());
27+
SubscriptionResource subscription = client.GetSubscriptionResource(new ResourceIdentifier($"/subscriptions/{subscriptionId}"));
28+
ResourceGroupResource resourceGroup = subscription.GetResourceGroups().Get(resourceGroupName);
29+
NotificationHubNamespaceResource notificationHubNamespaceResource = resourceGroup.GetNotificationHubNamespaces().Get(namespaceName);
30+
NotificationHubResource notificationHubResource = notificationHubNamespaceResource.GetNotificationHubs().Get(notificationHubName);
31+
```
32+
33+
### Step 2: Define and update FCMv1 credentials
34+
Next, create an `FcmV1Credential` object with your FCMv1 details and use it to update the Notification Hub.
35+
36+
```csharp
37+
NotificationHubUpdateContent updateContent = new()
38+
{
39+
FcmV1Credential = new FcmV1Credential("clientEmail", "privateKey", "projectid")
40+
};
41+
42+
NotificationHubResource updatedNotificationHub = await notificationHubResource.UpdateAsync(updateContent);
43+
Console.WriteLine($"Notification Hub '{notificationHubName}' updated successfully with FCMv1 credentials.");
44+
```
45+
46+
### Step 3: Verify the update
47+
After updating, you can verify the credentials by retrieving and printing them.
48+
49+
```csharp
50+
var notificationHubCredentials = updatedNotificationHub.GetPnsCredentials().Value;
51+
Console.WriteLine($"FCMv1 Credentials Email: '{notificationHubCredentials.FcmV1Credential.ClientEmail}'");
52+
```
53+
54+
This step confirms that the Notification Hub has been updated with the correct FCMv1 credentials.
55+
56+
## Complete code example
57+
Below is the complete code example that includes the setup, creation, update, and verification of the Notification Hub.
58+
59+
```csharp
60+
using Azure;
61+
using Azure.Core;
62+
using Azure.Identity;
63+
using Azure.ResourceManager;
64+
using Azure.ResourceManager.NotificationHubs;
65+
using Azure.ResourceManager.NotificationHubs.Models;
66+
using Azure.ResourceManager.Resources;
67+
68+
class Program
69+
{
70+
static async Task Main(string[] args)
71+
{
72+
string subscriptionId = "<Replace with your subscriptionid>";
73+
string resourceGroupName = "<Replace with your resourcegroupname>";
74+
string location = "<Replace with your location>";
75+
string namespaceName = "<Replace with your notificationhubnamespacename>";
76+
string notificationHubName = "<Replace with your notificationhubname>";
77+
78+
Console.WriteLine("Started Program");
79+
ArmClient client = new ArmClient(new DefaultAzureCredential());
80+
SubscriptionResource subscription = client.GetSubscriptionResource(new ResourceIdentifier($"/subscriptions/{subscriptionId}"));
81+
82+
// Create or get the resource group
83+
ResourceGroupCollection resourceGroups = subscription.GetResourceGroups();
84+
ResourceGroupResource? resourceGroup = null;
85+
bool resourceGroupExists = resourceGroups.Exists(resourceGroupName);
86+
if (!resourceGroupExists)
87+
{
88+
ArmOperation<ResourceGroupResource> operation = await resourceGroups.CreateOrUpdateAsync(WaitUntil.Completed, resourceGroupName, new ResourceGroupData(location));
89+
resourceGroup = operation.Value;
90+
Console.WriteLine($"ResourceGroup '{resourceGroupName}' created successfully.");
91+
}
92+
else
93+
{
94+
resourceGroup = resourceGroups.Get(resourceGroupName);
95+
Console.WriteLine($"ResourceGroup '{resourceGroupName}' already exists.");
96+
}
97+
98+
// Create or get a Notification Hub namespace with the required SKU
99+
NotificationHubNamespaceData namespaceData = new NotificationHubNamespaceData(location)
100+
{
101+
Sku = new NotificationHubSku(NotificationHubSkuName.Standard)
102+
};
103+
104+
NotificationHubNamespaceCollection notificationHubNamespaces = resourceGroup.GetNotificationHubNamespaces();
105+
NotificationHubNamespaceResource? notificationHubNamespaceResource = null;
106+
bool notificationHubNamespaceResourceExists = notificationHubNamespaces.Exists(namespaceName);
107+
if (!notificationHubNamespaceResourceExists)
108+
{
109+
ArmOperation<NotificationHubNamespaceResource> namespaceOperation = await notificationHubNamespaces.CreateOrUpdateAsync(WaitUntil.Completed, namespaceName, namespaceData);
110+
notificationHubNamespaceResource = namespaceOperation.Value;
111+
Console.WriteLine($"Notification Hub Namespace '{namespaceName}' created successfully.");
112+
}
113+
else
114+
{
115+
notificationHubNamespaceResource = notificationHubNamespaces.Get(namespaceName);
116+
Console.WriteLine($"NotificationHubNamespace '{namespaceName}' already exists.");
117+
}
118+
119+
// Create or get a Notification Hub in the namespace
120+
NotificationHubCollection notificationHubs = notificationHubNamespaceResource.GetNotificationHubs();
121+
NotificationHubResource? notiticationHubResource = null;
122+
bool notificationHubResourceExists = notificationHubs.Exists(notificationHubName);
123+
if (!notificationHubResourceExists)
124+
{
125+
ArmOperation<NotificationHubResource> hubOperation = await notificationHubs.CreateOrUpdateAsync(WaitUntil.Completed, notificationHubName, new NotificationHubData(location));
126+
notiticationHubResource = hubOperation.Value;
127+
Console.WriteLine($"Notification Hub '{notificationHubName}' created successfully in Namespace '{namespaceName}'.");
128+
}
129+
else
130+
{
131+
notiticationHubResource = notificationHubs.Get(notificationHubName);
132+
Console.WriteLine($"NotificationHub '{notificationHubName}' already exists.");
133+
}
134+
135+
// Update the Notification Hub with FCMv1 credentials
136+
NotificationHubUpdateContent updateContent = new()
137+
{
138+
FcmV1Credential = new FcmV1Credential("<Replace with your clientEmail>", "<Replace with your privateKey>", "<Replace with your projectid>")
139+
};
140+
141+
NotificationHubResource notificationHubResource = await notiticationHubResource.UpdateAsync(updateContent);
142+
Console.WriteLine($"Notification Hub '{notificationHubName}' updated successfully with FCMv1 credentials.");
143+
144+
// Get Notification Hub Credentials
145+
var notificationHubCredentials = notiticationHubResource.GetPnsCredentials().Value;
146+
Console.WriteLine($"FCMv1 Credentials Email '{notificationHubCredentials.FcmV1Credential.ClientEmail}'");
147+
}
148+
}
149+
```

0 commit comments

Comments
 (0)