Skip to content

Instantly share code, notes, and snippets.

@mulhoon
Last active November 9, 2021 07:28
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save mulhoon/c8893a2f7a964bdcf3aa to your computer and use it in GitHub Desktop.
Save mulhoon/c8893a2f7a964bdcf3aa 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
@saudpunjwani101
Copy link

How a add title, badge or other objects in payload? the message only seems to be the body of the notification

@kotran88
Copy link

Just add one line to body.

body:{
'app_id': appID,
'contents': {en: message},
'include_player_ids': Array.isArray(device) ? device : [device],
'data': {'abc': '123','test': 'haha'}

		}

@djnejk
Copy link

djnejk commented Mar 16, 2021

Hello,
is it possible to send it to all devices?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment