- ✅ Installed via
npm install firebase-admin - ✅ Added to package.json dependencies
- ✅ Endpoint created in
src/routes/notifications.routes.ts - ✅ Controller method:
PushController.subscribe - ✅ Saves FCM token with device type and device ID
- ✅ Validates input using Zod schema
- ✅ Returns tokenId on success
- ✅ Endpoint created in
src/routes/notifications.routes.ts - ✅ Controller method:
PushController.unsubscribe - ✅ Removes FCM token from database
- ✅ Returns 404 if token not found
- ✅
sendToUser(userId, title, body, data)method implemented - ✅ Firebase Admin SDK initialization
- ✅ Multi-device support (sends to all user tokens)
- ✅ Automatic invalid token handling
- ✅
sendSessionReminder()- 15 minutes before session - ✅
sendPaymentConfirmed()- payment processed - ✅
sendNewMessage()- new message received - ✅ Integrated with NotificationService for PUSH channel
- ✅ Detects FCM error codes:
invalid-registration-token,registration-token-not-registered - ✅ Marks tokens as inactive in database
- ✅
handleInvalidTokens()method processes cleanup - ✅ Removes tokens on 404 response
- ✅ Database schema allows multiple tokens per user
- ✅
getActiveTokensByUserId()returns all active tokens - ✅
sendToUser()sends to all devices - ✅ Tracks
last_used_atfor each token
- ✅ Checks
push_enabledflag before sending - ✅ Integrates with
NotificationPreferencesModel - ✅ Returns error if push disabled
- ✅ Send success test
- ✅ Invalid token cleanup test
- ✅ Preference check test
- ✅ Multi-device test
- ✅ Controller subscribe/unsubscribe tests
- ✅ All 10 tests passing
src/services/push.service.ts(306 lines) - FCM service with sendToUser, sendSessionReminder, sendPaymentConfirmed, sendNewMessagesrc/controllers/push.controller.ts(133 lines) - Subscribe, unsubscribe, getTokens, sendTest endpointssrc/models/push-tokens.model.ts(149 lines) - Database operations for FCM tokens
database/migrations/016_create_push_tokens.sql- Creates push_tokens table with indexes
src/services/__tests__/push.service.unit.test.ts(5 tests)src/controllers/__tests__/push.controller.unit.test.ts(5 tests)src/models/__tests__/push-tokens.model.test.ts(comprehensive model tests)
docs/push-notifications.md- Complete setup and usage guidePUSH_NOTIFICATIONS_QUICK_REFERENCE.md- Quick start guide
- Updated
src/config/env.ts- Added Firebase env variables - Updated
.env.example- Added Firebase configuration template - Updated
.env.test- Added test Firebase credentials - Updated
src/routes/notifications.routes.ts- Added push routes with Swagger docs
The push notification system seamlessly integrates with the existing notification infrastructure:
- NotificationChannel.PUSH - Already defined in notifications model
- NotificationService - Auto-sends push when channel is PUSH
- NotificationPreferencesModel - Respects push_enabled flag
- Multi-channel support - Works alongside email and in-app notifications
Test Suites: 2 passed, 2 total
Tests: 10 passed, 10 total
All unit tests pass successfully with proper mocking of Firebase Admin SDK and database operations.
- Configure Firebase project and add credentials to
.env - Run migration:
npm run migrate:up - Implement client-side FCM token registration
- Add push notification triggers to booking/payment flows
- Set up scheduled job for session reminders (15 min before)
- Add cleanup job for inactive tokens (optional)
// In booking confirmation flow
await PushService.sendSessionReminder(menteeId, {
mentorName: mentor.name,
scheduledAt: booking.scheduled_at,
durationMinutes: booking.duration_minutes,
bookingId: booking.id
});
// In payment processing
await PushService.sendPaymentConfirmed(userId, {
amount: transaction.amount,
transactionId: transaction.id
});