@azure/notification-hubs 2.0.0-alpha.20240919.1 → 2.0.0-alpha.20240920.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +114 -189
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -53,20 +53,15 @@ Once created, the Notification Hub can be configured using the [Azure Portal or
|
|
|
53
53
|
|
|
54
54
|
This SDK for JavaScript offers two ways of interacting with Azure Notification Hubs, either through the class-based approach, or with a modular design approach. The class-based approach is consistent across all packages to create a client and then interact with the methods on the client.
|
|
55
55
|
|
|
56
|
-
```
|
|
57
|
-
import {
|
|
58
|
-
NotificationHubsClient,
|
|
59
|
-
createAppleInstallation
|
|
60
|
-
} from "@azure/notification-hubs";
|
|
56
|
+
```ts snippet:importing_classical
|
|
57
|
+
import { NotificationHubsClient, createAppleInstallation } from "@azure/notification-hubs";
|
|
61
58
|
|
|
62
59
|
const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
63
|
-
|
|
64
60
|
const installation = createAppleInstallation({
|
|
65
61
|
installationId: "<installation-id>",
|
|
66
62
|
pushChannel: "<push-channel>",
|
|
67
63
|
tags: ["likes_javascript"],
|
|
68
64
|
});
|
|
69
|
-
|
|
70
65
|
const result = await client.createOrUpdateInstallation(installation);
|
|
71
66
|
```
|
|
72
67
|
|
|
@@ -81,18 +76,16 @@ The following subpaths are exposed:
|
|
|
81
76
|
|
|
82
77
|
The above code snippet then becomes the following:
|
|
83
78
|
|
|
84
|
-
```
|
|
79
|
+
```ts snippet:importing_modular
|
|
85
80
|
import { createClientContext, createOrUpdateInstallation } from "@azure/notification-hubs/api";
|
|
86
|
-
import { createAppleInstallation } from "@azure/notification-hubs
|
|
81
|
+
import { createAppleInstallation } from "@azure/notification-hubs";
|
|
87
82
|
|
|
88
83
|
const context = createClientContext("<connection string>", "<hub name>");
|
|
89
|
-
|
|
90
84
|
const installation = createAppleInstallation({
|
|
91
85
|
installationId: "<installation-id>",
|
|
92
86
|
pushChannel: "<push-channel>",
|
|
93
87
|
tags: ["likes_javascript"],
|
|
94
88
|
});
|
|
95
|
-
|
|
96
89
|
const result = await createOrUpdateInstallation(context, installation);
|
|
97
90
|
```
|
|
98
91
|
|
|
@@ -104,7 +97,7 @@ Listen allows for a client to register itself via the Registration and Installat
|
|
|
104
97
|
|
|
105
98
|
A new `NotificationHubsClient` client can be created using the constructor with the connection string and Notification Hub name.
|
|
106
99
|
|
|
107
|
-
```
|
|
100
|
+
```ts snippet:authenticating_classical
|
|
108
101
|
import { NotificationHubsClient } from "@azure/notification-hubs";
|
|
109
102
|
|
|
110
103
|
const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
@@ -112,7 +105,7 @@ const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
|
112
105
|
|
|
113
106
|
Using the modular approach, the `createClientContext` can be imported via the `"@azure/notification-hubs/api"` subpath.
|
|
114
107
|
|
|
115
|
-
```
|
|
108
|
+
```ts snippet:authenticating_modular
|
|
116
109
|
import { createClientContext } from "@azure/notification-hubs/api";
|
|
117
110
|
|
|
118
111
|
const context = createClientContext("<connection string>", "<hub name>");
|
|
@@ -140,97 +133,81 @@ Installations are a newer and native JSON approach to device management that con
|
|
|
140
133
|
|
|
141
134
|
Installations can be created through the `createOrUpdateInstallation` method such as the following:
|
|
142
135
|
|
|
143
|
-
```
|
|
136
|
+
```ts snippet:createOrUpdateInstallation_classical
|
|
144
137
|
import { NotificationHubsClient, createAppleInstallation } from "@azure/notification-hubs";
|
|
145
|
-
import { v4 as uuid } from "uuid";
|
|
146
138
|
|
|
147
139
|
const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
148
|
-
|
|
149
140
|
// Create an installation for APNs
|
|
150
|
-
|
|
151
|
-
installationId:
|
|
141
|
+
const installation = createAppleInstallation({
|
|
142
|
+
installationId: "0d8ab095-c449-493f-9195-17e4917806c4", // Must be unique
|
|
152
143
|
pushChannel: "00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0", // PNS specific handle
|
|
153
144
|
tags: ["likes_hockey", "likes_football"],
|
|
154
145
|
});
|
|
155
|
-
|
|
156
|
-
installation = await client.createOrUpdateInstallation(installation);
|
|
146
|
+
const response = await client.createOrUpdateInstallation(installation);
|
|
157
147
|
```
|
|
158
148
|
|
|
159
149
|
Using the modular approach, the code would be as follows:
|
|
160
150
|
|
|
161
|
-
```
|
|
151
|
+
```ts snippet:createOrUpdateInstallation_modular
|
|
162
152
|
import { createClientContext, createOrUpdateInstallation } from "@azure/notification-hubs/api";
|
|
163
|
-
import { createAppleInstallation } from "@azure/notification-hubs
|
|
164
|
-
import { v4 as uuid } from "uuid";
|
|
153
|
+
import { createAppleInstallation } from "@azure/notification-hubs";
|
|
165
154
|
|
|
166
155
|
const context = createClientContext("<connection string>", "<hub name>");
|
|
167
|
-
|
|
168
156
|
// Create an installation for APNs
|
|
169
|
-
|
|
170
|
-
installationId:
|
|
157
|
+
const installation = createAppleInstallation({
|
|
158
|
+
installationId: "0d8ab095-c449-493f-9195-17e4917806c4", // Must be unique
|
|
171
159
|
pushChannel: "00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0", // PNS specific handle
|
|
172
160
|
tags: ["likes_hockey", "likes_football"],
|
|
173
161
|
});
|
|
174
|
-
|
|
175
|
-
installation = await createOrUpdateInstallation(context, installation);
|
|
162
|
+
const response = await createOrUpdateInstallation(context, installation);
|
|
176
163
|
```
|
|
177
164
|
|
|
178
165
|
An update to an installation can be made through the JSON Patch schema such as adding a tag and a user ID using the `updateInstallation` method.
|
|
179
166
|
|
|
180
|
-
```
|
|
167
|
+
```ts snippet:updateInstallation_classical
|
|
181
168
|
import { NotificationHubsClient, JsonPatch } from "@azure/notification-hubs";
|
|
182
169
|
|
|
183
170
|
const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
184
|
-
|
|
185
171
|
const installationId = "<unique installation ID>";
|
|
186
|
-
|
|
187
172
|
const updates: JsonPatch[] = [
|
|
188
173
|
{ op: "add", path: "/tags", value: "likes_baseball" },
|
|
189
174
|
{ op: "add", path: "/userId", value: "bob@contoso.com" },
|
|
190
175
|
];
|
|
191
|
-
|
|
192
176
|
const installation = await client.updateInstallation(installationId, updates);
|
|
193
177
|
```
|
|
194
178
|
|
|
195
179
|
Using the modular approach, the code would be as follows:
|
|
196
180
|
|
|
197
|
-
```
|
|
181
|
+
```ts snippet:updateInstallation_modular
|
|
198
182
|
import { createClientContext, updateInstallation } from "@azure/notification-hubs/api";
|
|
199
|
-
import { JsonPatch } from "@azure/notification-hubs
|
|
183
|
+
import { JsonPatch } from "@azure/notification-hubs";
|
|
200
184
|
|
|
201
185
|
const context = createClientContext("<connection string>", "<hub name>");
|
|
202
|
-
|
|
203
186
|
const installationId = "<unique installation ID>";
|
|
204
|
-
|
|
205
187
|
const updates: JsonPatch[] = [
|
|
206
188
|
{ op: "add", path: "/tags", value: "likes_baseball" },
|
|
207
189
|
{ op: "add", path: "/userId", value: "bob@contoso.com" },
|
|
208
190
|
];
|
|
209
|
-
|
|
210
191
|
const installation = await updateInstallation(context, installationId, updates);
|
|
211
192
|
```
|
|
212
193
|
|
|
213
194
|
To retrieve an existing installation, use the `getInstallation` method with your existing unique installation ID.
|
|
214
195
|
|
|
215
|
-
```
|
|
196
|
+
```ts snippet:getInstallation_classical
|
|
216
197
|
import { NotificationHubsClient } from "@azure/notification-hubs";
|
|
217
198
|
|
|
218
199
|
const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
219
|
-
|
|
220
200
|
const installationId = "<unique installation ID>";
|
|
221
|
-
|
|
222
201
|
const installation = client.getInstallation(installationId);
|
|
223
202
|
```
|
|
224
203
|
|
|
225
204
|
Using the modular approach, the code would be as follows:
|
|
226
205
|
|
|
227
|
-
```
|
|
206
|
+
```ts snippet:getInstallation_modular
|
|
228
207
|
import { createClientContext, getInstallation } from "@azure/notification-hubs/api";
|
|
229
208
|
|
|
230
209
|
const context = createClientContext("<connection string>", "<hub name>");
|
|
231
|
-
|
|
232
210
|
const installationId = "<unique installation ID>";
|
|
233
|
-
|
|
234
211
|
const installation = getInstallation(context, installationId);
|
|
235
212
|
```
|
|
236
213
|
|
|
@@ -240,87 +217,77 @@ A registration is associated with a PNS just as the installation above, with the
|
|
|
240
217
|
|
|
241
218
|
An installation may be created in one of two ways, first by getting a registration ID from the server using `getInstallationId` and then `createOrUpdateRegistration` or via the `createRegistration` method.
|
|
242
219
|
|
|
243
|
-
```
|
|
220
|
+
```ts snippet:createRegistration_classical
|
|
244
221
|
import {
|
|
245
222
|
NotificationHubsClient,
|
|
246
223
|
createAppleRegistrationDescription,
|
|
247
224
|
} from "@azure/notification-hubs";
|
|
248
225
|
|
|
249
226
|
const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
250
|
-
|
|
251
|
-
let registration = createAppleRegistrationDescription({
|
|
227
|
+
const registration = createAppleRegistrationDescription({
|
|
252
228
|
deviceToken: "00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0",
|
|
253
229
|
tags: ["likes_hockey", "likes_football"],
|
|
254
230
|
});
|
|
255
|
-
|
|
256
|
-
registration = await client.createRegistration(registration);
|
|
257
|
-
|
|
258
|
-
console.log(`New Registration ID: ${registration.registrationId}`);
|
|
231
|
+
const updatedRegistration = await client.createRegistration(registration);
|
|
259
232
|
```
|
|
260
233
|
|
|
261
234
|
Using the modular approach, the code would be as follows:
|
|
262
235
|
|
|
263
|
-
```
|
|
236
|
+
```ts snippet:createRegistration_modular
|
|
264
237
|
import { createClientContext, createRegistration } from "@azure/notification-hubs/api";
|
|
265
|
-
import { createAppleRegistrationDescription } from "@azure/notification-hubs
|
|
238
|
+
import { createAppleRegistrationDescription } from "@azure/notification-hubs";
|
|
266
239
|
|
|
267
240
|
const context = createClientContext("<connection string>", "<hub name>");
|
|
268
|
-
|
|
269
|
-
let registration = createAppleRegistrationDescription({
|
|
241
|
+
const registration = createAppleRegistrationDescription({
|
|
270
242
|
deviceToken: "00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0",
|
|
271
243
|
tags: ["likes_hockey", "likes_football"],
|
|
272
244
|
});
|
|
273
|
-
|
|
274
|
-
registration = await createRegistration(context, registration);
|
|
275
|
-
|
|
276
|
-
console.log(`New Registration ID: ${registration.registrationId}`);
|
|
245
|
+
const updatedRegistration = await createRegistration(context, registration);
|
|
277
246
|
```
|
|
278
247
|
|
|
279
248
|
Updates can be done via the `updateRegistration` method but unlike installations, does not support incremental updates. Querying for an existing registration can be done with the `getRegistration` method.
|
|
280
249
|
|
|
281
|
-
```
|
|
250
|
+
```ts snippet:updateRegistration_classical
|
|
282
251
|
import { NotificationHubsClient } from "@azure/notification-hubs";
|
|
283
252
|
|
|
284
253
|
const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
285
|
-
|
|
286
254
|
const registrationId = "<unique Registration ID>";
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
255
|
+
const registration = await client.getRegistration(registrationId);
|
|
256
|
+
if (registration.tags) {
|
|
257
|
+
registration.tags.push("likes_sports");
|
|
258
|
+
} else {
|
|
259
|
+
registration.tags = ["likes_sports"];
|
|
260
|
+
}
|
|
261
|
+
const updatedRegistration = await client.updateRegistration(registration);
|
|
293
262
|
```
|
|
294
263
|
|
|
295
264
|
Using the modular approach, the code would be as follows:
|
|
296
265
|
|
|
297
|
-
```
|
|
266
|
+
```ts snippet:updateRegistration_modular
|
|
298
267
|
import {
|
|
299
268
|
createClientContext,
|
|
300
269
|
getRegistration,
|
|
301
|
-
updateRegistration
|
|
270
|
+
updateRegistration,
|
|
302
271
|
} from "@azure/notification-hubs/api";
|
|
303
272
|
|
|
304
273
|
const context = createClientContext("<connection string>", "<hub name>");
|
|
305
|
-
|
|
306
274
|
const registrationId = "<unique Registration ID>";
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
275
|
+
const registration = await getRegistration(context, registrationId);
|
|
276
|
+
if (registration.tags) {
|
|
277
|
+
registration.tags.push("likes_sports");
|
|
278
|
+
} else {
|
|
279
|
+
registration.tags = ["likes_sports"];
|
|
280
|
+
}
|
|
281
|
+
const updatedRegistration = await updateRegistration(context, registration);
|
|
313
282
|
```
|
|
314
283
|
|
|
315
284
|
Registrations, unlike installations, can be queried to get all registrations, matching registrations to a condition, or by tags. Registrations can be queried using the `listRegistrations`, `listRegistrationsByChannel` and `listRegistrationsByTag` method. All methods support limiting via the `top` option and support asynchronous paging.
|
|
316
285
|
|
|
317
|
-
```
|
|
318
|
-
import { NotificationHubsClient } from "@azure/notification-hubs
|
|
286
|
+
```ts snippet:listRegistrationsByTag_classical
|
|
287
|
+
import { NotificationHubsClient } from "@azure/notification-hubs";
|
|
319
288
|
|
|
320
289
|
const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
321
|
-
|
|
322
|
-
const registrations = await client.listRegistrationsByTag("likes_hockey");
|
|
323
|
-
|
|
290
|
+
const registrations = client.listRegistrationsByTag("likes_hockey");
|
|
324
291
|
let page = 0;
|
|
325
292
|
for await (const pages of registrations.byPage()) {
|
|
326
293
|
console.log(`Page number ${page++}`);
|
|
@@ -332,13 +299,11 @@ for await (const pages of registrations.byPage()) {
|
|
|
332
299
|
|
|
333
300
|
Using the modular approach, the code would be as follows:
|
|
334
301
|
|
|
335
|
-
```
|
|
302
|
+
```ts snippet:listRegistrationsByTag_modular
|
|
336
303
|
import { createClientContext, listRegistrationsByTag } from "@azure/notification-hubs/api";
|
|
337
304
|
|
|
338
305
|
const context = createClientContext("<connection string>", "<hub name>");
|
|
339
|
-
|
|
340
306
|
const registrations = await listRegistrationsByTag(context, "likes_hockey");
|
|
341
|
-
|
|
342
307
|
let page = 0;
|
|
343
308
|
for await (const pages of registrations.byPage()) {
|
|
344
309
|
console.log(`Page number ${page++}`);
|
|
@@ -356,12 +321,8 @@ For debugging purposes, the `enableTestSend` options can be set to `true` which
|
|
|
356
321
|
|
|
357
322
|
Raw JSON or XML strings can be sent to the send or scheduled send methods, or the notification builders can be used which helps construct messages per PNS such as APNs, Firebase, Baidu, ADM and WNS. These builders will build the native message format so there is no guessing about which fields are available for each PNS.
|
|
358
323
|
|
|
359
|
-
```
|
|
360
|
-
|
|
361
|
-
import { createAppleNotificationBody } from "@azure/notification-hubs";
|
|
362
|
-
|
|
363
|
-
// Using the modular approach
|
|
364
|
-
import { createAppleNotification, createAppleNotificationBody } from "@azure/notification-hubs/models";
|
|
324
|
+
```ts snippet:createAppleNotificationBody
|
|
325
|
+
import { createAppleNotificationBody, createAppleNotification } from "@azure/notification-hubs";
|
|
365
326
|
|
|
366
327
|
const apnsBody = createAppleNotificationBody({
|
|
367
328
|
alert: {
|
|
@@ -372,29 +333,21 @@ const apnsBody = createAppleNotificationBody({
|
|
|
372
333
|
sound: "default",
|
|
373
334
|
interruptionLevel: "time-sensitive",
|
|
374
335
|
});
|
|
375
|
-
|
|
376
336
|
// Send the message using the modular approach
|
|
377
337
|
const notification = createAppleNotification({
|
|
378
|
-
body: apnsBody
|
|
379
|
-
})
|
|
380
|
-
|
|
381
|
-
const result = await sendNotification(context, notification);
|
|
338
|
+
body: apnsBody,
|
|
339
|
+
});
|
|
382
340
|
```
|
|
383
341
|
|
|
384
342
|
#### Broadcast Send
|
|
385
343
|
|
|
386
344
|
Notification Hubs can be used to send notifications to all registered devices per platform using broadcast send through the `sendBroadcastNotification` method.
|
|
387
345
|
|
|
388
|
-
```
|
|
389
|
-
import {
|
|
390
|
-
NotificationHubsClient,
|
|
391
|
-
createAppleNotification,
|
|
392
|
-
} from "@azure/notification-hubs/api";
|
|
393
|
-
|
|
394
|
-
const context = createClientContext(connectionString, hubName);
|
|
346
|
+
```ts snippet:sendBroadcastNotification_classical
|
|
347
|
+
import { NotificationHubsClient, createAppleNotification } from "@azure/notification-hubs";
|
|
395
348
|
|
|
349
|
+
const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
396
350
|
const messageBody = `{ "aps" : { "alert" : "Hello" } }`;
|
|
397
|
-
|
|
398
351
|
const message = createAppleNotification({
|
|
399
352
|
body: messageBody,
|
|
400
353
|
headers: {
|
|
@@ -402,12 +355,9 @@ const message = createAppleNotification({
|
|
|
402
355
|
"apns-push-type": "alert",
|
|
403
356
|
},
|
|
404
357
|
});
|
|
405
|
-
|
|
406
358
|
const result = await client.sendBroadcastNotification(message);
|
|
407
|
-
|
|
408
359
|
console.log(`Tracking ID: ${result.trackingId}`);
|
|
409
360
|
console.log(`Correlation ID: ${result.correlationId}`);
|
|
410
|
-
|
|
411
361
|
// Only available in Standard SKU and above
|
|
412
362
|
if (result.notificationId) {
|
|
413
363
|
console.log(`Notification ID: ${result.notificationId}`);
|
|
@@ -416,14 +366,12 @@ if (result.notificationId) {
|
|
|
416
366
|
|
|
417
367
|
Using the modular approach, the code would be as follows:
|
|
418
368
|
|
|
419
|
-
```
|
|
369
|
+
```ts snippet:sendBroadcastNotification_modular
|
|
420
370
|
import { createClientContext, sendBroadcastNotification } from "@azure/notification-hubs/api";
|
|
421
|
-
import { createAppleNotification } from "@azure/notification-hubs
|
|
422
|
-
|
|
423
|
-
const context = createClientContext(connectionString, hubName);
|
|
371
|
+
import { createAppleNotification } from "@azure/notification-hubs";
|
|
424
372
|
|
|
373
|
+
const context = createClientContext("<connection string>", "<hub name>");
|
|
425
374
|
const messageBody = `{ "aps" : { "alert" : "Hello" } }`;
|
|
426
|
-
|
|
427
375
|
const message = createAppleNotification({
|
|
428
376
|
body: messageBody,
|
|
429
377
|
headers: {
|
|
@@ -431,12 +379,9 @@ const message = createAppleNotification({
|
|
|
431
379
|
"apns-push-type": "alert",
|
|
432
380
|
},
|
|
433
381
|
});
|
|
434
|
-
|
|
435
382
|
const result = await sendBroadcastNotification(context, message);
|
|
436
|
-
|
|
437
383
|
console.log(`Tracking ID: ${result.trackingId}`);
|
|
438
384
|
console.log(`Correlation ID: ${result.correlationId}`);
|
|
439
|
-
|
|
440
385
|
// Only available in Standard SKU and above
|
|
441
386
|
if (result.notificationId) {
|
|
442
387
|
console.log(`Notification ID: ${result.notificationId}`);
|
|
@@ -447,17 +392,12 @@ if (result.notificationId) {
|
|
|
447
392
|
|
|
448
393
|
To send directly a device, the user can send using the platform provided unique identifier such as APNs device token by calling the `sendNotification` method with a `deviceHandle` parameter.
|
|
449
394
|
|
|
450
|
-
```
|
|
451
|
-
import {
|
|
452
|
-
NotificationHubsClient,
|
|
453
|
-
createAppleNotification,
|
|
454
|
-
} from "@azure/notification-hubs";
|
|
455
|
-
|
|
456
|
-
const client = new NotificationHubsClient(connectionString, hubName);
|
|
395
|
+
```ts snippet:directSend_classical
|
|
396
|
+
import { NotificationHubsClient, createAppleNotification } from "@azure/notification-hubs";
|
|
457
397
|
|
|
398
|
+
const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
458
399
|
const deviceHandle = "00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0";
|
|
459
400
|
const messageBody = `{ "aps" : { "alert" : "Hello" } }`;
|
|
460
|
-
|
|
461
401
|
const message = createAppleNotification({
|
|
462
402
|
body: messageBody,
|
|
463
403
|
headers: {
|
|
@@ -465,12 +405,9 @@ const message = createAppleNotification({
|
|
|
465
405
|
"apns-push-type": "alert",
|
|
466
406
|
},
|
|
467
407
|
});
|
|
468
|
-
|
|
469
408
|
const result = await client.sendNotification(message, { deviceHandle });
|
|
470
|
-
|
|
471
409
|
console.log(`Tracking ID: ${result.trackingId}`);
|
|
472
410
|
console.log(`Correlation ID: ${result.correlationId}`);
|
|
473
|
-
|
|
474
411
|
// Only available in Standard SKU and above
|
|
475
412
|
if (result.notificationId) {
|
|
476
413
|
console.log(`Notification ID: ${result.notificationId}`);
|
|
@@ -479,15 +416,13 @@ if (result.notificationId) {
|
|
|
479
416
|
|
|
480
417
|
Using the modular approach, the code would be as follows:
|
|
481
418
|
|
|
482
|
-
```
|
|
483
|
-
import { createClientContext,
|
|
484
|
-
import { createAppleNotification } from "@azure/notification-hubs
|
|
485
|
-
|
|
486
|
-
const context = createClientContext(connectionString, hubName);
|
|
419
|
+
```ts snippet:directSend_modular
|
|
420
|
+
import { createClientContext, sendNotification } from "@azure/notification-hubs/api";
|
|
421
|
+
import { createAppleNotification } from "@azure/notification-hubs";
|
|
487
422
|
|
|
423
|
+
const context = createClientContext("<connection string>", "<hub name>");
|
|
488
424
|
const deviceHandle = "00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0";
|
|
489
425
|
const messageBody = `{ "aps" : { "alert" : "Hello" } }`;
|
|
490
|
-
|
|
491
426
|
const message = createAppleNotification({
|
|
492
427
|
body: messageBody,
|
|
493
428
|
headers: {
|
|
@@ -495,12 +430,9 @@ const message = createAppleNotification({
|
|
|
495
430
|
"apns-push-type": "alert",
|
|
496
431
|
},
|
|
497
432
|
});
|
|
498
|
-
|
|
499
433
|
const result = await sendNotification(context, message, { deviceHandle });
|
|
500
|
-
|
|
501
434
|
console.log(`Tracking ID: ${result.trackingId}`);
|
|
502
435
|
console.log(`Correlation ID: ${result.correlationId}`);
|
|
503
|
-
|
|
504
436
|
// Only available in Standard SKU and above
|
|
505
437
|
if (result.notificationId) {
|
|
506
438
|
console.log(`Notification ID: ${result.notificationId}`);
|
|
@@ -513,33 +445,22 @@ In addition to targeting a single device, a user can target multiple devices usi
|
|
|
513
445
|
|
|
514
446
|
If you wish to create a tag expression from an array of tags, there is a Tag Expression Builder available with the `createTagExpression` method which is exposed at the top level import or `@azure/notification-hubs/models/tagExpressionBuilder` modular import which creates an "or tag expression" from the tags.
|
|
515
447
|
|
|
516
|
-
```
|
|
517
|
-
// Top level import
|
|
448
|
+
```ts snippet:createTagExpression
|
|
518
449
|
import { createTagExpression } from "@azure/notification-hubs";
|
|
519
450
|
|
|
520
|
-
// Modular import
|
|
521
|
-
import { createTagExpression } from "@azure/notification-hubs/models";
|
|
522
|
-
|
|
523
451
|
const tags = ["likes_football", "likes_hockey"];
|
|
524
452
|
const tagExpression = createTagExpression(tags);
|
|
525
|
-
|
|
526
453
|
console.log(tagExpression);
|
|
527
|
-
// likes_football||likes_hockey
|
|
528
454
|
```
|
|
529
455
|
|
|
530
456
|
Tag expression messages can be sent using the following code:
|
|
531
457
|
|
|
532
|
-
```
|
|
533
|
-
import {
|
|
534
|
-
NotificationHubsClient,
|
|
535
|
-
createAppleNotification,
|
|
536
|
-
} from "@azure/notification-hubs";
|
|
458
|
+
```ts snippet:sendNotification_classical
|
|
459
|
+
import { NotificationHubsClient, createAppleNotification } from "@azure/notification-hubs";
|
|
537
460
|
|
|
538
461
|
const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
539
|
-
|
|
540
462
|
const tagExpression = "likes_hockey && likes_football";
|
|
541
463
|
const messageBody = `{ "aps" : { "alert" : "Hello" } }`;
|
|
542
|
-
|
|
543
464
|
const notification = createAppleNotification({
|
|
544
465
|
body: messageBody,
|
|
545
466
|
headers: {
|
|
@@ -547,12 +468,9 @@ const notification = createAppleNotification({
|
|
|
547
468
|
"apns-push-type": "alert",
|
|
548
469
|
},
|
|
549
470
|
});
|
|
550
|
-
|
|
551
471
|
const result = await client.sendNotification(notification, { tagExpression });
|
|
552
|
-
|
|
553
472
|
console.log(`Tracking ID: ${result.trackingId}`);
|
|
554
473
|
console.log(`Correlation ID: ${result.correlationId}`);
|
|
555
|
-
|
|
556
474
|
// Only available in Standard SKU and above
|
|
557
475
|
if (result.notificationId) {
|
|
558
476
|
console.log(`Notification ID: ${result.notificationId}`);
|
|
@@ -561,15 +479,13 @@ if (result.notificationId) {
|
|
|
561
479
|
|
|
562
480
|
Using the modular approach, the code would be as follows:
|
|
563
481
|
|
|
564
|
-
```
|
|
482
|
+
```ts snippet:sendNotification_modular
|
|
565
483
|
import { createClientContext, sendNotification } from "@azure/notification-hubs/api";
|
|
566
|
-
import { createAppleNotification } from "@azure/notification-hubs
|
|
484
|
+
import { createAppleNotification } from "@azure/notification-hubs";
|
|
567
485
|
|
|
568
486
|
const context = createClientContext("<connection string>", "<hub name>");
|
|
569
|
-
|
|
570
487
|
const tagExpression = "likes_hockey && likes_football";
|
|
571
488
|
const messageBody = `{ "aps" : { "alert" : "Hello" } }`;
|
|
572
|
-
|
|
573
489
|
const notification = createAppleNotification({
|
|
574
490
|
body: messageBody,
|
|
575
491
|
headers: {
|
|
@@ -577,12 +493,9 @@ const notification = createAppleNotification({
|
|
|
577
493
|
"apns-push-type": "alert",
|
|
578
494
|
},
|
|
579
495
|
});
|
|
580
|
-
|
|
581
496
|
const result = await sendNotification(context, notification, { tagExpression });
|
|
582
|
-
|
|
583
497
|
console.log(`Tracking ID: ${result.trackingId}`);
|
|
584
498
|
console.log(`Correlation ID: ${result.correlationId}`);
|
|
585
|
-
|
|
586
499
|
// Only available in Standard SKU and above
|
|
587
500
|
if (result.notificationId) {
|
|
588
501
|
console.log(`Notification ID: ${result.notificationId}`);
|
|
@@ -593,20 +506,14 @@ if (result.notificationId) {
|
|
|
593
506
|
|
|
594
507
|
Push notifications can be scheduled up to seven days in advance with Standard SKU namespaces and above using the `scheduleNotification` method to send to devices with tags or a general broadcast with `scheduleBroadcastNotification`. This returns a notification ID which can be then used to cancel if necessary via the `cancelScheduledNotification` method.
|
|
595
508
|
|
|
596
|
-
```
|
|
597
|
-
import {
|
|
598
|
-
NotificationHubsClient,
|
|
599
|
-
createAppleNotification,
|
|
600
|
-
} from "@azure/notification-hubs";
|
|
509
|
+
```ts snippet:scheduledSendNotification_classical
|
|
510
|
+
import { NotificationHubsClient, createAppleNotification } from "@azure/notification-hubs";
|
|
601
511
|
|
|
602
512
|
const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
603
|
-
|
|
604
513
|
const tagExpression = "likes_hockey && likes_football";
|
|
605
514
|
const messageBody = `{ "aps" : { "alert" : "Hello" } }`;
|
|
606
|
-
|
|
607
515
|
// Schedule 8 hours from now
|
|
608
|
-
const scheduledTime = new Date(Date.now() +
|
|
609
|
-
|
|
516
|
+
const scheduledTime = new Date(Date.now() + 8 * 60 * 60 * 1000);
|
|
610
517
|
const message = createAppleNotification({
|
|
611
518
|
body: messageBody,
|
|
612
519
|
headers: {
|
|
@@ -614,30 +521,24 @@ const message = createAppleNotification({
|
|
|
614
521
|
"apns-push-type": "alert",
|
|
615
522
|
},
|
|
616
523
|
});
|
|
617
|
-
|
|
618
524
|
const result = await client.scheduleNotification(scheduledTime, message, { tagExpression });
|
|
619
|
-
|
|
620
525
|
console.log(`Tracking ID: ${result.trackingId}`);
|
|
621
526
|
console.log(`Correlation ID: ${result.correlationId}`);
|
|
622
|
-
|
|
623
527
|
// Can be used to cancel via the cancelScheduledSend method
|
|
624
528
|
console.log(`Notification ID: ${result.notificationId}`);
|
|
625
529
|
```
|
|
626
530
|
|
|
627
531
|
Using the modular approach, the code would be as follows:
|
|
628
532
|
|
|
629
|
-
```
|
|
533
|
+
```ts snippet:scheduledSendNotification_modular
|
|
630
534
|
import { createClientContext, scheduleNotification } from "@azure/notification-hubs/api";
|
|
631
|
-
import { createAppleNotification } from "@azure/notification-hubs
|
|
535
|
+
import { createAppleNotification } from "@azure/notification-hubs";
|
|
632
536
|
|
|
633
537
|
const context = createClientContext("<connection string>", "<hub name>");
|
|
634
|
-
|
|
635
538
|
const tagExpression = "likes_hockey && likes_football";
|
|
636
539
|
const messageBody = `{ "aps" : { "alert" : "Hello" } }`;
|
|
637
|
-
|
|
638
540
|
// Schedule 8 hours from now
|
|
639
|
-
const scheduledTime = new Date(Date.now() +
|
|
640
|
-
|
|
541
|
+
const scheduledTime = new Date(Date.now() + 8 * 60 * 60 * 1000);
|
|
641
542
|
const message = createAppleNotification({
|
|
642
543
|
body: messageBody,
|
|
643
544
|
headers: {
|
|
@@ -645,12 +546,9 @@ const message = createAppleNotification({
|
|
|
645
546
|
"apns-push-type": "alert",
|
|
646
547
|
},
|
|
647
548
|
});
|
|
648
|
-
|
|
649
549
|
const result = await scheduleNotification(context, scheduledTime, message, { tagExpression });
|
|
650
|
-
|
|
651
550
|
console.log(`Tracking ID: ${result.trackingId}`);
|
|
652
551
|
console.log(`Correlation ID: ${result.correlationId}`);
|
|
653
|
-
|
|
654
552
|
// Can be used to cancel via the cancelScheduledSend method
|
|
655
553
|
console.log(`Notification ID: ${result.notificationId}`);
|
|
656
554
|
```
|
|
@@ -661,10 +559,6 @@ console.log(`Notification ID: ${result.notificationId}`);
|
|
|
661
559
|
|
|
662
560
|
React Native currently does not have support for [`URLSearchParams`] which is used by the Azure Notification Hubs SDK. In order to use the SDK in React Native, you will need to install the [`url-search-params-polyfill`](https://www.npmjs.com/package/url-search-params-polyfill) package and import it before using the SDK.
|
|
663
561
|
|
|
664
|
-
```typescript
|
|
665
|
-
import 'url-search-params-polyfill';
|
|
666
|
-
```
|
|
667
|
-
|
|
668
562
|
We also need to provide polyfill for `TextEncoder` API and async iterator API. Please see our [React Native sample with Expo](https://github.com/Azure/azure-sdk-for-js/blob/main/samples/frameworks/react-native/appconfigBasic/README.md#add-polyfills) for more details.
|
|
669
563
|
|
|
670
564
|
### Diagnose Dropped Notifications
|
|
@@ -673,20 +567,51 @@ Azure Notification Hubs has a complete guide to troubleshooting problems with dr
|
|
|
673
567
|
|
|
674
568
|
[Test send](https://docs.microsoft.com/azure/notification-hubs/notification-hubs-push-notification-fixer#enabletestsend-property) is supported supported in the `sendNotification` and `sendBroadcastNotification` methods with the `enableTestSend` option:
|
|
675
569
|
|
|
676
|
-
```
|
|
677
|
-
|
|
678
|
-
|
|
570
|
+
```ts snippet:testSend_classical
|
|
571
|
+
import { NotificationHubsClient, createAppleNotification } from "@azure/notification-hubs";
|
|
572
|
+
|
|
573
|
+
const client = new NotificationHubsClient("<connection string>", "<hub name>");
|
|
574
|
+
const tagExpression = "likes_hockey && likes_football";
|
|
575
|
+
const messageBody = `{ "aps" : { "alert" : "Hello" } }`;
|
|
576
|
+
const notification = createAppleNotification({
|
|
577
|
+
body: messageBody,
|
|
578
|
+
headers: {
|
|
579
|
+
"apns-priority": "10",
|
|
580
|
+
"apns-push-type": "alert",
|
|
581
|
+
},
|
|
582
|
+
});
|
|
583
|
+
const result = await client.sendNotification(notification, {
|
|
584
|
+
tagExpression,
|
|
585
|
+
enableTestSend: true,
|
|
586
|
+
});
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
```ts snippet:testSend_modular
|
|
590
|
+
import { createClientContext, sendNotification } from "@azure/notification-hubs/api";
|
|
591
|
+
import { createAppleNotification } from "@azure/notification-hubs";
|
|
679
592
|
|
|
680
|
-
|
|
681
|
-
const
|
|
593
|
+
const context = createClientContext("<connection string>", "<hub name>");
|
|
594
|
+
const tagExpression = "likes_hockey && likes_football";
|
|
595
|
+
const messageBody = `{ "aps" : { "alert" : "Hello" } }`;
|
|
596
|
+
const notification = createAppleNotification({
|
|
597
|
+
body: messageBody,
|
|
598
|
+
headers: {
|
|
599
|
+
"apns-priority": "10",
|
|
600
|
+
"apns-push-type": "alert",
|
|
601
|
+
},
|
|
602
|
+
});
|
|
603
|
+
const result = await sendNotification(context, notification, {
|
|
604
|
+
tagExpression,
|
|
605
|
+
enableTestSend: true,
|
|
606
|
+
});
|
|
682
607
|
```
|
|
683
608
|
|
|
684
609
|
### Logging
|
|
685
610
|
|
|
686
611
|
Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:
|
|
687
612
|
|
|
688
|
-
```
|
|
689
|
-
|
|
613
|
+
```ts snippet:logging
|
|
614
|
+
import { setLogLevel } from "@azure/logger";
|
|
690
615
|
|
|
691
616
|
setLogLevel("info");
|
|
692
617
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azure/notification-hubs",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.20240920.2",
|
|
4
4
|
"description": "Azure Notification Hubs SDK for JavaScript",
|
|
5
5
|
"sdk-type": "client",
|
|
6
6
|
"main": "./dist/commonjs/index.js",
|
|
@@ -37,7 +37,8 @@
|
|
|
37
37
|
"test": "npm run clean && tshy && npm run unit-test:node && npm run build:test && npm run unit-test:browser && npm run integration-test",
|
|
38
38
|
"unit-test:browser": "npm run build:test && dev-tool run test:vitest --no-test-proxy --browser -- -c vitest.browser.unit.config.ts",
|
|
39
39
|
"unit-test:node": "dev-tool run test:vitest --no-test-proxy -- -c vitest.unit.config.ts",
|
|
40
|
-
"unit-test": "npm run unit-test:node && npm run unit-test:browser"
|
|
40
|
+
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
|
|
41
|
+
"update-snippets": "tshy && dev-tool run update-snippets"
|
|
41
42
|
},
|
|
42
43
|
"files": [
|
|
43
44
|
"dist/",
|
|
@@ -75,7 +76,7 @@
|
|
|
75
76
|
"rimraf": "^5.0.5",
|
|
76
77
|
"tshy": "^2.0.0",
|
|
77
78
|
"tsx": "^4.19.1",
|
|
78
|
-
"typescript": "~5.
|
|
79
|
+
"typescript": "~5.5.3",
|
|
79
80
|
"vitest": "^2.0.5"
|
|
80
81
|
},
|
|
81
82
|
"dependencies": {
|