> ## Documentation Index
> Fetch the complete documentation index at: https://docs.auxia.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Sample Flutter Client Implementation

## main.dart

```dart theme={null}
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'firebase_options.dart';

/// Background message handler for Firebase Cloud Messaging.
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized(); // Ensure Flutter is initialized

  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  await _initializeAnalytics();

  runApp(const MyApp());
}

/// Initializes Firebase Analytics and enables analytics collection.
Future<void> _initializeAnalytics() async {
  FirebaseAnalytics analytics = FirebaseAnalytics.instance;
  await analytics.setAnalyticsCollectionEnabled(true);
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  String? _fcmToken;

  @override
  void initState() {
    super.initState();
    _initializeFirebaseMessaging();
  }

  /// Initializes Firebase Messaging and retrieves the FCM token.
  Future<void> _initializeFirebaseMessaging() async {
    FirebaseMessaging messaging = FirebaseMessaging.instance;

    await messaging.requestPermission(
      alert: true,
      badge: true,
      sound: true,
      provisional: true,
    );

    // Get the initial FCM token
    String? token = await messaging.getToken();
    _updateFcmToken(token);

    // Handle token refresh
    FirebaseMessaging.instance.onTokenRefresh.listen(_updateFcmToken);
  }

  /// Updates the FCM token and prints it for debugging.
  /// TODO(): Store the updated token along with the user_id on the server.
  void _updateFcmToken(String? token) {
    setState(() => _fcmToken = token);
    debugPrint("FCM Token: $_fcmToken");
  }

  void _incrementCounter() {
    setState(() => _counter++);
  }

  /// UI code below.
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('You have pushed the button this many times:'),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            const SizedBox(height: 20),
            Text(
              _fcmToken ?? "Fetching FCM token...",
              style: Theme.of(context).textTheme.bodyMedium,
              textAlign: TextAlign.center,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}
```

## firebase\_options.dart

```dart theme={null}
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
import 'package:flutter/foundation.dart'
    show defaultTargetPlatform, TargetPlatform;

class DefaultFirebaseOptions {
  static FirebaseOptions get currentPlatform {
    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
        return android;
      default:
        throw UnsupportedError(
          'DefaultFirebaseOptions are not supported for this platform.',
        );
    }
  }

  static const FirebaseOptions android = FirebaseOptions(
    apiKey: '<TODO(): Add API Key>',
    appId: '<TODO(): Add App Id>',
    messagingSenderId: '<TODO(): Add messaging sender id>',
    projectId: 'demo',
    storageBucket: 'demo.firebasestorage.app',
  );
}
```

## .flutter-plugins (generated file)

```
Install plugins:
https://firebase.google.com/docs/analytics/get-started?platform=flutter
https://firebase.google.com/docs/cloud-messaging/flutter/client
# This is a generated file; do not edit or check into version control.
firebase_analytics=xxxxxxxxxx
firebase_analytics_web=xxxxxxxxxx
firebase_core=xxxxxxxxxx
firebase_core_web=xxxxxxxxxx
firebase_messaging=xxxxxxxxxx
firebase_messaging_web=xxxxxxxxxx
```
