Last active
January 29, 2018 02:49
-
-
Save jasminabasurita/0dd97fd6ceea68034b89d4fb88300e37 to your computer and use it in GitHub Desktop.
register for Push tutorial
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
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