Node client for connecting to Apple's Push Notification Service using the new HTTP/2 protocol with JSON web tokens.
Create an APNS client using a signing key:
const APNS =const client =team: `TFLP87PW54`keyId: `123ABC456`signingKey: fsdefaultTopic: `com.tablelist.Tablelist`
Send a basic notification with message:
const BasicNotification =const bn = deviceToken 'Hello, World'tryawait clientcatch errconsole
Send a basic notification with message and options:
const BasicNotification =const bn = deviceToken 'Hello, World'badge: 4data:userId: usergetUserIdtryawait clientcatch errconsole
Send a silent notification using
const SilentNotification =const sn = deviceTokentryawait clientcatch errconsole
Note: Apple recommends that no options other than the
content-available flag be sent in order for a notification to truly be silent and wake up your app in the background. Therefore this class does not accept any additional options in the constructor.
Send multiple notifications concurrently:
const BasicNotification =const notifications =deviceToken1 'Hello, World'deviceToken2 'Hello, World'tryawait clientcatch errconsole
For complete control over the push notification packet use the base
const Notification =const notification = deviceTokenaps: ...tryawait clientcatcherrconsole
Available options can be found at APNS Payload Options
All errors are defined in
./lib/errors.js and come directly from APNS Table 4
You can easily listen for these errors by attaching an error handler to the APNS client:
const Errors =// Listen for a specific errorclient// Listen for any errorclient
If you need to close connections to Apple's APNS servers in order to allow the Node process to exit, you can tear down the APNS client:
Once a client is closed you will not be able to use it again. Instead you should instantiate a new client with
By default the APNS client connects to the production push notification server. This is identical to passing in the options:
const client =host: 'api.push.apple.com'port: 443...
To connect to the development push notification server, pass the options:
const client =host: 'api.development.push.apple.com'...
apns2 requires Node.js v12.14 or later