@vlasky/node-apn-http2
v1.4.0
Published
Communicate with Apple Push Notification Service via native Node.js v10+ HTTP2 module (node-apn drop-in). A fork of petercv's fork of andreialecu's package.
Readme
@vlasky/node-apn-http2
A Node.js module for interfacing with the Apple Push Notification service using NATIVE node.js http2 API (requires node v14.17+)
This package is supposed to be drop-in compatible with node-apn, however, only token based credentials are supported (p8).
Upgrading to v1.4.0
Version 1.4.0 adds comprehensive input validation. Code that previously passed invalid values silently will now throw descriptive errors. This helps catch bugs early, but may require code changes if you were relying on lenient behavior. See CHANGELOG.md for details.
Installation
yarn is the preferred installation method:
$ yarn add @vlasky/node-apn-http2Load in the module
var apn = require('@vlasky/node-apn-http2');var options = {
token: {
key: "path/to/APNsAuthKey_XXXXXXXXXX.p8",
keyId: "key-id",
teamId: "developer-team-id"
},
production: false
};
var apnProvider = new apn.Provider(options);By default, the provider will connect to the sandbox unless the environment variable NODE_ENV=production is set.
Sending a notification
To send a notification you will first need a device token from your app as a string
let deviceToken = "a9d0ed10e9cfd022a61cb08753f49c5a0b0dfb383697bf9f9d750a1003da19c7"var note = new apn.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge = 3;
note.sound = "ping.aiff";
note.alert = "\uD83D\uDCE7 \u2709 You have a new message";
note.payload = {'messageFrom': 'John Appleseed'};
note.topic = "<your-app-bundle-id>";Send the notification to the API with send, which returns a promise.
apnProvider.send(note, deviceToken).then( (result) => {
// see documentation for an explanation of result
});This will result in the the following notification payload being sent to the device
{"messageFrom":"John Appleseed","aps":{"badge":3,"sound":"ping.aiff","alert":"\uD83D\uDCE7 \u2709 You have a new message"}}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.
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.
Troubleshooting
You are encouraged to read the extremely informative Troubleshooting Push Notifications Tech Note in the first instance, in case your query is answered there.
Changelog
See CHANGELOG.md for a detailed list of changes.
