Skip to content

Loading…

Buffer notifications option #87

Closed
wants to merge 7 commits into from

4 participants

@joseprl89

I added a new option to pass to node-apn.

Documented as: @config {Boolean} [buffersNotifications=true] Whether to buffer notifications and resend them on reconnections.

If buffersNotification is true, node-apn will buffer notifications as done until now, and resend them as soon as reconnecting to apple's servers. If false, the notifications won't be buffered and resent.

This can be useful, as is my case, if developing an http push server, which will answer to the clients whether the push has failed or not. If the push fails to be delivered to apple, node-apn shouldn't try to resend it as the client application doesn't expects it to be sent.

Josep

porsager and others added some commits
@porsager porsager Update package.json
q was updated and has removed features. Dependency should not be as loose as higher than X version
e4a3550
@argon Merge pull request #82 from porsager/master
Q 0.9 has removed isResolved which breaks node-apn. Updated package json depenency to 0.8.x which solves it for now.
673c67b
@mgcrea mgcrea add support for mdm push notifications b3dad81
@argon Fixed mdm support. a1a5a65
@argon v1.2.6 cb4cb82
Josep Added stress test to check that failures can be managed by the library. 6e1566f
Josep Added buffers notification option 4985694
@argon argon closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 28, 2013
  1. @porsager

    Update package.json

    porsager committed
    q was updated and has removed features. Dependency should not be as loose as higher than X version
  2. Merge pull request #82 from porsager/master

    committed
    Q 0.9 has removed isResolved which breaks node-apn. Updated package json depenency to 0.8.x which solves it for now.
  3. @mgcrea
  4. Fixed mdm support.

    committed
  5. v1.2.6

    committed
Commits on Mar 8, 2013
Commits on Mar 12, 2013
  1. Added buffers notification option

    Josep committed
Showing with 101 additions and 17 deletions.
  1. +11 −1 README.md
  2. +21 −10 lib/connection.js
  3. +8 −3 lib/notification.js
  4. +3 −3 package.json
  5. +58 −0 test/stress.js
View
12 README.md
@@ -175,7 +175,7 @@ You will need the `debug` module which can be installed with `npm install debug`
Written and maintained by [Andrew Naylor][andrewnaylor].
-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]
+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]
## License
@@ -210,10 +210,20 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
[nsabovic]: https://github.com/nsabovic
[gimenete]: https://github.com/gimenete
[rwtombaugh]: https://github.com/rwtombaugh
+[thegreatmichael]: https://github.com/thegreatmichael
+[mgcrea]: https://github.com/mgcrea
+[porsager]: https://porsager
[q]: https://github.com/kriskowal/q
## Changelog
+1.2.6:
+
+* Added mdm support.
+* Constrained 'q' module to 0.8.x because 0.9.0 is API incompatible.
+* Fixed a `trim()` bug when compiling notification.
+* ***NOTICE:*** v1.3.0 which will be released soon will break some API compatibility with error handling and there will be a new sending API (the legacy sending API will remain)
+
1.2.5:
* Introduced a new event model. The connection class is now an event emitter which will emit events on connection state changes. This should largely replace the existing, somewhat inadequate error handling in previous versions. Please see the section above for more details or the message on commit d0a1d17961
View
31 lib/connection.js
@@ -38,6 +38,7 @@ if(process.env.DEBUG) {
* @config {Number} [cacheLength=100] Number of notifications to cache for error purposes (See Readme)
* @config {Boolean} [autoAdjustCache=false] Whether the cache should grow in response to messages being lost after errors. (Will still emit a 'cacheTooSmall' event)
* @config {Number} [connectionTimeout=0] The duration the socket should stay alive with no activity in milliseconds. 0 = Disabled.
+ * @config {Boolean} [buffersNotifications=true] Whether to buffer notifications and resend them on reconnections.
*/
function Connection (options) {
if(false === (this instanceof Connection)) {
@@ -59,7 +60,8 @@ function Connection (options) {
errorCallback: undefined,
cacheLength: 100,
autoAdjustCache: true,
- connectionTimeout: 0
+ connectionTimeout: 0,
+ buffersNotifications: true,
};
util.extend(this.options, options);
@@ -73,8 +75,11 @@ function Connection (options) {
this.currentId = 0;
this.cachedNotifications = [];
- this.notificationBuffer = [];
-
+
+ if ( this.options.buffersNotifications ) {
+ this.notificationBuffer = [];
+ }
+
events.EventEmitter.call(this);
};
@@ -236,9 +241,11 @@ Connection.prototype.socketDrained = function() {
return;
}
debug("Socket writeable");
- if (this.notificationBuffer.length > 0) {
- debug("Sending notification from buffer");
- this.sendNotification(this.notificationBuffer.shift());
+ if ( this.options.buffersNotifications ) {
+ if (this.notificationBuffer.length > 0) {
+ debug("Sending notification from buffer");
+ this.sendNotification(this.notificationBuffer.shift());
+ }
}
};
@@ -280,9 +287,11 @@ Connection.prototype.restartConnection = function() {
this.socket = undefined;
this.deferredConnection = undefined;
- if (this.notificationBuffer.length) {
- debug("Notification queue has %d items, resending the first", this.notificationBuffer.length);
- this.sendNotification(this.notificationBuffer.shift());
+ if ( this.options.buffersNotifications ) {
+ if (this.notificationBuffer.length) {
+ debug("Notification queue has %d items, resending the first", this.notificationBuffer.length);
+ this.sendNotification(this.notificationBuffer.shift());
+ }
}
};
@@ -290,7 +299,9 @@ Connection.prototype.restartConnection = function() {
* @private
*/
Connection.prototype.bufferNotification = function (notification) {
- this.notificationBuffer.push(notification);
+ if ( this.options.buffersNotifications ) {
+ this.notificationBuffer.push(notification);
+ }
};
/**
View
11 lib/notification.js
@@ -25,7 +25,7 @@ function Notification () {
*/
Notification.prototype.clone = function (device) {
var notification = new Notification();
-
+
notification.encoding = this.encoding;
notification.payload = this.payload;
notification.expiry = this.expiry;
@@ -36,7 +36,8 @@ Notification.prototype.clone = function (device) {
notification.badge = this.badge;
notification.sound = this.sound;
notification.newsstandAvailable = this.newsstandAvailable;
-
+ notification.mdm = this.mdm;
+
return notification
}
@@ -173,6 +174,10 @@ Notification.prototype.toJSON = function () {
if (this.payload === undefined) {
this.payload = {};
}
+ if (typeof this.mdm == 'string') {
+ this.payload.mdm = this.mdm;
+ return this.payload;
+ }
if (this.payload.aps === undefined) {
this.payload.aps = {};
}
@@ -191,4 +196,4 @@ Notification.prototype.toJSON = function () {
return this.payload;
};
-module.exports = Notification;
+module.exports = Notification;
View
6 package.json
@@ -1,7 +1,7 @@
{
"name": "apn",
"description": "An interface to the Apple Push Notification service for Node.js",
- "version": "1.2.5",
+ "version": "1.2.6",
"author": "Andrew Naylor <argon@mkbot.net>",
"contributors": [
{ "name": "Andrew Naylor", "email": "argon@mkbot.net" }
@@ -17,8 +17,8 @@
"url": "https://github.com/argon/node-apn.git"
},
"dependencies": {
- "q": ">=0.8.5"
+ "q": "0.8.x"
},
"engines": { "node": ">= 0.6.6" },
"license": "MIT"
-}
+}
View
58 test/stress.js
@@ -0,0 +1,58 @@
+// Require apn
+var apns = require('apn');
+
+// This function should be called every time a notification is not properly sent to APNS.
+var callbackFunction = function(err, notification){
+ console.log("Fail callback Called : " + err);
+ console.log("notification: " + notification);
+};
+
+var deviceToken = "INSERT YOUR TOKEN HERE";
+
+// APNS Connection Options.
+var options = {
+ cert: null, /* Certificate file path */
+ certData: null, /* String or Buffer containing certificate data, if supplied uses this instead of cert file path */
+ key: null, /* Key file path */
+ keyData: null, /* String or Buffer containing key data, as certData */
+ passphrase: null, /* A passphrase for the Key file */
+ ca: null, /* String or Buffer of CA data to use for the TLS connection */
+ pfx: null, /* File path for private key, certificate and CA certs in PFX or PKCS12 format. If supplied will be used instead of certificate and key above */
+ pfxData: null, /* PFX or PKCS12 format data containing the private key, certificate and CA certs. If supplied will be used instead of loading from disk. */
+ gateway: 'gateway.sandbox.push.apple.com',
+ port: 2195, /* gateway port */
+ rejectUnauthorized: true, /* Value of rejectUnauthorized property to be passed through to tls.connect() */
+ enhanced: true, /* enable enhanced format */
+ cacheLength: 100, /* Number of notifications to cache for error purposes */
+ autoAdjustCache: true, /* Whether the cache should grow in response to messages being lost after errors. */
+ connectionTimeout: 60000, /* The duration the socket should stay alive with no activity in milliseconds. 0 = Disabled. */
+ errorCallback: callbackFunction /* Callback when error occurs function(err,notification) */
+};
+
+// Creating the connection and the device to send notifications.
+var apnsConnection = new apns.Connection(options);
+var myDevice = new apns.Device(deviceToken);
+
+//////////////////////////////////////////////////////////////////////////////////////
+// This method runs every 2 seconds, and tries to send a notification to the device.//
+//////////////////////////////////////////////////////////////////////////////////////
+
+var periodicTask = function () {
+
+ var note = new apns.Notification();
+
+ note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
+ note.badge = 3;
+ note.sound = "ping.aiff";
+ note.alert = "You have a new message";
+ note.payload = {'messageFrom': 'Caroline'};
+ note.device = myDevice;
+ apnsConnection.sendNotification(note);
+
+ console.log("Sent notification...");
+ setTimeout(periodicTask, 2 * 1000);
+}
+
+// Start running periodically the task.
+periodicTask();
+
Something went wrong with that request. Please try again.