You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This readme is a brief introduction, please refer to the full [documentation](doc/apn.markdown) in `doc/` for more details.
41
+
This readme is a brief introduction; please refer to the full [documentation](doc/apn.markdown) in `doc/` for more details.
40
42
41
43
If you have previously used v1.x and wish to learn more about what's changed in v2.0, please see [What's New](doc/whats-new.markdown)
42
44
@@ -59,15 +61,20 @@ var options = {
59
61
production:false
60
62
};
61
63
62
-
var apnProvider =newapn.Provider(options);
64
+
constapnProvider=newapn.Provider(options);
63
65
```
64
66
65
67
By default, the provider will connect to the sandbox unless the environment variable `NODE_ENV=production` is set.
66
68
67
-
For more information about configuration options consult the [provider documentation](doc/provider.markdown).
69
+
For more information about configuration options, consult the [provider documentation](doc/provider.markdown).
68
70
69
71
Help with preparing the key and certificate files for connection can be found in the [wiki][certificateWiki]
70
72
73
+
> [!WARNING]
74
+
> You should only create one `Provider` per-process for each certificate/key pair you have. You do not need to create a new `Provider` for each notification. If you are only sending notifications to one app, there is no need for more than one `Provider`.
75
+
>
76
+
> If you are constantly creating `Provider` instances in your app, make sure to call `Provider.shutdown()` when you are done with each provider to release its resources and memory.
77
+
71
78
### Connecting through an HTTP proxy
72
79
73
80
If you need to connect through an HTTP proxy, you simply need to provide the `proxy: {host, port}` option when creating the provider. For example:
@@ -86,7 +93,7 @@ var options = {
86
93
production:false
87
94
};
88
95
89
-
var apnProvider =newapn.Provider(options);
96
+
constapnProvider=newapn.Provider(options);
90
97
```
91
98
92
99
The provider will first send an HTTP CONNECT request to the specified proxy in order to establish an HTTP tunnel. Once established, it will create a new secure connection to the Apple Push Notification provider API through the tunnel.
@@ -111,11 +118,11 @@ var options = {
111
118
production:false
112
119
};
113
120
114
-
var apnProvider =newapn.MultiProvider(options);
121
+
constapnProvider=newapn.MultiProvider(options);
115
122
```
116
123
117
124
## Sending a notification
118
-
To send a notification you will first need a device token from your app as a string
125
+
To send a notification, you will first need a device token from your app as a string.
119
126
120
127
```javascript
121
128
let deviceToken ="a9d0ed10e9cfd022a61cb08753f49c5a0b0dfb383697bf9f9d750a1003da19c7"
@@ -124,7 +131,7 @@ let deviceToken = "a9d0ed10e9cfd022a61cb08753f49c5a0b0dfb383697bf9f9d750a1003da1
124
131
Create a notification object, configuring it with the relevant parameters (See the [notification documentation](doc/notification.markdown) for more details.)
125
132
126
133
```javascript
127
-
var note =newapn.Notification();
134
+
let note =newapn.Notification();
128
135
129
136
note.expiry=Math.floor(Date.now() /1000) +3600; // Expires 1 hour from now.
This will result in the the following notification payload being sent to the device
155
+
This will result in the following notification payload being sent to the device.
146
156
147
157
```json
148
158
{"messageFrom":"John Appelseed","aps":{"badge":3,"sound":"ping.aiff","alert":"\uD83D\uDCE7\u2709 You have a new message"}}
149
159
```
150
160
151
-
Create a Live Activity notification object, configuring it with the relevant parameters (See the [notification documentation](doc/notification.markdown) for more details.)
161
+
Create a Live Activity notification object and configure it with the relevant parameters (See the [notification documentation](doc/notification.markdown) for more details.)
// see the documentation for an explanation of the result
186
+
} catch (error) {
187
+
// Handle error...
188
+
}
176
189
```
177
190
178
-
This will result in the the following notification payload being sent to the device
191
+
This will result in the following notification payload being sent to the device.
179
192
180
193
181
194
```json
182
195
{"messageFrom":"John Appleseed","aps":{"badge":3,"sound":"ping.aiff","alert":"\uD83D\uDCE7\u2709 You have a new message", "relevance-score":75,"timestamp":1683129662,"stale-date":1683216062,"event":"update","content-state":{}}}
183
196
```
184
197
185
-
You should only create one `Provider` per-process for each certificate/key pair you have. You do not need to create a new `Provider` for each notification. If you are only sending notifications to one app then there is no need for more than one `Provider`.
198
+
## Manage Channels
199
+
Starting in iOS 18 and iPadOS 18 Live Activities can be used to broadcast push notifications over channels. To do so, you will need your apps' `bundleId`.
200
+
201
+
```javascript
202
+
let bundleId ="com.node.apn";
203
+
```
204
+
205
+
Create a notification object, configuring it with the relevant parameters (See the [notification documentation](doc/notification.markdown) for more details.)
apns-channel-id:'dHN0LXNyY2gtY2hubA=='// The new channel
230
+
}
231
+
```
232
+
233
+
Similarly, `manageChannels` has additional `action`s that allow you to `read`, `readAll`, and `delete` channels. The `read` and `delete` actions require similar information to the `create` example above, with the exception that they require `note.channelId` to be populated. To request all active channel id's, you can use the `readAll` action:
// see the documentation for an explanation of the result
239
+
} catch (error) {
240
+
// Handle error...
241
+
}
242
+
```
243
+
244
+
After the promise is fulfilled, `result` will look like the following:
245
+
246
+
```javascript
247
+
{
248
+
apns-request-id:'some id value',
249
+
channels: ['dHN0LXNyY2gtY2hubA==', 'eCN0LXNyY2gtY2hubA=='...] // A list of active channels
250
+
}
251
+
```
252
+
253
+
Further information about managing channels can be found in [Apple's documentation](https://developer.apple.com/documentation/usernotifications/sending-channel-management-requests-to-apns).
254
+
255
+
## Sending A Broadcast Notification
256
+
Starting in iOS 18 and iPadOS 18, after a channel is created using `manageChannels`, broadcast push notifications can be sent to any device subscribed to the respective `channelId` created for a `bundleId`. A broadcast notification looks similar to a standard Live Activity notification mentioned above but requires `note.channelId` to be populated. An example is below:
If you are constantly creating `Provider` instances in your app, make sure to call `Provider.shutdown()` when you are done with each provider to release its resources and memory.
286
+
Further information about broadcast notifications can be found in [Apple's documentation](https://developer.apple.com/documentation/usernotifications/sending-broadcast-push-notification-requests-to-apns).
Copy file name to clipboardexpand all lines: doc/provider.markdown
+51-12
Original file line number
Diff line number
Diff line change
@@ -13,18 +13,34 @@ Options:
13
13
14
14
-`key` {Buffer|String} The filename of the connection key to load from disk, or a Buffer/String containing the key data. (Defaults to: `key.pem`)
15
15
16
-
-`ca` An array of trusted certificates. Each element should contain either a filename to load, or a Buffer/String (in PEM format) to be used directly. If this is omitted several well known "root" CAs will be used. - You may need to use this as some environments don't include the CA used by Apple (entrust_2048).
16
+
-`ca` An array of trusted certificates. Each element should contain either a filename to load, or a Buffer/String (in PEM format) to be used directly. If this is omitted several well known "root" CAs will be used. - You may need to use this as some environments don't include the CA used by Apple (entrust_2048)
17
17
18
-
-`pfx` {Buffer|String} File path for private key, certificate and CA certs in PFX or PKCS12 format, or a Buffer containing the PFX data. If supplied will always be used instead of certificate and key above.
18
+
-`pfx` {Buffer|String} File path for private key, certificate and CA certs in PFX or PKCS12 format, or a Buffer containing the PFX data. If supplied will always be used instead of certificate and key above
19
19
20
20
-`passphrase` {String} The passphrase for the connection key, if required
21
21
22
22
-`production` {Boolean} Specifies which environment to connect to: Production (if true) or Sandbox - The hostname will be set automatically. (Defaults to NODE_ENV == "production", i.e. false unless the NODE_ENV environment variable is set accordingly)
23
23
24
24
-`rejectUnauthorized` {Boolean} Reject Unauthorized property to be passed through to tls.connect() (Defaults to `true`)
25
25
26
+
-`address` {String} The address of the APNs server to send notifications to. If not provided, will connect to standard APNs server
27
+
28
+
-`port` {Number} The port of the APNs server to send notifications to. (Defaults to 443)
29
+
30
+
-`manageChannelsAddress` {String} The address of the APNs channel management server to send notifications to. If not provided, will connect to standard APNs channel management server
31
+
32
+
-`manageChannelsPort` {Number} The port of the APNs channel management server to send notifications to. If not provided, will connect to standard APNs channel management port
33
+
34
+
-`proxy` {host: String, port: Number|String} Connect through an HTTP proxy when sending notifications
35
+
36
+
-`manageChannelsProxy` {host: String, port: Number|String} Connect through an HTTP proxy when managing channels
37
+
38
+
-`rejectUnauthorized` {Boolean} Reject Unauthorized property to be passed through to tls.connect() (Defaults to `true`)
39
+
26
40
-`connectionRetryLimit` {Number} The maximum number of connection failures that will be tolerated before `apn.Provider` will "give up". [See below.](#connection-retry-limit) (Defaults to: 3)
27
41
42
+
-`heartBeat` {Number} The delay interval in ms that apn will ping APNs servers. (Defaults to: 60000)
43
+
28
44
-`requestTimeout` {Number} The maximum time in ms that apn will wait for a response to a request. (Defaults to: 5000)
29
45
30
46
#### Provider Certificates vs. Authentication Tokens
@@ -47,31 +63,54 @@ The `Provider` can continue to be used for sending notifications and the counter
47
63
48
64
## Class: apn.Provider
49
65
66
+
`apn.Provider` provides a number of methods for sending notifications, broadcasting notifications, and managing channels. Calling any of the methods will return a `Promise` for each notification, which is discussed more in [Results from APN Provider Methods](#results-from-apnprovider-methods).
67
+
50
68
### connection.send(notification, recipients)
51
69
52
-
This is main interface for sending notifications. Create a `Notification` object and pass it in, along with a single recipient or an array of them and node-apn will take care of the rest, delivering a copy of the notification to each recipient.
70
+
This is the main interface for sending notifications. Create a `Notification` object and pass it in, along with a single recipient or an array of them, and node-apn will take care of the rest, delivering a copy of the notification to each recipient.
53
71
54
72
> A "recipient" is a `String` containing the hex-encoded device token.
55
73
56
-
Calling `send` will return a `Promise`. The Promise will resolve after each notification (per token) has reached a final state. Each notification can end in one of three possible states:
74
+
Calling `send` will return a `Promise`. The Promise will resolve after each notification (per token) has reached a final state.
This is the interface for managing broadcast channels. Create a single `Notification` object or an array of them and pass the notification(s) in, along with a bundleId, and an action (`create`, `read`, `readAll`, `delete`) and node-apn will take care of the rest, asking the APNs to perform the action using the criteria specified in each notification.
78
+
79
+
> A "bundleId" is a `String` containing the bundle identifier for the application.
80
+
81
+
> An "action" is a `String` containing: `create`, `read`, `readAll`, or `delete` and represents what action to perform with a channel (See more in [Apple Documentation](https://developer.apple.com/documentation/usernotifications/sending-channel-management-requests-to-apns)).
82
+
83
+
Calling `manageChannels` will return a `Promise`. The Promise will resolve after each notification has reached a final state.
84
+
85
+
### connection.broadcast(notification, bundleId)
86
+
87
+
This is the interface for broadcasting Live Activity notifications. Create a single `Notification` object or an array of them and pass the notification(s) in, along with a bundleId and node-apn will take care of the rest, asking the APNs to broadcast using the criteria specified in each notification.
88
+
89
+
> A "bundleId" is a `String` containing the bundle identifier for the application.
90
+
91
+
Calling `broadcast` will return a `Promise`. The Promise will resolve after each notification has reached a final state.
92
+
93
+
### Results from apn.Provider methods
94
+
95
+
Each notification can end in one of three possible states:
57
96
58
97
-`sent` - the notification was accepted by Apple for delivery to the given recipient
59
-
-`failed` (rejected) - the notification was rejected by Apple. A rejection has an associated `status` and `reason` which is included.
60
-
-`failed` (error) - a connection-level error occurred which prevented successful communication with Apple. In very rare cases it's possible that the notification was still delivered. However, this state usually results from a network problem.
98
+
-`failed` (rejected) - the notification was rejected by Apple. A rejection has an associated `status` and `reason` which are included.
99
+
-`failed` (error) - a connection-level error occurred, which prevented successful communication with Apple. In very rare cases, it's possible that the notification was still delivered. However, this state usually results from a network problem.
61
100
62
101
When the returned `Promise` resolves, its value will be an Object containing two properties
63
102
64
103
#### sent
65
104
66
-
An array of device tokens to which the notification was successfully sent and accepted by Apple.
105
+
An array of device tokens or bundle identifiers to which the notification was successfully sent and accepted by Apple.
67
106
68
-
Being `sent` does **not**guaranteed the notification will be _delivered_, other unpredictable factors - including whether the device is reachable - can ultimately prevent delivery.
107
+
Being `sent` does **not**guarantee the notification will be _delivered_, other unpredictable factors - including whether the device is reachable - can ultimately prevent delivery.
69
108
70
109
#### failed
71
110
72
-
An array of objects for each failed token. Each object will contain the device token which failed and details of the failure which will differ between rejected and errored notifications.
111
+
An array of objects for each failed token or bundle identifier. Each object will contain the device token or bundle identifier that failed and details of the failure, which will differ between rejected and errored notifications.
73
112
74
-
For **rejected** notifications the object will take the following form
113
+
For **rejected** notifications using `send()`, the object will take the following form
75
114
76
115
```javascript
77
116
{
@@ -85,7 +124,7 @@ For **rejected** notifications the object will take the following form
85
124
86
125
More details about the response and associated status codes can be found in the [HTTP/2 Response from APN documentation][http2-response].
87
126
88
-
If a failed notification was instead caused by an **error** then it will have an `error` property instead of `response` and `status`:
127
+
If a failed notification using `send()`was instead caused by an **error** then it will have an `error` property instead of `response` and `status`:
89
128
90
129
```javascript
91
130
{
@@ -103,7 +142,7 @@ If you wish to send notifications containing emoji or other multi-byte character
103
142
104
143
Indicate to node-apn that it should close all open connections when the queue of pending notifications is fully drained. This will allow your application to terminate.
105
144
106
-
**Note:** If notifications are pushed after the connection has started, an error will be thrown.
145
+
**Note:** If notifications are pushed after the shutdown has started, an error will be thrown.
0 commit comments