pushwoosh-node-client
A node js client to consume the Pushwoosh API to send push notifications to mobile devices
Quick Reference
Installation
npm i pushwoosh-client --save
Send message to all devices
var Pushwoosh = require('pushwoosh-client');
var client= new Pushwoosh("AppCode", "AuthToken");
client.sendMessage('Hello world', function(error, response) {
if (error) {
console.log('Some error occurs: ', error);
}
console.log('Pushwoosh API response is', response);
});
To a specific device or devices
To send messages to a specificed device or devices, you can pass a device token or an arrays with devices
client.sendMessage('Hello world', 'device token', function(error, response) {
...
});
client.sendMessage('Hello world', ['deviceToken1', 'deivceToken2'], function(error, response) {
...
});
To pass extra options (please refer to the Pushwoosh doc for the available options) , you could define an option object and pass it to the function as a 2nd or 3rd parameter. E.g. if you want to pass addtional payload to the device, you could do:
var Pushwoosh = require('pushwoosh-client'),
client= new Pushwoosh("AppCode", "AuthToken"),
options = {
data: {
username: 'bob smith',
email: 'bob@example.com'
}
};
client.sendMessage('Hello world', 'device token', options, function(error, response) {
...
});
Note that if you define devices or content in the options, the devices and message will be overwritten.
var options = {
data: {
username: 'bob smith',
email: 'bob@example.com'
},
devices: ['deviceToken1', 'deviceToken2', 'deviceToken3']
};
client.sendMessage('Hello world', 'device token', options, function(error, response) {
...
});
Then this will send to ['deviceToken1', 'deviceToken2', 'deviceToken3'] as defined in options. so you probably just want to just do
client.sendMessage('Hello world', options, function(error, response) {
...
});
Applications group
To use Puswoosh applications_group
code(which allows you to send to multilple applications) instead of application
code, you must pass a third options
argument when creating the client with useApplicationsGroup
set to true:
var Pushwoosh = require('pushwoosh-client');
var client= new Pushwoosh("AppsGroupCode", "AuthToken", {
useApplicationsGroup: true,
...
});
client.sendMessage('Hello world', function(error, response) {
...
});
client.sendMessage('Hello world', options, function(error, response) {
...
});
Register device
To register a device's push token in Pushwoosh:
var Pushwoosh = require('pushwoosh-client');
var client= new Pushwoosh('AppCode', 'AuthToken');
var registerDeviceOptions = {
push_token: 'pushtoken',
hwid: 'hwid',
device_type: 3,
language: 'en',
timezone: -3600
};
client.registerDevice(registerDeviceOptions, function(error, response) {
...
});
Unregister device
To unregister a device in Pushwoosh:
var Pushwoosh = require('pushwoosh-client');
var client= new Pushwoosh('AppCode', 'AuthToken');
var unregisterDeviceOptions = {
hwid: 'hwid'
};
client.unregisterDevice(unregisterDeviceOptions, function(error, response) {
...
});
Set tags for device
To set tags for a device in Pushwoosh:
var Pushwoosh = require('pushwoosh-client');
var client= new Pushwoosh('AppCode', 'AuthToken');
var setTagsOptions = {
hwid: 'hwid',
tags: {
price: "1.99",
language: "pl"
}
};
client.setTags(setTagsOptions , function(error, response) {
...
});
Get tags for device
To get tags for a device from Pushwoosh:
var Pushwoosh = require('pushwoosh-client');
var client= new Pushwoosh('AppCode', 'AuthToken');
var getTagsOptions = {
hwid: 'hwid'
};
client.getTags(getTagsOptions , function(error, response) {
...
});
Tests
npm test
Currently tests are all passed and with 100% coverage