From b7e9fec501fef6ed264d32e588be5c9da902e806 Mon Sep 17 00:00:00 2001 From: Andrew Naylor Date: Mon, 1 Jul 2013 01:41:43 +0100 Subject: [PATCH] Added some initial examples. --- examples/feedback.js | 17 +++++++ examples/sending-to-multiple-devices.js | 60 +++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 examples/feedback.js create mode 100644 examples/sending-to-multiple-devices.js diff --git a/examples/feedback.js b/examples/feedback.js new file mode 100644 index 0000000..5a39239 --- /dev/null +++ b/examples/feedback.js @@ -0,0 +1,17 @@ +var apn = require ('../index.js'); + +// Setup a connection to the feedback service using a custom interval (10 seconds) +var feedback = new apn.feedback({ gateway:'feedback.sandbox.push.apple.com', interval: 10 }); + +feedback.on('feedback', handleFeedback); +feedback.on('feedbackError', console.error); + +function handleFeedback(feedbackData) { + var time, device; + for(var i in feedbackData) { + time = feedbackData[i].time; + device = feedbackData[i].device; + + console.log("Device: " + device.toString() + " has been unreachable, since: " + time); + } +} \ No newline at end of file diff --git a/examples/sending-to-multiple-devices.js b/examples/sending-to-multiple-devices.js new file mode 100644 index 0000000..a99e38c --- /dev/null +++ b/examples/sending-to-multiple-devices.js @@ -0,0 +1,60 @@ +var apn = require ('../index.js'); + +var tokens = ["", ""]; + +if(tokens[0] == "") { + console.log("Please set token to a valid device token for the push notification service"); + process.exit(); +} + +// Create a connection to the service using mostly default parameters. + +var service = new apn.connection({ gateway:'gateway.sandbox.push.apple.com' }); + +service.on('connected', function() { + console.log("Connected"); +}); + +service.on('transmitted', function(notification, device) { + console.log("Notification transmitted to:" + device.token.toString()); +}); + +service.on('transmissionError', function(errCode, notification, device) { + console.error("Notification caused error: " + errCode + " for device ", device, notification); +}); + +service.on('timeout', function () { + console.log("Connection Timeout"); +}); + +service.on('disconnected', function() { + console.log("Disconnected from APNS"); +}); + +service.on('socketError', console.error); + + +// If you plan on sending identical paylods to many devices you can do something like this. +function pushNotificationToMany() { + var note = new apn.notification(); + note.setAlertText("Hello, from node-apn!"); + note.badge = 1; + + service.pushNotification(note, tokens); +} + +pushNotificationToMany(); + + +// If you have a list of devices for which you want to send a customised notification you can create one and send it to and individual device. +function pushSomeNotifications() { + for (var i in tokens) { + var note = new apn.notification(); + note.setAlertText("Hello, from node-apn! You are number: " + i); + note.badge = i; + + service.pushNotification(note, tokens[i]); + } +} + +pushSomeNotifications();