push-notify

Easily send notifications over multiple protocols.

push-notify

Easily send notifications over several protocols.

npm install push-notify
// Create a new APN sender. 
var apn = notify.apn({
  key: 'myKey.pem',
  cert: 'myCert.pem'
});
 
// Send a notification. 
apn.send({
  token: 'DEVICE_TOKEN',
  alert: 'Hello world!'
});
  {string|string[]} token Device token
  ...

Additional fields can be found in node-apn documentation.

APN use a socket and keep it connected, so if we want to gracefully stop a process, you will need to close this connection. A close method is avalaible on the APN sender.

apn.close(function () {
  // the connection is closed 
});

Emmited when a notification was correctly transmitted to Apple servers. You can find more details in node-apn documentation.

apn.on('transmitted', function (notificationdevice) {});

Emmited when a error occurs during notfication transmission. You can find more details in node-apn documentation.

apn.on('transmissionError', function (errorCodenotificationdevice) {});

Called when an error occurs during the connection to Apple servers. You can find more details in node-apn documentation.

apn.on('error', function (error) {});
// Create a new GCM sender. 
var gcm = require('push-notify').gcm({
  apiKey: 'GOOGLE_SERVER_API_KEY',
  retries: 1
});
 
// Send a notification. 
gcm.send({
  registrationId: 'REGISTRATION_ID',
  collapseKey: 'COLLAPSE_KEY',
  delayWhileIdle: true,
  timeToLive: 3,
  data: {
    key1: 'message1',
    key2: 'message2'
  }
});
  {string|string[]} registrationId Device registration id
  {string} collapseKey Collapse key
  {boolean} delayWhileIdle If included, indicates that the message should not be sent immediately if the device is idle. The server will wait for the device to become active, and then only the last message for each collapse_key value will be sent. Optional. The default value is false, and must be a JSON boolean.
  {number} timeToLive How long (in seconds) the message should be kept on GCM 
  {object} data Custom data

Emmited when a notification was correctly transmitted to Google servers.

gcm.on('transmitted', function (resultmessageregistrationId) {});

Emmited when a error occurs during the transmission of the message.

gcm.on('transmissionError', function (errormessageregistrationId) {});

Emmited when a registration id must be updated in the database.

gcm.on('updated', function (resultregistrationId) {});
// Create a new C2DM sender. 
var c2dm = require('push-notify').c2dm({
  user: 'user@gmail.com',
  password: 'password',
  domain: 'com.myapp'
});
 
// Send notification. 
c2dm.send({
  registration_id: 'REGISTRATION_ID',
  collapse_key: 'COLLAPSE_KEY',
  'data.titre': 'Hello world!',
  'data.text': 'Is that true?'
});
  {string|string[]]} registration_id Device registration id
  {string} collapse_key Collapse key
  {string} data.* Custom data field

Emmited when a notification was correctly transmitted to Google servers.

c2dm.on('transmitted', function (messageIdpayloadregistrationId) {});

Emmited when a error occurs during notfication transmission.

c2dm.on('transmissionError', function (errorpayloadregistrationId) {});

Called when an error occurs during the login.

c2dm.on('error', function (error) {});
// Create a new WNS sender. 
var wns = require('push-notify').wns({
  client_id: 'your Package Security Identifier',
  client_secret: 'your Client secret'
});
 
// Send notification. 
wns.send({
  channelURI: 'URI to your application notification channel',
  payload: 'XML containing the notification data',
  type: 'notification type'
});
  {string} channelURI URI for the device to send the notification to
  {string} payload the XML string containing the notif data
  {string} type notif type. One of: toast, badge, tile, raw
  {object} options an optional options object passed to wns lib (see wns lib)

Emmited when a notification was correctly transmitted to Microsoft servers.

wns.on('transmitted', function (result) {});

Emmited when a error occurs during the transmission of the message.

wns.on('transmissionError', function (error) {});
// Create a new MPNS sender. 
var mpns = require('push-notify').mpns();
 
// Send notification. 
mpns.send({
  pushUri: 'DEVICE_PUSH_URI',
  text1: 'Hello world!',
  text2: 'Is that true?'
});
  {string|string[]} pushUri Device push uri
  {string} text1 Text of the toast (bold)
  {string} text2 Text of the toast (normal)
  {string} param Optional uri parameters

Emmited when a notification was correctly transmitted to Microsoft servers.

mpns.on('transmitted', function (resultpayloadpushUri) {});

Emmited when a error occurs during the transmission of the message.

mpns.on('transmissionError', function (errorpayloadpushUri) {});

MIT