-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathsendDirectNotificationBatch.ts
More file actions
98 lines (83 loc) · 3.1 KB
/
sendDirectNotificationBatch.ts
File metadata and controls
98 lines (83 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* This sample demonstrates how the sendDirectNotification() method can be used to send a direct batch
* notification using APNs. This sends a JSON message to an APNs given device token and returns
* a Tracking ID which can be used for troubleshooting with the Azure Notification Hubs team.
*
* See https://learn.microsoft.com/rest/api/notificationhubs/direct-batch-send
* to learn about Direct Send Batch.
*
*
* @summary Demonstrates how to send direct notifications using Azure Notification Hubs
* @azsdk-weight 100
*/
import "dotenv/config";
import {
type NotificationDetails,
type NotificationOutcomeState,
createAppleNotification,
} from "@azure/notification-hubs/models";
import {
type NotificationHubsClientContext,
createClientContext,
getNotificationOutcomeDetails,
sendNotification,
} from "@azure/notification-hubs/api";
import { delay } from "@azure/core-util";
import { isRestError } from "@azure/core-rest-pipeline";
// Define connection string and hub name
const connectionString = process.env.NOTIFICATIONHUBS_CONNECTION_STRING || "<connection string>";
const hubName = process.env.NOTIFICATION_HUB_NAME || "<hub name>";
// Define message constants
const DUMMY_DEVICE = "00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0";
const deviceHandle = process.env.APNS_DEVICE_TOKENS?.split(",") || [DUMMY_DEVICE];
async function main(): Promise<void> {
const context = createClientContext(connectionString, hubName);
const messageBody = `{ "aps" : { "alert" : { title: "Hello", body: "Hello there SDK Review!" } } }`;
const notification = createAppleNotification({
body: messageBody,
headers: {
"apns-priority": "10",
"apns-push-type": "alert",
},
});
const result = await sendNotification(context, notification, { deviceHandle });
console.log(`Direct send Tracking ID: ${result.trackingId}`);
console.log(`Direct send Correlation ID: ${result.correlationId}`);
// Only available in Standard SKU and above
if (result.notificationId) {
console.log(`Direct send Notification ID: ${result.notificationId}`);
const results = await getNotificationDetails(context, result.notificationId);
if (results) {
console.log(JSON.stringify(results, null, 2));
}
}
}
async function getNotificationDetails(
context: NotificationHubsClientContext,
notificationId: string,
): Promise<NotificationDetails | undefined> {
let state: NotificationOutcomeState = "Enqueued";
let count = 0;
let result: NotificationDetails | undefined;
while ((state === "Enqueued" || state === "Processing") && count++ < 10) {
try {
result = await getNotificationOutcomeDetails(context, notificationId);
state = result.state!;
} catch (e) {
// Possible to get 404 for when it doesn't exist yet.
if (isRestError(e) && e.statusCode === 404) {
continue;
} else {
throw e;
}
}
await delay(1000);
}
return result;
}
main().catch((err) => {
console.log("sendDirectNotification Sample: Error occurred: ", err);
process.exit(1);
});