1.  **Analyze Existing Code and Infrastructure:**
    1.  1.  Review `@/html/early_access/api/notifications.php` to understand the current notification mechanism (likely APNs or a development FCM setup). Identify how it currently handles user IDs or any device identification.
    2.  2.  Examine `@/mysportmanager-shared/lib/services/notification_service.dart` to understand how the app currently registers for notifications and handles incoming notifications. Pay close attention to the existing token registration logic (if any).
    3.  3.  Examine `/mnt/ssd_data/bugs.md` to identify any known issues or bugs related to the current notification system. This will inform the testing and validation steps later.

2.  **Implement Device Token Storage in `notifications.php`:**
    1.  1.  Modify `@/html/early_access/api/notifications.php` to include a database table and associated code for storing device tokens.  Include fields for `user_id`, `device_token`, `platform` (iOS, Android), and potentially a `timestamp` for tracking token updates.
    2.  2.  Implement an API endpoint within `notifications.php` (e.g., `/register_token.php`) to receive device tokens from the mobile app.  This endpoint should:
        *   Validate the incoming `user_id` and `device_token`.
        *   Determine the platform (iOS/Android) based on the token or a separate parameter.
        *   Store the `device_token` in the database, associating it with the `user_id` and platform.  Handle duplicate token registration (update existing record if necessary).
        *   Implement error handling and logging for token registration failures.
    3.  3.  Secure the token registration endpoint using appropriate authentication/authorization mechanisms (e.g., API keys, user authentication).

3.  **Create `fcm_sender.php` for FCM Push Notifications:**
    1.  1.  Create a new file `fcm_sender.php` in a suitable location (e.g., within the API directory).
    2.  2.  Implement the logic to send FCM push notifications using the FCM API. This will involve:
        *   Retrieving device tokens from the database based on the intended recipient(s) (`user_id` or group of `user_id`s).
        *   Constructing the FCM message payload (title, body, data).
        *   Using the FCM API (via a library or direct API calls) to send the message to the retrieved device tokens.
        *   Handling FCM API responses (success, failure, error codes) and logging the results.
        *   Implementing retry logic for failed message sends.
    3.  3.  Configure `fcm_sender.php` to be executed as a cron job.  Determine the appropriate frequency (e.g., every minute, every 5 minutes) based on the notification volume and urgency.
    4.  4.  Implement error handling and logging within `fcm_sender.php` to track notification delivery status and identify potential issues.
    5.  5.  Ensure that the FCM server key is securely stored and accessed within `fcm_sender.php` (e.g., using environment variables).

4.  **Update `notification_service.dart` to Register Tokens:**
    1.  1.  Modify `@/mysportmanager-shared/lib/services/notification_service.dart` to handle device token registration with the new `notifications.php` endpoint.
    2.  2.  Obtain the FCM token using the appropriate FlutterFire method (`FirebaseMessaging.instance.getToken()`).
    3.  3.  After obtaining the token, make an HTTP request to the `/register_token.php` endpoint in `notifications.php`, sending the `user_id` (obtain from user authentication), `device_token`, and platform (Android/iOS).
    4.  4.  Handle the response from the `notifications.php` endpoint.  Log success or failure.  Implement retry logic for token registration failures.
    5.  5.  Ensure that token registration occurs whenever the user logs in or the app is reinstalled.  Consider also refreshing the token periodically.
    6.  6.  Implement error handling and logging within `notification_service.dart` to track token registration status.

5.  **Testing and Validation:**
    1.  1.  Thoroughly test the entire notification flow, from token registration to notification delivery, on both Android and iOS devices.
    2.  2.  Test different notification types (e.g., transactional, promotional) and message payloads.
    3.  3.  Test with different network conditions (e.g., poor connectivity, airplane mode) to ensure robustness.
    4.  4.  Monitor the logs in `notifications.php` and `fcm_sender.php` to identify any errors or issues.
    5.  5.  Verify that notifications are delivered promptly and reliably.
    6.  6.  Address any bugs or issues identified during testing. Refer to `/mnt/ssd_data/bugs.md` for known issues and add new findings.
    7.  7.  Test the token refresh mechanism to ensure that tokens are updated correctly.
    8.  8.  Implement analytics tracking to monitor notification delivery rates and user engagement.

6.  **Deployment and Monitoring:**
    1.  1.  Deploy the updated `notifications.php` code to the production server.
    2.  2.  Configure the `fcm_sender.php` cron job on the production server.
    3.  3.  Monitor the notification delivery rates and error logs to ensure that the system is functioning correctly.
    4.  4.  Track FCM statistics in the Firebase console.
    5.  5.  Establish alerts for any critical errors or failures.

7.  **Documentation:**
    1.  1.  Document the new notification system architecture and implementation details.
    2.  2.  Document the API endpoints and data structures used for token registration and notification sending.
    3.  3.  Document the cron job configuration and monitoring procedures.
    4.  4.  Update any relevant documentation for the mobile app to reflect the new notification system.
