-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
79 lines (70 loc) · 2.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const request = require('request');
require('dotenv').config();
const API_KEY = process.env.onesignal_api_key;
const ONESIGNAL_APP_ID = "YOUR ONESIGNAL APP ID";
const BASE_URL = "https://onesignal.com/api/v1";
/**
* OPTIONS BUILDER
* @param {string} method
* @param {string} path
* @param {object} body
* @returns {object} options
*/
const optionsBuilder = (method, path, body) => {
return {
method,
'url': `${BASE_URL}/${path}`,
'headers': {
'Content-Type': 'application/json',
'Authorization': `Basic ${API_KEY}`,
},
body: body ? JSON.stringify(body) : null,
};
}
/**
* CREATE A PUSH NOTIFICATION
* method: POST
* Postman: https://www.postman.com/onesignaldevs/workspace/onesignal-api/request/16845437-c4f3498f-fd80-4304-a6c1-a3234b923f2c
* API Reference: https://documentation.onesignal.com/reference#create-notification
* path: /notifications
* @param {object} body
*/
const createNotication = (body) => {
const options = optionsBuilder("POST","notifications", body);
console.log(options);
request(options, (error, response) => {
if (error) throw new Error(error);
console.log(response.body);
viewNotifcation(JSON.parse(response.body).id);
});
}
/**
* VIEW NOTIFICATION
* method: GET
* Postman: https://www.postman.com/onesignaldevs/workspace/onesignal-api/request/16845437-6c96ecf0-5882-4eac-a386-0d0cabc8ecd2
* API Reference: https://documentation.onesignal.com/reference#view-notification
* path: /notifications/{notification_id}?app_id=${ONE_SIGNAL_APP_ID}
* @param {string} notificationId
*/
const viewNotifcation = (notificationId) => {
const path = `notifications/${notificationId}?app_id=${ONESIGNAL_APP_ID}`;
const options = optionsBuilder("GET", path);
request(options, (error, response)=> {
if (error) throw new Error(error);
console.log(response.body);
});
}
/**
* RUN THE NODE JS APP
*/
const body = {
app_id: ONESIGNAL_APP_ID,
included_segments: ['Subscribed Users'],
data: {
foo: 'bar',
},
contents: {
en: 'Sample Push Message',
},
};
createNotication(body);