How to send Firebase Push Notifications to Flutter app

We can use Firebase cloud messaging feature to send push notifications fro our flutter apps.

First off, head over to the firebase_messaging package home, to see how to set it up and explore more.

In your pubspec.yaml file, import this:

firebase_messaging: any

Create a new Dart file, paste this code:

import 'package:flutter/material.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

class FirebaseNotification extends StatefulWidget {
  _FirebaseNotificationState createState() => _FirebaseNotificationState();

class _FirebaseNotificationState extends State<FirebaseNotification> {
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  void initState() {

  initNotifications() {
    _firebaseMessaging.configure(onMessage: (data) {
      if (data != null) {
            .showSnackBar(SnackBar(content: Text("Notification received")));

  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(title: Text('Firebase Notification Example')),
      body: Center(child: Text("Main Page")),

In main.dart:

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Code Snippets',
      theme: new ThemeData(primarySwatch:,
      home: new FirebaseNotification(),

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

50% Discounts On Projects

Do you have a custom project with a clear requirements, we are currently giving a 50% discount on all projects.

We will work with you to ensure we deliver what you need.

If you have questions or want to chat with us, you can reach us on Skype (henry.tochi)

Languages and technologies we use - Java, Kotlin, Laravel, Vue, React, Flutter, Swift and more.