Skip to content

Latest commit

 

History

History
107 lines (86 loc) · 3.09 KB

File metadata and controls

107 lines (86 loc) · 3.09 KB

Push Notifications Quick Reference

Setup Checklist

  • Install firebase-admin SDK
  • Add Firebase credentials to .env
  • Run migration 016_create_push_tokens.sql
  • Create PushService with sendToUser() method
  • Create PushController with subscribe/unsubscribe endpoints
  • Add routes: POST /push/subscribe, DELETE /push/unsubscribe
  • Handle invalid/expired tokens (mark inactive on 404)
  • Support multiple devices per user
  • Respect user notification preferences
  • Unit tests: send success, invalid token cleanup, preference check

Quick Start

1. Configure Firebase

Add to .env:

FIREBASE_PROJECT_ID=your-project-id
FIREBASE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"
FIREBASE_CLIENT_EMAIL=firebase-adminsdk-xxxxx@your-project.iam.gserviceaccount.com

2. Subscribe Client

// Client-side: Get FCM token and subscribe
const token = await getFirebaseToken();
await fetch('/api/v1/notifications/push/subscribe', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${accessToken}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ token, deviceType: 'web' })
});

3. Send Notifications

// Session reminder (15 min before)
await PushService.sendSessionReminder(userId, {
  mentorName: 'John Doe',
  scheduledAt: sessionDate,
  durationMinutes: 60,
  bookingId: 'booking-123'
});

// Payment confirmed
await PushService.sendPaymentConfirmed(userId, {
  amount: '100.50',
  transactionId: 'tx-123'
});

// New message
await PushService.sendNewMessage(userId, {
  senderName: 'Jane Smith',
  messagePreview: 'Hey, are you available?',
  conversationId: 'conv-123'
});

API Endpoints

Method Endpoint Description
POST /api/v1/notifications/push/subscribe Save FCM token
DELETE /api/v1/notifications/push/unsubscribe Remove FCM token
GET /api/v1/notifications/push/tokens List active tokens
POST /api/v1/notifications/push/test Send test notification

Features

✅ Multi-device support (web, Android, iOS) ✅ Automatic invalid token cleanup ✅ User preference checking ✅ Multiple devices per user ✅ Last used timestamp tracking ✅ Comprehensive error handling

Testing

# Run unit tests
npm test -- --config=jest.unit.config.ts src/services/__tests__/push.service.unit.test.ts
npm test -- --config=jest.unit.config.ts src/controllers/__tests__/push.controller.unit.test.ts

Files Created

  • src/services/push.service.ts - FCM integration service
  • src/controllers/push.controller.ts - API endpoints
  • src/models/push-tokens.model.ts - Database model
  • database/migrations/016_create_push_tokens.sql - Schema
  • docs/push-notifications.md - Full documentation
  • Unit tests for service, controller, and model

Integration Points

The push notification system integrates with:

  • Notification preferences (respects push_enabled flag)
  • Multi-channel notification system (via NotificationChannel.PUSH)
  • Existing notification service (auto-sends on PUSH channel)