/**
 * Module dependencies.
 */

var EventEmitter = require('events').EventEmitter;
var util = require('util');
var pipeEvent = require('pipe-event');
var _ = require('lodash');
var apn = require('apn');

/**
 * Expose module.
 */

module.exports = Sender;

/**
 * Create a new APN sender.
 *
 * @param {object} options
 * @see https://github.com/argon/node-apn
 */

function Sender(options) {
  EventEmitter.call(this);
  this.options = options || {};
}

util.inherits(Sender, EventEmitter);

/**
 * Send a notification.
 *
 * @param {object} data Avalaible parameters can be found at
 * https://github.com/argon/node-apn/blob/master/doc/apn.markdown
 * @param {function} cb
 */

Sender.prototype.send = function (data) {
  // Create a new APN connection.
  this.connection = this.connection || this.createConnection();

  // Token can be string or array.
  if (! _.isArray(data.token)) data.token = [data.token];

  // Create notification.
  var notification = new apn.Notification();
  _.merge(notification, _.omit(data, 'token'));

  // Push notification.
  this.connection.pushNotification(notification, data.token);
};

/**
 * Close all connections.
 *
 * @param {function} cb
 */

Sender.prototype.close = function (cb) {
  if (! this.connection) {
    if (cb) process.nextTick(cb);
    return ;
  }

  this.connection.shutdown();
  // node-apn use a delay of 2500ms before really closing the connection
  // https://github.com/argon/node-apn/blob/3731fae9fefccdb7964606d819561058debf4b70/lib/connection.js#L355
  if (cb) setTimeout(cb, 2600);
};

/**
 * Create a new APN connection and proxify events.
 *
 * @returns {apn.Connection}
 */

Sender.prototype.createConnection = function () {
  var connection = new apn.Connection(this.options);

  pipeEvent([
    'error',
    'socketError',
    'transmitted',
    'cacheTooSmall',
    'connected',
    'disconnected',
    'timeout',
    'transmissionError'
  ], connection, this);

  return connection;
};
@@ -712,16 +712,22 @@
 on (data
+, trim
 ) %7B%0A  //
@@ -1031,24 +1031,174 @@
 'token'));%0A%0A
+  // Attempt to trim the alert text body to meet the payload size limit.%0A  // TODO: write tests%0A  if (trim !== false) %7B%0A    notification.trim();%0A  %7D%0A%0A
   // Push no

Change default APN send behaviour to trim the payload