node-apn/README.md

158 строки
7.9 KiB
Markdown
Исходник Обычный вид История

2010-10-09 03:11:42 +04:00
#node-apn
A Node.js module for interfacing with the Apple Push Notification service.
## Features
- Fast
- Maintains a connection to the server to maximise notification batching and throughput.
- Enhanced binary interface support, with error handling
2010-10-09 03:11:42 +04:00
- Automatically sends unsent notifications if an error occurs
- Feedback service support
- Complies with all best practises laid out by Apple
2010-10-09 03:11:42 +04:00
## Installation
2010-10-10 03:08:26 +04:00
Via [npm][]:
2010-10-09 03:11:42 +04:00
$ npm install apn
As a submodule of your project (you will also need to install [q][q])
2010-10-09 03:11:42 +04:00
2010-10-10 03:08:26 +04:00
$ git submodule add http://github.com/argon/node-apn.git apn
$ git submodule update --init
2010-10-09 03:11:42 +04:00
## Quick Start
This is intended as a brief introduction, please refer to the documentation in `doc/` for more details.
2010-10-09 03:11:42 +04:00
### Load in the module
var apn = require('apn');
2010-10-09 03:11:42 +04:00
### Connecting
Create a new connection to the APN gateway server using a dictionary of options. If you name your certificate and key files appropriately (`cert.pem` and `key.pem`) then the defaults should be suitable to get you up and running, the only thing you'll need to change is the `gateway` if you're in the sandbox environment.
2010-10-09 03:11:42 +04:00
```javascript
var options = { "gateway": "gateway.sandbox.push.apple.com" };
var apnConnection = new apn.Connection(options);
```
Help with preparing the key and certificate files for connection can be found in the [wiki][certificateWiki]
2010-10-09 03:11:42 +04:00
### Sending a notification
To send a notification first create a `Device` object. Pass it the device token as either a hexadecimal string, or alternatively as a `Buffer` object containing the token in binary form.
2010-10-09 03:11:42 +04:00
var myDevice = new apn.Device(token);
2010-10-09 03:11:42 +04:00
Next, create a notification object, set the relevant parameters (See the [payload documentation][pl] for more details.) and use the `pushNotification` method on the connection to send it.
2010-10-09 03:11:42 +04:00
var note = new apn.Notification();
2010-10-09 03:11:42 +04:00
2012-06-16 19:46:03 +04:00
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
2010-10-09 03:11:42 +04:00
note.badge = 3;
note.sound = "ping.aiff";
note.alert = "\uD83D\uDCE7 \u2709 You have a new message";
2010-10-09 03:11:42 +04:00
note.payload = {'messageFrom': 'Caroline'};
apnConnection.pushNotification(note, myDevice);
2010-10-09 03:11:42 +04:00
The above options will compile the following dictionary to send to the device:
{"messageFrom":"Caroline","aps":{"badge":3,"sound":"ping.aiff","alert":"\uD83D\uDCE7 \u2709 You have a new message"}}
2010-10-09 03:11:42 +04:00
### Setting up the feedback service
Apple recommends checking the feedback service periodically for a list of devices for which there were failed delivery attempts.
Using the `Feedback` object it is possible to periodically query the server for the list. Many of the options are similar to that of `Connection`.
Attach a listener to the `feedback` event to receive the output as two arguments, the `time` returned by the server (epoch time) and a `Buffer` object containing the device token - this event will be emitted for each device separately. Alternatively you can enable the `batchFeedback` option and the `feedback` event will provide an array of objects containing `time` and `device` properties.
2010-10-09 03:11:42 +04:00
var options = {
"batchFeedback": true,
"interval": 300
};
2010-10-09 03:11:42 +04:00
var feedback = new apn.Feedback(options);
feedback.on("feedback", function(devices) {
devices.forEach(function(item) {
// Do something with item.device and item.time;
});
});
2010-10-09 03:11:42 +04:00
By specifying a time interval (in seconds) `Feedback` will periodically query the service without further intervention.
More information about the feedback service can be found in the [feedback service documentation][fs].
## Debugging
2012-08-02 21:25:09 +04:00
If you experience difficulties sending notifications or using the feedback service you can enable debug messages within the library by running your application with `DEBUG=apn` or `DEBUG=apnfb` set as an environment variable.
You will need the `debug` module which can be installed with `npm install debug`.
You are encouraged to read the extremely informative [Troubleshooting Push Notifications][tn2265] Tech Note in the first instance, in case your query is answered there.
## Support
If you have any questions or difficulties working with the module, the [node-apn Google group][googlegroup] should be your first port of call.
Please include as much detail as possible - especially debug logs, if the problem is reproducible sample code is also extremely helpful. GitHub Issues should only be created for verified problems and enhancements, this will allow them to be tracked more easily.
2013-04-21 04:28:48 +04:00
## Resources
* [Local and Push Notification Programming Guide: Apple Push Notification Service][pl]
* [Apple Technical Note: Troubleshooting Push Notifications][tn2265]
* [List of Projects, Applications and Companies Using Node-apn][pacapn]
2010-10-09 03:11:42 +04:00
## Credits
Written and maintained by [Andrew Naylor][andrewnaylor].
2010-10-09 03:11:42 +04:00
Thanks to: [Ian Babrou][bobrik], [dgthistle][dgthistle], [Keith Larsen][keithnlarsen], [Mike P][mypark], [Greg Bergé][neoziro], [Asad ur Rehman][AsadR], [Nebojsa Sabovic][nsabovic], [Alberto Gimeno][gimenete], [Randall Tombaugh][rwtombaugh], [Michael Stewart][thegreatmichael], [Olivier Louvignes][mgcrea], [porsager][porsager], [Craig Hockenberry][chockenberry]
2011-11-25 03:59:58 +04:00
2010-10-09 03:11:42 +04:00
## License
Released under the MIT License
Copyright (c) 2010 Andrew Naylor
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[certificateWiki]:https://github.com/argon/node-apn/wiki/Preparing-Certificates "Preparing Certificates"
[errors]:https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW4 "The Binary Interface and Notification Formats"
[pl]: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW1 "Local and Push Notification Programming Guide: Apple Push Notification Service"
[fs]: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3 "The Feedback Service"
2013-04-21 04:28:48 +04:00
[tn2265]: http://developer.apple.com/library/ios/#technotes/tn2265/_index.html "Troubleshooting Push Notifications"
[googlegroup]:https://groups.google.com/group/node-apn "node-apn Google Group"
2013-04-21 04:28:48 +04:00
[pacapn]:https://github.com/argon/node-apn/wiki/Projects,-Applications,-and-Companies-Using-Node-apn "List of Projects, Applications and Companies Using Node-apn"
[andrewnaylor]: http://andrewnaylor.co.uk
2010-10-10 03:08:26 +04:00
[bnoordhuis]: http://bnoordhuis.nl
[npm]: https://npmjs.org
2011-11-25 03:59:58 +04:00
[bobrik]: http://bobrik.name
[dgthistle]: https://github.com/dgthistle
[keithnlarsen]: https://github.com/keithnlarsen
[mypark]: https://github.com/mypark
2012-06-16 19:41:36 +04:00
[neoziro]: https://github.com/neoziro
[AsadR]: https://github.com/AsadR
2012-09-24 18:00:15 +04:00
[nsabovic]: https://github.com/nsabovic
[gimenete]: https://github.com/gimenete
2012-12-22 04:12:40 +04:00
[rwtombaugh]: https://github.com/rwtombaugh
2013-03-01 00:04:21 +04:00
[thegreatmichael]: https://github.com/thegreatmichael
[mgcrea]: https://github.com/mgcrea
[porsager]: https://github.com/porsager
[q]: https://github.com/kriskowal/q
[chockenberry]: https://github.com/chockenberry