Athena — roomy-mobile/archive/changes/2026-01-05-implement-notifications/tasks/003-implement-notification-cloud-functions.md


skill-level: senior status: done

Task: Implement Notification Cloud Functions

End Goal

Cloud Functions that trigger on shopping list item events, batch notifications within a 30-second window, and dispatch FCM push notifications to household members.

Currently

  • No notification-related Cloud Functions exist
  • Shopping list item creates/updates do not trigger any notifications
  • No batching infrastructure exists

Should

  • Firestore trigger on shoppingListItems collection for create/update events
  • Cloud Tasks handle 30-second batching window
  • Notifications exclude the actor (user who performed the action)
  • FCM multicast sends to all recipient devices
  • Invalid tokens are cleaned up on send failure

Constraints

  • [x] Follow existing Cloud Function patterns (region: europe-west3)
  • [x] Use TypeScript with strict typing
  • [x] Batch key format: {householdId}:{actorUserId}:{action}:{timestampBucket}
  • [x] Handle Cloud Task retries idempotently

Acceptance Criteria

  • [x] Shopping item creation triggers notification function
  • [x] Shopping item completion triggers notification function
  • [x] Notifications are batched within 30-second windows
  • [x] Actor does not receive notification for own action
  • [x] FCM messages are sent to all recipient devices
  • [x] Invalid tokens are removed from Firestore

Implementation Checklist

  • [x] 3.1 Create roomy-firebase/functions/src/notifications/ directory
  • [x] 3.2 Create notificationDto.ts for notification document structure
  • [x] 3.3 Create onShoppingListItemWrite.ts trigger function
  • [x] 3.4 Implement action detection (add vs complete)
  • [x] 3.5 Implement household member lookup (exclude actor)
  • [x] 3.6 Create Cloud Task queue configuration
  • [x] 3.7 Implement processNotificationBatch.ts Cloud Task handler
  • [x] 3.8 Implement notification document creation
  • [x] 3.9 Implement FCM multicast sending
  • [x] 3.10 Implement invalid token cleanup on send failure
  • [x] 3.11 Export functions in index.ts
  • [x] 3.12 Deploy functions to Firebase

Notes

  • Cloud Tasks require cloudtasks.googleapis.com API enabled
  • Use admin.messaging().sendEachForMulticast() for efficient multicast
  • Error code messaging/registration-token-not-registered indicates invalid token
  • Batch aggregation uses Firestore transaction to count items in window

Reacties

Nog geen reacties