pbnkp
question.. web push notifications having trouble with modules
Dam that was a long 18 + hours trying to make my code function. Just when I was so close to finishing it there were just so many failures and the web push library from NPM I converted to Deno wouldn’t work without the crypto module.
13 replies
question.. web push notifications having trouble with modules
Thank you I’m going to try that. I’m still learning Deno and coming from php it’s been a bit of a curve but one that I’m loving and happy to be moving to. I’m working on each area of a production level system so we are 100% Deno and increase performance.
13 replies
question.. web push notifications having trouble with modules
router.get("/sendwebpushnotification", async (ctx) => {
try {
const allSubscriptions = await subscriptionCollection.find();
for (const item of allSubscriptions) {
let theMessage = JSON.stringify({
title: "Some notification",
body: "Some description",
});
await webpush.sendNotification(item.subscriptionEl, theMessage, { vapidDetails: webpushVapidDetails }).catch(
(error) => {
console.error(error.stack);
}
);
}
ctx.response.status = 200;
ctx.response.body = { message: "notification sent" };
} catch (e) {
console.log(e);
}
});
app.use(router.routes());
app.use(router.allowedMethods());
router.use("/", router.routes());
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 80 });
const mail = "random@mydomain";
const publicKey = "vapidpub";
const privateKey = "vapidpriv";
// (E) SEND TEST PUSH NOTIFICATION
router.get("/", async (ctx) => {
await send(ctx, "/testme.html", { root: Deno.cwd() });
});
router.post("/goodboy", async (ctx) => {
ctx.response.status = 201;
ctx.response.body = {};
try {
await webpush.sendNotification(ctx.request.body, JSON.stringify({
title: "😊",
body: "👍🏼",
icon: "i-ico.png",
image: "i-banner.png"
}), { vapidDetails: webpushVapidDetails });
} catch (err) {
console.log(err);
}
});
// (F) START!
app.listen(port, () => console.log(
🙏🏽 is live on port ${port}
));13 replies
question.. web push notifications having trouble with modules
// Subscribe
router.post("/subscribe", async (ctx) => {
try {
const body = await ctx.request.body();
const subscription = body.value;
const hashValue = crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify(subscription)));
const hash = Array.from(new Uint8Array(hashValue)).map(byte => byte.toString(16).padStart(2, '0')).join('');
const checkSubscription = await subscriptionCollection.findOne({
hash: hash,
});
let theMessage = JSON.stringify({
title: "You have already subscribed",
body: "Although we appreciate you trying to again",
});
if (!checkSubscription) {
const newSubscription = {
hash: hash,
subscriptionEl: subscription,
};
await subscriptionCollection.insertOne(newSubscription);
theMessage = JSON.stringify({
title: "Thank you for Subscribing!",
body: "Some body message here",
});
}
ctx.response.status = 201;
ctx.response.body = {};
await webpush.sendNotification(subscription, theMessage, { vapidDetails: webpushVapidDetails });
} catch (e) {
console.log(e);
}
});
13 replies