Skip to content

Commit 9994520

Browse files
authored
Create firebase-migration-update-sdk.md
1 parent e94f396 commit 9994520

File tree

1 file changed

+150
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)