Push notifications allow you to send updates and messages to your app users even when the app is not actively in use. This guide will walk you through the process of setting up push notifications for your Flutter project using Firebase Cloud Messaging (FCM).
Create a Firebase Project
Add Project
and follow the setup steps. Name your project and accept the terms.Add Firebase to Your App
Add Firebase to your Android app
.google-services.json
file and place it in the android/app
directory of your Flutter project.Add Firebase to your iOS app
.GoogleService-Info.plist
file and add it to your Xcode project by dragging it into the ios/Runner
directory in Xcode.Configure Firebase Cloud Messaging
Project Settings
-> Cloud Messaging
.For Android
android/build.gradle
and add the Google services classpath to the dependencies block:
buildscript {
dependencies {
// Add this line
classpath 'com.google.gms:google-services:4.3.15'
}
}
android/app/build.gradle
and apply the Google services plugin at the bottom of the file:
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation 'com.google.firebase:firebase-messaging:23.1.0'
}
For iOS
ios/Podfile
and ensure you have the following line:
pod 'Firebase/Messaging'
pod install
in the ios
directory to install the Firebase SDK:
cd ios
pod install
Add Dependencies
pubspec.yaml
and add the firebase_core
and firebase_messaging
dependencies:
dependencies:
flutter:
sdk: flutter
firebase_core: ^2.12.0
firebase_messaging: ^14.0.0
flutter pub get
to install the new dependencies.Initialize Firebase
Open lib/main.dart
and initialize Firebase:
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
Set Up Firebase Messaging
Create a new file lib/firebase_messaging_service.dart
and configure Firebase Messaging:
import 'package:firebase_messaging/firebase_messaging.dart';
class FirebaseMessagingService {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
FirebaseMessagingService() {
_firebaseMessaging.getInitialMessage().then((RemoteMessage? message) {
if (message != null) {
// Handle notification tap when app is launched from terminated state
}
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
// Handle foreground message
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
// Handle notification tap when app is in background
});
}
}
Handle Notifications
In lib/main.dart
, set up notification handling:
import 'firebase_messaging_service.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
FirebaseMessagingService();
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Push Notifications Example'),
),
body: Center(child: Text('Hello World')),
),
);
}
}
Send a Test Notification from Firebase Console
Cloud Messaging
.Send your first message
.Verify Notification Reception
You have successfully integrated Firebase Cloud Messaging into your Flutter project. By following these steps, you’ve set up the necessary configurations for sending and receiving push notifications on both Android and iOS platforms.
For more detailed information, refer to the Firebase Cloud Messaging documentation and the FlutterFire documentation.