# Update FCM Token

Register or update a Firebase Cloud Messaging (FCM) device token so the user receives push notifications for transactions and alerts.

## Overview

Firebase Cloud Messaging (FCM) tokens identify a specific device for push notification delivery. Call this endpoint after the user logs in and after Firebase issues a token. The token can change (e.g. when the app is reinstalled), so you should call this endpoint every time Firebase provides a new token.

Specify the `platform` so Aurea can send platform-specific payloads: `ios`, `android`, or `web`.

> ℹ️ To stop receiving notifications (e.g. on logout), call `DELETE /v1/notifications/device-token` with the token instead of this endpoint.

### `PATCH /v1/users/me/fcm-token`

Authentication: bearer token required.

Registers or updates the authenticated user's FCM device token.

**Request body**

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `fcmToken` | string | yes | Firebase Cloud Messaging device token |
| `platform` | string | no | Device platform: ios, android, or web (defaults to ios) |

**Responses**

`200` OK

```json
{ "success": true, "message": "FCM token updated" }
```

## Implementation

```javascript
// After login: get FCM token from Firebase and register it with Aurea
import { initializeApp } from 'firebase/app';
import { getMessaging, getToken, onMessage } from 'firebase/messaging';

const app = initializeApp({ /* your Firebase config */ });
const messaging = getMessaging(app);

async function registerForPushNotifications(aureaToken) {
  try {
    // Request notification permission
    const permission = await Notification.requestPermission();
    if (permission !== 'granted') return;

    // Get the FCM token from Firebase
    const fcmToken = await getToken(messaging, {
      vapidKey: 'YOUR_VAPID_KEY' // Web push certificate from Firebase console
    });

    // Register the token with Aurea
    await fetch('https://api.aureahub.com/v1/users/me/fcm-token', {
      method: 'PATCH',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${aureaToken}`
      },
      body: JSON.stringify({ fcmToken, platform: 'web' })
    });

    console.log('Push notifications registered');

    // Listen for foreground messages
    onMessage(messaging, (payload) => {
      console.log('Push notification received:', payload);
      showNotificationToast(payload.notification);
    });

  } catch (err) {
    console.warn('Push notification setup failed:', err);
  }
}
```

---

Web version: https://docs.aureahub.com/#users-fcm
