Skip to main content
POST
/
notifications
/
update-fcm-token-usage
Update FCM Token Usage
curl --request POST \
  --url http://localhost:3000/api/notifications/update-fcm-token-usage \
  --header 'Content-Type: application/json' \
  --data '
{
  "deviceId": "device_abc123"
}
'
{
  "success": true,
  "message": "FCM token usage updated successfully"
}

Overview

Updates the lastUsed timestamp for an FCM token. This endpoint can be called periodically to track which devices are actively being used, enabling better token lifecycle management.

Use Cases

  • Activity Tracking: Monitor which devices are actively in use
  • Token Cleanup: Identify stale tokens that haven’t been used in a while
  • Analytics: Track user engagement across multiple devices
  • Token Management: Maintain accurate device activity records

When to Call

This endpoint is optional but recommended for:
  • App launch (update token usage on startup)
  • Periodic background tasks (e.g., once per day)
  • After receiving push notifications (device is active)
This endpoint is not required for basic FCM functionality. Registration and removal are sufficient for most use cases.

Example Usage

Basic Usage

import * as Device from "expo-device";

// Update token usage on app startup
async function updateTokenUsage() {
  await fetch("/api/notifications/update-fcm-token-usage", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      deviceId: Device.deviceId,
    }),
  });
}

Using React Hook

import { useUpdateFCMTokenUsage } from '@/hooks/notifications/useFCMTokenRegistration';
import { useEffect } from 'react';

function App() {
  const { mutate: updateUsage } = useUpdateFCMTokenUsage();

  useEffect(() => {
    // Update usage on app mount
    updateUsage(Device.deviceId);
  }, []);

  return <YourApp />;
}

Periodic Updates

import { useEffect } from "react";
import { AppState } from "react-native";

function useTokenUsageTracking(deviceId: string) {
  const { mutate: updateUsage } = useUpdateFCMTokenUsage();

  useEffect(() => {
    // Update when app comes to foreground
    const subscription = AppState.addEventListener("change", (nextAppState) => {
      if (nextAppState === "active") {
        updateUsage(deviceId);
      }
    });

    return () => subscription.remove();
  }, [deviceId, updateUsage]);
}

Behavior

When token usage is updated:
  1. Only the lastUsed timestamp is modified
  2. Token itself and other metadata remain unchanged
  3. Operation is idempotent - safe to call frequently
  4. Fails silently if token doesn’t exist (returns success anyway)

Future Use Cases

The lastUsed timestamp can be used for:
  • Automatic Cleanup: Remove tokens not used in 90+ days
  • Analytics Dashboard: Show active devices per user
  • Security Monitoring: Detect unusual device activity
  • User Notifications: Alert users about unrecognized devices

See Also

Body

application/json
deviceId
string
required

Unique identifier for the device to update

Example:

"device_abc123"

Response

FCM token usage updated successfully

success
boolean
required

Whether the operation was successful

Example:

true

message
string
required

Success message

Example:

"FCM token usage updated successfully"