Skip to content

Instantly share code, notes, and snippets.

@AlexisLeon
Forked from mulhoon/onesignal-node.js
Created April 6, 2017 06:03
Show Gist options
  • Save AlexisLeon/1ffb7c26a07c3831cd5b19bad601990c to your computer and use it in GitHub Desktop.
Save AlexisLeon/1ffb7c26a07c3831cd5b19bad601990c to your computer and use it in GitHub Desktop.
Send a push notification in node with OneSignal
var request = require('request');
var sendMessage = function(device, message){
var restKey = '****';
var appID = '****';
request(
{
method:'POST',
uri:'https://onesignal.com/api/v1/notifications',
headers: {
"authorization": "Basic "+restKey,
"content-type": "application/json"
},
json: true,
body:{
'app_id': appID,
'contents': {en: message},
'include_player_ids': Array.isArray(device) ? device : [device]
}
},
function(error, response, body) {
if(!body.errors){
console.log(body);
}else{
console.error('Error:', body.errors);
}
}
);
}
sendMessage('a9fb63b1-b5cc-4ee9-92f0-5be15eb300c0', 'Hello!');
// Also accepts an array of devices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment