Skip to content

Instantly share code, notes, and snippets.

@vacom
Created May 19, 2020 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vacom/923819f8b2b4afed2e96726cf1989223 to your computer and use it in GitHub Desktop.
Save vacom/923819f8b2b4afed2e96726cf1989223 to your computer and use it in GitHub Desktop.
const { json } = require("micro");
const axios = require("axios");
module.exports = async (req, res) => {
if (req.method !== "POST") {
res.end("404 - not found");
}
const content = await json(req);
const userId = content.data.Notification.node.patient.user.id;
const scheduling = content.data.Notification.node.scheduling;
const data = {
app_id: process.env.ONESIGNAL_APP_ID,
include_external_user_ids: [userId],
template_id: "9fe216ad-3726-423d-9c91-8a0ebdd2cf03",
send_after: scheduling
};
try {
await axios({
method: "post",
url: "https://onesignal.com/api/v1/notifications",
headers: {
Authorization: `Basic ${process.env.ONESIGNAL_APP_TOKEN}`,
"Content-Type": "application/json"
},
data
});
res.end("Push notification successfully registered");
} catch (error) {
res.end("Error creating Push Notification");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment