Skip to content

Instantly share code, notes, and snippets.

@jasminabasurita
Last active January 29, 2018 02:49
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 jasminabasurita/0dd97fd6ceea68034b89d4fb88300e37 to your computer and use it in GitHub Desktop.
Save jasminabasurita/0dd97fd6ceea68034b89d4fb88300e37 to your computer and use it in GitHub Desktop.
register for Push tutorial
const testData = {
title: "Testing",
body: "It's a success!",
icon: "/path/to/an/icon.png"
}
let subscription
let pushIntervalID
router.post("/register", (req, res, next) => {
subscription = req.body
console.log(subscription)
res.sendStatus(201)
pushIntervalID = setInterval(() => {
// sendNotification can only take a string as it's second parameter
webpush.sendNotification(subscription, JSON.stringify(testData))
.catch(() => clearInterval(pushIntervalID))
}, 30000)
})
router.delete("/unregister", (req, res, next) => {
subscription = null
clearInterval(pushIntervalID)
res.sendStatus(200)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment