# Push Notifications (FCM) — Setup

The API uses **Firebase Admin SDK** to send FCM push notifications to mobile
devices for likes, comments, schedule events, and orders. The Admin SDK needs
a **service account key** to authenticate with Google.

## 1. Create a service account key

1. Open the Firebase console for the **`sbbackend-dc3a6`** project:
   https://console.firebase.google.com/project/sbbackend-dc3a6/settings/serviceaccounts/adminsdk
2. Click **"Generate new private key"** → confirm → a JSON file downloads.
3. Keep this file private — it grants full admin access to the Firebase project.

## 2. Make the API find it

Pick **one** of the four supported options:

### Option A — drop the file in (easiest for local dev)

```bash
mv ~/Downloads/sbbackend-dc3a6-*.json \
   packages/api/firebase-service-account.json
```

The file is already in `.gitignore`, so it won't be committed.

### Option B — set a custom env var (good for CI / shared dev)

```bash
# packages/api/.env
FIREBASE_SERVICE_ACCOUNT_PATH=/absolute/path/to/sbbackend-dc3a6-firebase-adminsdk.json
```

### Option C — standard Google ADC

```bash
# packages/api/.env (or your shell profile)
GOOGLE_APPLICATION_CREDENTIALS=/absolute/path/to/sbbackend-dc3a6-firebase-adminsdk.json
```

### Option D — inline JSON (cPanel / serverless / Docker secrets)

```bash
# packages/api/.env  (single line, escape newlines in private_key with \n)
FIREBASE_SERVICE_ACCOUNT_JSON={"type":"service_account","project_id":"sbbackend-dc3a6",...}
```

## 3. Verify

Restart the API:

```bash
npm run dev:api
```

On boot you should see one of:

```
[PushNotificationsService] Firebase Admin initialised (project=sbbackend-dc3a6).
```

If you still see:

```
Firebase Admin NOT initialised — push notifications disabled.
```

…then none of the options above were picked up. Double-check the path and that
the file is valid JSON.

## How it integrates

- `PushNotificationsService` is exported from `NotificationsModule`.
- Injected into `PostService`, `OrderService`, and `ScheduleService`.
- Every `prisma.notification.create*` call is followed by a fire-and-forget
  `pushNotifications.sendToUser(...)`.
- Failures (no token, expired token, network) are logged but **never** break
  the underlying business logic (like/comment/order/schedule still succeed).
- Expired tokens are auto-cleaned from `user.pushToken`.

## Tap routing (on the mobile side)

Tapping a notification (foreground, background, or cold start) is mapped to a
route by `packages/app/utils/notificationRouter.js`:

| Type                                   | Destination                          |
| -------------------------------------- | ------------------------------------ |
| `LIKE`, `NEW_COMMENT`, `COMMENT_REPLY` | `/post/[postId]`                     |
| `NEW_SCHEDULE`, `NEW_EVENT`, …         | `/schedule/[username]?eventId=…`     |
| `NEW_PRODUCT`, `PRODUCT_LISTED`        | `/product/[productId]`               |
| `BOOKING_CONFIRMATION`                 | `/manage-bookings`                   |
| `BOOKING_SUCCESS`                      | `/my-bookings`                       |
| `NEW_FOLLOWER`                         | `/user/[username]`                   |
| `ORDER_*`                              | `/product/[productId]` or `/my-purchases` |

## Sound mute toggle

Pinned to the top of the in-app notifications screen
(`packages/app/app/(dashboard)/notifications.js`). The setting is persisted in
AsyncStorage (`@lensseek/notificationSoundEnabled`) and consumed by
`Notifications.setNotificationHandler` in `usePushNotifications.js`.
