Flutter
Last updated
Last updated
You will need to integrate your Push.Express account with Firebase.
Go to Firebase Console and create a new project (or use existing one)
You can use one project for all your apps.
Open Project Settings -> General
Create new Flutter app or just download google-services.json
from existing app
Install and run the FlutterFire CLI
dart pub global activate flutterfire_cli
Then, at the root of your Flutter project directory, run this command:
dart pub global activate flutterfire_cli
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.
Go to Firebase Console and create a new project (or use existing one)
You can use one project for all your apps.
Open Project Settings
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)
Go to your Push.Express account
Open existing App settings or create a new App
Switch type application Firebase
Paste private-key.json
file to Firebase Admin SDK private key textbox
Import package in your main.dart file
import 'package:push_express_lib/push_express_lib.dart';
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.
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,
);
}
}
Call initFirebase() in the initState() method in the main.dart file.
@override
void initState() {
super.initState();
// call to init
initFirebase();
}
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);
}
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());
}
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),
),
);
}
}