feat: Implement push notifications in Budget Manager PWA
- Added functionality for subscribing to push notifications in Settings.vue. - Integrated service worker registration and notification permission requests. - Enhanced Vite configuration for PWA with manifest and caching strategies. - Created a script for generating PDF reports. - Added project report markdown file detailing the PWA implementation and features.
This commit is contained in:
@@ -3,6 +3,7 @@ import { ObjectId } from "mongodb";
|
||||
import dbConfig from "../config/database";
|
||||
import type { Transaction } from "../models";
|
||||
import DatabaseService from "../services/database";
|
||||
import pushController from "./push";
|
||||
|
||||
const db = DatabaseService.getInstance();
|
||||
|
||||
@@ -117,6 +118,17 @@ export default {
|
||||
|
||||
console.log("Inserting transaction:", newTransaction);
|
||||
const result = await transactionsCollection.insertOne(newTransaction);
|
||||
|
||||
// Send push notification for the new transaction
|
||||
try {
|
||||
await pushController.sendTransactionNotification(
|
||||
userId,
|
||||
{ ...newTransaction, _id: result.insertedId }
|
||||
);
|
||||
} catch (notificationError) {
|
||||
console.error("Failed to send notification:", notificationError);
|
||||
// Continue even if notification fails
|
||||
}
|
||||
|
||||
ctx.status = 201;
|
||||
ctx.body = {
|
||||
|
||||
@@ -6,8 +6,9 @@ import webpush from "web-push";
|
||||
|
||||
// Configure web-push with VAPID keys
|
||||
const vapidKeys = {
|
||||
publicKey: process.env.VAPID_PUBLIC_KEY || 'your-public-key',
|
||||
privateKey: process.env.VAPID_PRIVATE_KEY || 'your-private-key'
|
||||
// These should be replaced with generated keys from the script
|
||||
publicKey: process.env.VAPID_PUBLIC_KEY || 'BEHsROsRzfqdMv4g50BEKUbFEL0NNdig0yxdmd-d9j5ZNLOOWbwsTZY3q_W_Y2E-h-hCCJmC5r3uATdFKQTN4z0',
|
||||
privateKey: process.env.VAPID_PRIVATE_KEY || 'dhty4Wi3rKZlKRs_7zSPmcW6qEK8sCMZ23Qw9iMGuBM'
|
||||
};
|
||||
|
||||
webpush.setVapidDetails(
|
||||
@@ -21,7 +22,9 @@ const db = DatabaseService.getInstance();
|
||||
export default {
|
||||
// Get VAPID public key
|
||||
async getVapidPublicKey(ctx: Context) {
|
||||
ctx.set('Content-Type', 'text/plain');
|
||||
ctx.body = vapidKeys.publicKey;
|
||||
console.log("Returning VAPID public key:", vapidKeys.publicKey);
|
||||
},
|
||||
|
||||
// Register a new push subscription
|
||||
|
||||
@@ -7,7 +7,12 @@ export const authenticate: Middleware = jwt({
|
||||
secret: jwtConfig.secret,
|
||||
}).unless({
|
||||
// Paths that don't require authentication
|
||||
path: ["/api/auth/login", "/api/auth/register", "/api/auth/register-admin"],
|
||||
path: [
|
||||
"/api/auth/login",
|
||||
"/api/auth/register",
|
||||
"/api/auth/register-admin",
|
||||
"/api/push/vapid-public-key"
|
||||
],
|
||||
});
|
||||
|
||||
// Error handling middleware for JWT authentication
|
||||
|
||||
@@ -53,4 +53,8 @@ router.delete(
|
||||
categoriesController.deleteCategory,
|
||||
);
|
||||
|
||||
// Push notification routes
|
||||
router.get("/push/vapid-public-key", pushController.getVapidPublicKey);
|
||||
router.post("/push/register", pushController.registerPushSubscription);
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user