Skip to content

Instantly share code, notes, and snippets.

@Olanetsoft
Forked from mulhoon/onesignal-node.js
Created May 19, 2021 01:24
Show Gist options
  • Save Olanetsoft/d84acdd4c864f87a540c6a0933363f4f to your computer and use it in GitHub Desktop.
Save Olanetsoft/d84acdd4c864f87a540c6a0933363f4f 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