Firebase Push Notifications (FCM) in a Flutter app for both Android and iOS, follow these steps.
1. Create Firebase Project
Open Firebase Console
Click Create Project
Enter project name
Disable Google Analytics if not needed
Create project
2. Install Firebase CLI Windows
(Installing Node.js automatically installs the npm command tools.)
npm install -g firebase-tools
Verify: firebase --version
Login: firebase login
3. Install FlutterFire CLI
dart pub global activate flutterfire_cli
Verify: flutterfire --version
(Add Dart pub cache to PATH if command not found.)
4. Add Firebase to Flutter Project
Inside project folder: flutterfire configure
Select:
Firebase Project
Android
iOS
This generates: lib/firebase_options.dart
5. Add Packages
flutter pub add firebase_core
flutter pub add firebase_messaging
6. Initialize Firebase
main.dart
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const MyApp());
}
Message Handler
----------------------
1 Add dependency
dependencies:
firebase_core: ^latest
firebase_messaging: ^latest
flutter_local_notifications: ^latest
2 NotificationService.dart
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
class NotificationService {
final FirebaseMessaging _firebaseMessaging =
FirebaseMessaging.instance;
final FlutterLocalNotificationsPlugin
flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> initialize() async {
// Local notification initialization
const AndroidInitializationSettings androidSettings =
AndroidInitializationSettings('@mipmap/ic_launcher');
const InitializationSettings initSettings =
InitializationSettings(
android: androidSettings,
);
await flutterLocalNotificationsPlugin.initialize(
initSettings,
);
// Create notification channel
const AndroidNotificationChannel channel =
AndroidNotificationChannel(
'high_importance_channel',
'High Importance Notifications',
importance: Importance.max,
);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
// Request permission
await _firebaseMessaging.requestPermission();
// FCM Token
String? token = await _firebaseMessaging.getToken();
print("FCM Token: $token");
// Foreground messages
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print("Foreground Notification");
print(message.notification?.title);
print(message.notification?.body);
// Show popup notification
flutterLocalNotificationsPlugin.show(
message.hashCode,
message.notification?.title ?? '',
message.notification?.body ?? '',
const NotificationDetails(
android: AndroidNotificationDetails(
'high_importance_channel',
'High Importance Notifications',
importance: Importance.max,
priority: Priority.high,
),
),
);
});
// Notification clicked
FirebaseMessaging.onMessageOpenedApp.listen((message) {
print("Notification clicked");
});
// App launched from notification
RemoteMessage? initialMessage =
await _firebaseMessaging.getInitialMessage();
if (initialMessage != null) {
print("App opened from terminated state");
}
}
static Future<void> backgroundHandler(
RemoteMessage message) async {
print("Background Notification");
print(message.notification?.title);
print(message.notification?.body);
}
}
3 main.dart
await NotificationService().initialize();
4 AndroidManifest.xml
Make sure you have:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
and inside <application>:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
ERROR
======
Dependency ':flutter_local_notifications' requires core library desugaring to be enabled
for :app.
Android/app/build.gradle.kts
android {
namespace = "com.example.firebase5"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
isCoreLibraryDesugaringEnabled = true
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
}
}
dependencies {
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.1.4")
}