HOW TO SEND FIREBASE PUSH NOTIFICATIONS TO FLUTTER APP
You like what you Read then Vote!
0 / 5 4.86 21

Your page rank:

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 {
  @override
  _FirebaseNotificationState createState() => _FirebaseNotificationState();
}

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

  @override
  void initState() {
    super.initState();
    initNotifications();
  }

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

  @override
  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 {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Code Snippets',
      theme: new ThemeData(primarySwatch: Colors.red),
      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.

Recent Articles

Related Stories

Stay on op - Ge the daily news in your inbox