# Flutter

### PushExpress -- SDK Flutter\*\*

You will need to integrate your Push.Express account with Firebase.

1. Follow [Firebase Cloud Messaging integration guide](#setup-firebase)
2. [Create application in Push.Express](#setup-pushexpress)
3. [Add sdk in your application](#add-sdk-in-your-application)

### Setup Firebase

1. Go to [Firebase Console](https://console.firebase.google.com) and create a new project (or use existing one)

   You can use one project for all your apps.
2. Open Project Settings -> General

   <div align="center"><img src="https://4196027459-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEXhSGyb0mYOQ2RZvkDaH%2Fuploads%2Fgit-blob-a8252e7021a7e46720b40975dcec06834a016718%2Ffcm-project-settings.png?alt=media" alt="" width="800"></div>
3. Create new Flutter app or just download `google-services.json` from existing app
4. Install and run the FlutterFire CLI

```bash
dart pub global activate flutterfire_cli
```

5. Then, at the root of your Flutter project directory, run this command:

```bash
dart pub global activate flutterfire_cli
```

6. Add your app in firebase project

```bash
flutterfire configure --project=<Firebase Project ID>
```

This automatically registers your per-platform apps with Firebase and adds a lib/firebase\_options.dart configuration file to your Flutter project.\
\
\
\ <br>

### Setup Push.Express

#### Get Firebase Private key

1. Go to [Firebase Console](https://console.firebase.google.com) and create a new project (or use existing one)

   You can use one project for all your apps.
2. Open Project Settings

   <div align="center"><img src="https://4196027459-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEXhSGyb0mYOQ2RZvkDaH%2Fuploads%2Fgit-blob-a8252e7021a7e46720b40975dcec06834a016718%2Ffcm-project-settings.png?alt=media" alt="" width="800"></div>
3. Go to Service accounts, press `Generate new private key` and save it to file `private-key.json` (**you can use same key for all apps**)

   <div align="center"><img src="https://4196027459-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEXhSGyb0mYOQ2RZvkDaH%2Fuploads%2Fgit-blob-2a00f0365ac888f714f77eef7b0eae1cc1257077%2Ffcm-private-key-page.png?alt=media" alt="" width="800"></div>

#### Integrate your Push.Express App with Firebase

1. Go to your [Push.Express](https://push.express) account
2. Open existing *App* settings or create a new App
3. Switch type application Firebase

   <div align="center"><img src="https://4196027459-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEXhSGyb0mYOQ2RZvkDaH%2Fuploads%2Fgit-blob-4d4659530a7db5c6d52f24524f829d751c591de6%2Fpx-sdk-switch.png?alt=media" alt="" width="800"></div>
4. Paste `private-key.json` file to *Firebase Admin SDK private key* textbox

   <div align="center"><img src="https://4196027459-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEXhSGyb0mYOQ2RZvkDaH%2Fuploads%2Fgit-blob-2f39e9dc21f10066a0e59387d28f727cd6345b59%2Fpx-sdk-fcm-key.png?alt=media" alt="" width="800"></div>

\
\
\
\ <br>

### Add sdk in your application

1. Import package in your main.dart file

```dart
   import 'package:push_express_lib/push_express_lib.dart';
```

2. In main.dart add code for init Push Express sdk. Replace the default value of 12345-12 with the PULEXPRESS\_APP\_ID of your application. You can find out your PUSHEXPRESS\_APP\_ID in the [Applications](https://github.com/pushex/px-docs-public/blob/main/docs/sdk-integrations/flutter/https:/app.push.express).

```dart
   void initFirebase() async {
    // Initialize firebase app
    await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
    );

    // Request permissions for push notifications
    await FirebaseMessaging.instance.requestPermission();

    // get unique token for messaging from firebase messaging
    String? token = await FirebaseMessaging.instance.getToken();

    if (token != null) {
        // initialize package
        PushExpressManager().init(
            // your application id from https://app.push.express/
            '12345-12',
            TransportType.fcmData,
            transportToken: token,
            // set property foreground "true" if you need to get notifications when app is in foreground (IOS only)
            foreground: true,
        );
    }
}
```

3. Call initFirebase() in the initState() method in the main.dart file.

```dart
@override
void initState() {
    super.initState();

    // call to init
    initFirebase();
}
```

4. Add a background push notification handler to Firebase Cloud Messaging in the main.dart file.

```dart
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // initialize firebase app
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  // call function from the package to handle notifications,
  // and show them properly in background
  NotificationManager().handleNotification(message);
}
```

5. Register the firebaseMessagingBackgroundHandler background push notification handler in the main() function of the main.dart file.

```dart
void main() {
  // ensure widgets are initialized
  WidgetsFlutterBinding.ensureInitialized();

  // handle Firebase background messages
  // and call our handler
  FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

  runApp(const MyApp());
}
```

6. Example of the main.dart file

```dart
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test_package/firebase_options.dart';
import 'package:push_express_lib/enums/common.dart';
import 'package:push_express_lib/notification_manager.dart';
import 'package:push_express_lib/push_express_lib.dart';

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  NotificationManager().handleNotification(message);
}

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
  runApp(const MyApp());
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    initFirebase();
  }

  void initFirebase() async {
    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );

    await FirebaseMessaging.instance.requestPermission();

    String? token = await FirebaseMessaging.instance.getToken();

    if (token != null) {
      PushExpressManager().init(
        '21486-1212',
        TransportType.fcm,
        transportToken: token,
        foreground: true,
      );
    }

    FirebaseMessaging.onMessage.listen(
      NotificationManager().handleNotification,
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      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;

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

  @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,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}
```
