Last active
November 9, 2021 07:28
-
-
Save mulhoon/c8893a2f7a964bdcf3aa to your computer and use it in GitHub Desktop.
Send a push notification in node with OneSignal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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'}
}
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
How a add title, badge or other objects in payload? the message only seems to be the body of the notification