Push.Express Documentation
  • Push.Express Documentation
  • Quckstart
    • How to use
    • Analytics
    • Demo apps
  • SDK
    • PWA
    • Kotlin
    • Swift
    • Unity
    • Flutter
    • Onesignal
  • API
    • Applications
    • App Instances (Devices)
    • Pushes
    • One-time sending in future
    • Smart weekly sending
    • Instant sending
  • SDK API
    • SDK API specs
Powered by GitBook
On this page
  • PushExpress -- SDK Flutter**
  • Setup Firebase
  • Setup Push.Express
  • Add sdk in your application
  1. SDK

Flutter

PreviousUnityNextOnesignal

Last updated 28 days ago

PushExpress -- SDK Flutter**

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

  1. Follow

Setup Firebase

  1. Go to and create a new project (or use existing one)

    You can use one project for all your apps.

  2. Open Project Settings -> General

  3. Create new Flutter app or just download google-services.json from existing app

  4. Install and run the FlutterFire CLI

dart pub global activate flutterfire_cli
  1. Then, at the root of your Flutter project directory, run this command:

dart pub global activate flutterfire_cli
  1. Add your app in firebase project

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.

Setup Push.Express

Get Firebase Private key

  1. You can use one project for all your apps.

  2. Open Project Settings

  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)

Integrate your Push.Express App with Firebase

  1. Open existing App settings or create a new App

  2. Switch type application Flutter

  3. Paste private-key.json file to Firebase Admin SDK private key textbox

Add sdk in your application

  1. Import package in your main.dart file

   import 'package:push_express_lib/push_express_lib.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,
        );
    }
}
  1. Call initFirebase() in the initState() method in the main.dart file.

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

    // call to init
    initFirebase();
}
  1. Add a background push notification handler to Firebase Cloud Messaging in the main.dart file.

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);
}
  1. Register the firebaseMessagingBackgroundHandler background push notification handler in the main() function of the main.dart file.

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

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

  runApp(const MyApp());
}
  1. Example of the main.dart file

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),
      ),
    );
  }
}

Go to and create a new project (or use existing one)

Go to your account

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 .

Firebase Console
Push.Express
Applications
Firebase Console
Firebase Cloud Messaging integration guide
Create application in Push.Express
Add sdk in your application