Push Notification Using Firebase

Introduction

 
In this article we are going to learn how to integrate Firebase Push Notification in Flutter applications. Firebase provides a cloud messaging service, also known as Firebase Cloud Messaging (FCM). Previously this service was known as Google Cloud Messaging (GCM). So don’t get confused with the term, basically FCM is the upgraded version of GCM.
 
So let’s start integrating FCM in Flutter. The plugin which we require is firebase_messaging. We need to setup a Firebase Project as well.
 
Prerequisite (Firebase Project Setup)
 
To set up the Firebase project, please check the previous part to learn about how to setup Firebase for Flutter. Please note that you need to follow only the first 2 steps.
 

Steps

 
Step 1
 
Follow the prerequisites and create a new Flutter project and setup a Firebase project.
 
Step 2
 
Add firebase_messaging dependency in the Flutter project. To add dependency, open pubspec.yaml, which is located at the root of the project. Check the below code snippet for more understanding.
 
dependencies:
 flutter:
sdk: flutter
 cupertino_icons: ^0.1.2
 firebase_messaging: ^5.1.1
 
Step 3
 
Import Firebase messaging plugin in main.dart file.
 
import'package:firebase_messaging/firebase_messaging.dart';
 
Step 4
 
If you want to be notified in your app (via onResume and onLaunch, see below) when the user clicks on a notification in the system tray include the following intent-filter within the <activity> tag of your android/app/src/main/AndroidManifest.xml,
  1. <intent-filter>  
  2.    <action android:name="FLUTTER_NOTIFICATION_CLICK" />  
  3.    <category android:name="android.intent.category.DEFAULT" />  
  4. </intent-filter>  
Step 5
 
Following is the instantiation and implementation of Firebase push notification in Flutter. We have declared _firebaseMessagin object and implemented its configuration.
  1. final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();  
  2. final List<Notification> notifications = [];  
  3. @override  
  4. void initState() {  
  5.   super.initState();  
  6.   _firebaseMessaging.configure(  
  7.     onMessage: (Map<String, dynamic> notification) async {  
  8.       setState(() {  
  9.         notifications.add(  
  10.           Notification(  
  11.             title: notification["notification"]["title"],  
  12.             body: notification["notification"]["body"],  
  13.             color: Colors.red,  
  14.           ),  
  15.         );  
  16.       });  
  17.     },  
  18.     onLaunch: (Map<String, dynamic> notification) async {  
  19.       setState(() {  
  20.         notifications.add(  
  21.           Notification(  
  22.             title: notification["notification"]["title"],  
  23.             body: notification["notification"]["body"],  
  24.             color: Colors.green,  
  25.           ),  
  26.         );  
  27.       });  
  28.     },  
  29.     onResume: (Map<String, dynamic> notification) async {  
  30.       setState(() {  
  31.         notifications.add(  
  32.           Notification(  
  33.             title: notification["notification"]["title"],  
  34.             body: notification["notification"]["body"],  
  35.             color: Colors.blue,  
  36.           ),  
  37.         );  
  38.       });  
  39.     },  
  40.   );  
Firebase Messaging configuration includes events like onMessage, onResume, onLaunch.
  1. onMessage
    If the app is open and notification is delivered then this event will be fired.
  2. onResume
    If app is in the background and notification is  delivered and you open the app via notification tap then this event will be fired.
  3. onLaunch
    If app is closed and notification is delivered and you open the app via notification tap then this event will be fired.
Step 6
 
Request notification permission.
 
_firebaseMessaging.requestNotificationPermissions();
 
Step 7
 
Send Firebase push notification from Firebase project. Go to Firebase project and click on Cloud Messaging option in left menu under Grow Option. You can create your first Notification from there.
 
NOTE
You need to pass click_action and FLUTTER_NOTIFICATION_CLICK in additional option custom data.
 
Flutter Push Notification Using Firebase
 
Try sending notification while: 1. Your app is pen;  2. Your app is in the background; and 3. Your app is closed. That’s all, you are finished with the Firebase push notification.
 
I have attached the full source code and git repository for this article.
 
 

Conclusion

 
In this article we have learned how to integrate firebase push notifications in Flutter apps and broadcast push notifications from Firebase. You can also broadcast push notifications programmatically from the server side.
Author
Parth Patel
259 7k 1.6m
Next » Firebase Firestore CRUD Operations