Skip to content

Commit d3f1a30

Browse files
Merge pull request #268838 from sethmanheim/anh-bpcode
Notification Hubs: add .NET SDK examples
2 parents da206fa + 3554099 commit d3f1a30

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

articles/notification-hubs/android-sdk.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ title: Send push notifications to Android using Azure Notification Hubs and Fire
33
description: In this tutorial, you learn how to use Azure Notification Hubs and Google Firebase Cloud Messaging to send push notifications to Android devices (version 1.0.0-preview1).
44
author: sethmanheim
55
ms.author: sethm
6-
ms.date: 06/30/2023
6+
ms.date: 03/14/2024
77
ms.topic: tutorial
88
ms.service: notification-hubs
9-
ms.reviewer: thsomasu
10-
ms.lastreviewed: 05/27/2020
9+
ms.reviewer: heathertian
10+
ms.lastreviewed: 03/14/2024
1111
ms.custom: devx-track-csharp
1212
---
1313

@@ -32,6 +32,9 @@ This tutorial covers the following steps:
3232

3333
To complete this tutorial, you must have an active Azure account. If you don't have an account, you can create a free trial account in just a couple of minutes. For details, see [Azure Free Trial](https://azure.microsoft.com/free/).
3434

35+
> [!NOTE]
36+
> Google/Firebase APIs are not supported in Azure China regions.
37+
3538
You also need the following items:
3639

3740
- The latest version of [Android Studio](https://go.microsoft.com/fwlink/?LinkId=389797) is recommended.

articles/notification-hubs/browser-push.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ manager: femila
77
ms.service: notification-hubs
88
ms.tgt_pltfrm: mobile-multiple
99
ms.topic: article
10-
ms.date: 12/06/2023
10+
ms.date: 03/14/2024
1111
ms.author: sethm
1212
ms.reviewer: heathertian
13-
ms.lastreviewed: 12/06/2023
13+
ms.lastreviewed: 03/14/2024
1414
---
1515

1616
# Web push notifications with Azure Notification Hubs (preview)
@@ -25,6 +25,7 @@ At a high level, the process is:
2525
1. [Set credentials](#set-credentials):
2626
- [In the Azure portal](#set-credentials-in-azure-portal)
2727
- [Using the REST API](#set-credentials-using-rest-api)
28+
- [Using the Azure SDKs](#set-credentials-using-azure-sdks)
2829

2930
2. [Create registrations and installations](#create-registrations-and-installations).
3031

@@ -107,6 +108,28 @@ Enter the credentials in this format, providing the subscription ID, resource gr
107108
https://management.azure.com/subscriptions/{subcription}/resourceGroups/{resource-group}/providers/Microsoft.NotificationHubs/namespaces/{namespace}/notificationHubs/{hub}api-version=2016-03-01
108109
```
109110

111+
### Set credentials using Azure SDKs
112+
113+
You can set the credentials for Browser Push using the Azure SDKs. Here's an example using the .NET SDK:
114+
115+
```csharp
116+
var browserCreds = new BrowserCredential
117+
{
118+
Subject = "<subject>",
119+
VapidPublicKey = "<vapid public key>",
120+
VapidPrivateKey = "<vapid private key>",
121+
}
122+
```
123+
124+
and:
125+
126+
```csharp
127+
await nhManagementClient.NotificationHubs.CreateOrUpdateAsync(config.ResourceGroupName, config.NamespaceName, config.HubName, new NotificationHubCreateOrUpdateParameters(config.Location)
128+
{
129+
BrowserCredential = browserCreds
130+
});
131+
```
132+
110133
## Create registrations and installations
111134

112135
Bulk sends require registrations or installations. You can also use the registrations and installations in debug sends.
@@ -149,6 +172,40 @@ The following examples show the registration request body for a native registrat
149172
}
150173
```
151174

175+
### Create native registrations (SDK)
176+
177+
```csharp
178+
await notificationHubClient.CreateBrowserNativeRegistrationAsync(subscriptionInfo, tagSet);
179+
```
180+
181+
### Create template registrations (SDK)
182+
183+
```csharp
184+
await notificationHubClient.CreateBrowserTemplateRegistrationAsync(subscriptionInfo, template, tagSet);
185+
```
186+
187+
### Create browser installations (SDK)
188+
189+
```csharp
190+
var browserPushSubscription = new BrowserPushSubscription
191+
{
192+
Endpoint = "",
193+
P256DH = "",
194+
Auth = "",
195+
};
196+
197+
var browserInstallation = new BrowserInstallation
198+
{
199+
InstallationId = installationId,
200+
Tags = tags,
201+
Subscription = browserPushSubscription,
202+
UserId = userId,
203+
ExpirationTime = DateTime.UtcNow.AddDays(1),
204+
};
205+
206+
await notificationHubClient.CreateOrUpdateInstallationAsync(browserInstallation);
207+
```
208+
152209
## Send push notifications
153210

154211
After you [set credentials for browser push](#set-credentials) and [create registrations and installations](#create-registrations-and-installations) for the devices, you're ready to create push notifications. This section describes how to create a notification for a [direct send|](#create-direct-sends), [audience send](#create-audience-sends), and [debug (test) send](#create-debugtest-sends).
@@ -179,6 +236,19 @@ To create a direct send notification, follow these steps:
179236

180237
1. Send the notification.
181238

239+
You can also use the .NET SDK to create a direct send:
240+
241+
```csharp
242+
var browserSubscriptionEndpoint = "";
243+
var browserPushHeaders = new Dictionary<string, string>
244+
{
245+
{ "P256DH", "" },
246+
{ "Auth", "" },
247+
};
248+
249+
var directSendOutcome = await notificationHubClient.SendDirectNotificationAsync(new BrowserNotification("payload", browserPushHeaders), browserSubscriptionEndpoint);
250+
```
251+
182252
### Create audience sends
183253

184254
For an audience send, use the same `ServiceBus Notification-Format` header used for a direct send, and modify the message payload as desired. Optionally, specify a tag expression using the `ServiceBusNotification-Tags` header. For more information about creating an audience send, see [Send an APNS native notification](/rest/api/notificationhubs/send-apns-native-notification).

0 commit comments

Comments
 (0)