# FCM Setup Checklist for Milton SDK

This checklist ensures you have properly configured Firebase Cloud Messaging (FCM) for the Milton Health Coach SDK. **FCM is mandatory** for the SDK to function.

## ✅ **Pre-Installation Checklist**

### Firebase Project Setup
- [ ] Created Firebase project at [Firebase Console](https://console.firebase.google.com/)
- [ ] Added Android app to Firebase project
- [ ] Added iOS app to Firebase project
- [ ] Noted Firebase Project ID
- [ ] Noted Firebase Server Key (for backend)

### Android Configuration
- [ ] Downloaded `google-services.json` from Firebase Console
- [ ] Placed `google-services.json` in `android/app/` directory
- [ ] Added Google Services plugin to `android/app/build.gradle`
- [ ] Added Google Services classpath to `android/build.gradle`
- [ ] Verified Android package name matches Firebase configuration

### iOS Configuration
- [ ] Downloaded `GoogleService-Info.plist` from Firebase Console
- [ ] Added `GoogleService-Info.plist` to iOS project in Xcode
- [ ] Enabled "Push Notifications" capability in Xcode
- [ ] Enabled "Background Modes" → "Remote notifications" in Xcode
- [ ] Generated APNs key or certificate
- [ ] Uploaded APNs key/certificate to Firebase Console
- [ ] Verified iOS bundle ID matches Firebase configuration

## ✅ **Installation Checklist**

### Dependencies
- [ ] Installed Milton SDK: `npm install @milton/fcm-client-sdk`
- [ ] Installed Firebase App: `npm install @react-native-firebase/app`
- [ ] Installed Firebase Messaging: `npm install @react-native-firebase/messaging`
- [ ] Installed AsyncStorage: `npm install @react-native-async-storage/async-storage`
- [ ] Installed NetInfo: `npm install @react-native-community/netinfo`
- [ ] Installed Device Info: `npm install react-native-device-info`

### iOS Pod Installation
- [ ] Ran `cd ios && pod install && cd ..`
- [ ] Verified pods installed successfully
- [ ] No pod installation errors

### Android Build
- [ ] Ran `npx react-native run-android` successfully
- [ ] No build errors related to Firebase
- [ ] App launches without crashes

### iOS Build
- [ ] Ran `npx react-native run-ios` successfully
- [ ] No build errors related to Firebase
- [ ] App launches without crashes

## ✅ **SDK Integration Checklist**

### Basic SDK Setup
- [ ] Imported Milton SDK: `import { MiltonAsyncClient } from '@milton/fcm-client-sdk'`
- [ ] Created client instance with required parameters
- [ ] SDK initializes without errors
- [ ] FCM token is generated successfully

### Test Integration
```javascript
// Test this code to verify setup
import { MiltonAsyncClient } from '@milton/fcm-client-sdk';

const testSDKSetup = async () => {
  try {
    const client = new MiltonAsyncClient({
      baseUrl: 'https://api.milton.com', // Replace with your API URL
      apiKey: 'your-api-key-here'        // Replace with your API key
    });
    
    console.log('✅ SDK initialized successfully');
    console.log('✅ Client ID:', client.getClientId());
    
    // Test a simple request
    const response = await client.submitUserMessage({
      orgId: 123,
      userId: 456,
      question: "Test message"
    }, {
      onProgress: (status) => console.log('Progress:', status.status),
      onComplete: (result) => console.log('✅ Request completed:', result),
      onError: (error) => console.error('❌ Request failed:', error)
    });
    
    console.log('✅ Request submitted:', response.request_id);
    
  } catch (error) {
    console.error('❌ SDK setup failed:', error.message);
    // Check the error message for specific setup issues
  }
};

// Run the test
testSDKSetup();
```

### Verification Steps
- [ ] SDK initializes without throwing errors
- [ ] FCM token is logged in console
- [ ] Client ID is generated and logged
- [ ] Test request submits successfully
- [ ] No Firebase-related errors in logs

## ✅ **Push Notification Testing**

### Permission Testing
- [ ] App requests notification permissions on first launch
- [ ] User grants notification permissions
- [ ] FCM token is generated after permission grant
- [ ] Token is logged in console

### Firebase Console Testing
- [ ] Go to Firebase Console → Cloud Messaging
- [ ] Click "Send your first message"
- [ ] Enter test title and message
- [ ] Click "Send test message"
- [ ] Paste FCM token from console
- [ ] Click "Test"
- [ ] Notification received on device

### Background Testing
- [ ] Submit a request through SDK
- [ ] Put app in background
- [ ] Request completes and push notification received
- [ ] App opens when notification tapped
- [ ] Request result is properly handled

## ✅ **Production Checklist**

### Security
- [ ] API keys are stored securely (environment variables)
- [ ] Firebase configuration files are not committed to version control
- [ ] Production Firebase project is separate from development
- [ ] APNs production certificates/keys are configured

### Performance
- [ ] Push notifications are working reliably
- [ ] Polling intervals are optimized for your use case
- [ ] Battery optimization is enabled
- [ ] Offline queue is configured appropriately

### Monitoring
- [ ] Error logging is implemented
- [ ] FCM token refresh is handled
- [ ] Network connectivity changes are handled
- [ ] App state changes are handled properly

## 🚨 **Common Issues & Solutions**

### "FCM Token not generated"
**Solution:** Check Firebase configuration files are properly added and permissions are granted.

### "Firebase not initialized"
**Solution:** Ensure `@react-native-firebase/app` is installed and native configuration is complete.

### "Push notifications not received"
**Solution:** Verify APNs certificates/keys are uploaded to Firebase and device permissions are granted.

### "SDK initialization fails"
**Solution:** Check all dependencies are installed and Firebase configuration is complete.

### "Build errors on Android"
**Solution:** Verify `google-services.json` is in correct location and Google Services plugin is applied.

### "Build errors on iOS"
**Solution:** Verify `GoogleService-Info.plist` is added to Xcode project and pods are installed.

## 📞 **Support**

If you encounter issues after completing this checklist:

1. **Check Error Messages:** SDK provides detailed error messages for common issues
2. **Review Logs:** Check console logs for Firebase and SDK initialization messages
3. **Verify Configuration:** Double-check all configuration files and settings
4. **Test Incrementally:** Test each component (Firebase, SDK, Push notifications) separately

## 📚 **Additional Resources**

- [Complete Firebase Setup Guide](FIREBASE_SETUP.md)
- [SDK API Reference](API_REFERENCE.md)
- [Configuration Guide](CONFIGURATION_GUIDE.md)
- [Troubleshooting Guide](TROUBLESHOOTING.md)
- [React Native Firebase Documentation](https://rnfirebase.io/)

---

**Remember:** FCM is mandatory for the Milton SDK. The SDK is designed specifically to handle Milton's async APIs efficiently using push notifications for background processing.