Skip to content

Commit

Permalink
Merge pull request #107 from dvishal485/main
Browse files Browse the repository at this point in the history
update notif
  • Loading branch information
dvishal485 authored Aug 26, 2024
2 parents 4f30d2f + 76bb753 commit f927f7c
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/api/controllers/notificationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { Subscription } from "../models/subscriptionModel";
import webpush from "web-push";

export const push_notification = async () => {
export const push_notification = async (notification: ClientNotification) => {
const PRIVATE_KEY = process.env.NOTIF_PRIVATE_KEY;
if (!PRIVATE_KEY) {
throw new Error("Private key for notification API not configured");
Expand All @@ -18,9 +18,8 @@ export const push_notification = async () => {
const subscriptions = await Subscription.find();

const notificationPayload = JSON.stringify({
title: "New Notification",
body: "This is a new notification",
icon: "https://dtutimes.com/favicon.ico",
title: notification.title,
body: notification.description,
});

webpush.setVapidDetails(
Expand All @@ -32,9 +31,7 @@ export const push_notification = async () => {
subscriptions.forEach((subscription) => {
webpush
.sendNotification(subscription, notificationPayload)
.then(() => {
console.log("Notification sent");
})
.then(() => console.log)
.catch(async (err) => {
console.error("Error sending notification", err);
await subscription.deleteOne();
Expand All @@ -43,7 +40,11 @@ export const push_notification = async () => {
};

export const test_notif = asyncErrorHandler(async (req, res, _next) => {
await push_notification();
await push_notification({
title: "Test notification",
description: "This is a test notification",
actions: [],
});
res.json({
done: true,
});
Expand Down

0 comments on commit f927f7c

Please sign in to comment.