Service for Sails framework with Pusher features.
- APNS (Apple Push Notification Service)
- GCMS (Google Cloud Messaging Service)
Install this module.
npm install sails-service-pusher
Then require it in your service and create pusher instance.
// api/services/PusherService.js
import PusherService from 'sails-service-pusher';
export default PusherService('ios', {
provider: {
cert: 'cert.pem',
key: 'key.pem',
production: false
}
});
// api/controllers/PusherController.js
export default {
send: function(req, res) {
PusherService
.send(['DEVICE_TOKEN_1', 'DEVICE_TOKEN_2'], {
title: req.param('title') || 'Pusher',
body: req.param('body') || 'Hello from sails-service-pusher'
})
.then(res.ok)
.catch(res.negotiate);
}
};
When you instantiate new instance via PusherService()
you can provide configuration object with 3 keys:
config.device
- {Array} Device tokens that should get notification (will be merged with another devices insend()
)config.provider
- {Object} Options that will go to each of SDKs (APN, GCM)config.notification
- {Object} Options that will go to each of notifications (it has one interface for each of providers, see below)
Each of Pusher instances has only one method:
Sends Push Notification.
device
- {Array} Device tokens (registration IDs) to which push need to send (mixed up with pre-defined devices).
notification
- {Object} Config for notification:
notification.title
- Notification titlenotification.body
- Notification body textnotification.icon
- Notification iconnotification.sound
- Notification sound to be playednotification.badge
- Indicates the badge on client app home iconnotification.payload
- Custom data to send within Push Notification
config
- Additional configuration for notification with specific platform. See appropriate documentation.
All of this examples contains all the configuration keys. And most of them is optional.
let ios = PusherService('ios', {
device: [], // Array of string with device tokens
provider: {
cert: 'cert.pem', // The filename of the connection certificate to load from disk
key: 'key.pem', // The filename of the connection key to load from disk
ca: [], // An array of trusted certificates
pfx: '', // File path for private key, certificate and CA certs in PFX or PKCS12 format
passphrase: '', // The passphrase for the connection key
production: false, // Specifies which environment to connect to: Production (if true) or Sandbox
voip: false, // Enable when you are using a VoIP certificate to enable paylods up to 4096 bytes
port: 2195, // Gateway port
rejectUnauthorized: true, // Reject Unauthorized property to be passed through to tls.connect()
cacheLength: 1000, // Number of notifications to cache for error purposes
autoAdjustCache: true, // Whether the cache should grow in response to messages being lost after errors
maxConnections: 1, // The maximum number of connections to create for sending messages
connectTimeout: 10000, // The duration of time the module should wait, in milliseconds
connectionTimeout: 3600000, // The duration the socket should stay alive with no activity in milliseconds
connectionRetryLimit: 10, // The maximum number of connection failures that will be tolerated before apn will "terminate"
buffersNotifications: true, // Whether to buffer notifications and resend them after failure
fastMode: false // Whether to aggresively empty the notification buffer while connected
},
notification: {
title: 'iOS Test Push', // Indicates notification title
body: 'Hey, there!', // Indicates notification body text
icon: '', // Indicates notification icon
sound: '', // Indicates sound to be played
badge: '', // Indicates the badge on client app home icon
payload: {} // Custom data to send within Push Notification
}
});
ios
.send(['TOKEN_1', 'TOKEN_2'], {
body: 'You can override pre-defined'
})
.then(console.log.bind(console))
.catch(console.error.bind(console));
let android = PusherService('android', {
device: [], // Array of string with device tokens
provider: {
apiKey: '<GOOGLE_API_KEY>', // Your Google Server API Key
maxSockets: 12, // Max number of sockets to have open at one time
proxy: 'http://your-proxy.com' // This is [just like passing a proxy on to request](https://github.com/request/request#proxies)
},
notification: {
title: 'Android Test Push', // Indicates notification title
body: 'Hey, there!', // Indicates notification body text
icon: '', // Indicates notification icon
sound: '', // Indicates sound to be played
badge: '', // Indicates the badge on client app home icon
payload: {} // Custom data to send within Push Notification
}
});
android
.send(['TOKEN_1', 'TOKEN_2'], {
body: 'You can override pre-defined'
})
.then(console.log.bind(console))
.catch(console.error.bind(console));
The MIT License (MIT)
Copyright (c) 2015 Eugene Obrezkov
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.