# FCM Quick Start Guide (HTTP v1 API)

Get push notifications working in 5 minutes using the modern Firebase HTTP v1 API!

## ⚠️ Important Note

Firebase deprecated the legacy Server Key on June 20, 2024. This guide uses the **new HTTP v1 API** which requires a service account JSON file instead.

## Step 1: Get Firebase Credentials (3 minutes)

### A. Get Your Project ID

1. Go to [Firebase Console](https://console.firebase.google.com/)
2. Select your project
3. Click ⚙️ → **Project settings**
4. Copy your **Project ID** (shown at the top)

Example: `betting-tips-12345`

### B. Download Service Account JSON

1. Still in **Project settings**, click **Service accounts** tab
2. Click **"Generate new private key"** button
3. Click **"Generate key"** to confirm
4. A JSON file downloads (save it securely!)

Example filename: `my-app-firebase-adminsdk-xxxxx.json`

## Step 2: Backend Configuration (1 minute)

1. **Place the JSON file** in your project:
   ```
   writable/firebase-service-account.json
   ```

2. **Add to `.gitignore`** (IMPORTANT for security):
   ```
   writable/firebase-service-account.json
   ```

3. **Update `.env` file**:
   ```env
   FCM_PROJECT_ID=betting-tips-12345
   FCM_SERVICE_ACCOUNT_PATH=writable/firebase-service-account.json
   ```

4. **Restart PHP server** (if needed)

✅ **Backend is ready!**

## Step 3: Flutter App Setup (2 minutes)

### A. Add Firebase Config Files

1. **For Android**: Download `google-services.json` from Firebase Console
   - Place in: `android/app/google-services.json`

2. **For iOS**: Download `GoogleService-Info.plist` from Firebase Console
   - Place in: `ios/Runner/GoogleService-Info.plist`

### B. Add Dependencies

Add to `pubspec.yaml`:
```yaml
dependencies:
  firebase_messaging: ^14.7.9
  firebase_core: ^2.24.2
```

Run:
```bash
flutter pub get
```

### C. Update API URL

In `betting_tips/lib/services/fcm_notification_service.dart`:
```dart
static const String _baseUrl = 'https://your-actual-domain.com';
```

### D. Initialize Firebase

In `main.dart`:
```dart
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'services/fcm_notification_service.dart';

@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
  runApp(MyApp());
}
```

### E. Add to Login/Logout

**After login**:
```dart
await FCMNotificationService.initialize(userMsisdn);
```

**On logout**:
```dart
await FCMNotificationService.deactivate();
```

✅ **Flutter is ready!**

## Step 4: Test It! (30 seconds)

1. Login to the Flutter app on a **real device** (emulators may not work properly)
2. Go to **Admin panel → Tips**
3. Click **⋮** on any tip → **"Send Notification"**
4. Check your phone! 📱

## That's It! 🎉

Your push notifications are now working with the modern HTTP v1 API!

## Troubleshooting

### Not receiving notifications?

**Check Backend:**
- ✅ `FCM_PROJECT_ID` is correct in `.env`
- ✅ `FCM_SERVICE_ACCOUNT_PATH` points to the JSON file
- ✅ JSON file exists in the specified location
- ✅ Check `writable/logs/` for errors

**Check Flutter:**
- ✅ `google-services.json` (Android) or `GoogleService-Info.plist` (iOS) is in place
- ✅ Firebase is initialized in `main.dart`
- ✅ Notification permissions are granted
- ✅ Token is registered (check `device_tokens` table)

**Check Firebase Console:**
- ✅ Cloud Messaging API is enabled
- ✅ Service account has correct permissions

### Common Errors

**"FCM service account file not found"**
- Check the path in `FCM_SERVICE_ACCOUNT_PATH`
- Ensure file exists and is readable

**"Failed to get access token"**
- Verify JSON file is valid
- Check Firebase API is enabled in your project

**"Invalid service account JSON"**
- Re-download the service account JSON from Firebase Console
- Ensure it's the correct format

### Where to Find Your Sender ID

The Sender ID you saw (119168610137) is still used by Flutter, but the backend now uses:
- **Project ID** (for API endpoint)
- **Service Account JSON** (for authentication)

Your Sender ID is automatically included in the `google-services.json` or `GoogleService-Info.plist` files, so you don't need to configure it separately!

## Need More Help?

- **Full Setup Guide**: `FCM_NOTIFICATION_SETUP.md`
- **Implementation Details**: `FCM_IMPLEMENTATION_SUMMARY.md`
- **Flutter Service Code**: `betting_tips/lib/services/fcm_notification_service.dart`

---

**Pro Tip**: Test with your own device first before sending to all users!

**Important**: The legacy Server Key API is deprecated. This new approach is required for all Firebase projects.
