Skip to content

nirmal0707/Firebase-Push-Notification-Flutter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Firebase Push Notification

Pushing notifications using cloud functions to Flutter app when app is launched, resumed, running and closed.

Getting Started

  1. Add firebase_messaging plugin in pubspec.yaml.

  2. Follow the instructions correctly from the README firebase_messaging in pub.dev page.

  3. Copy the code from lib directory.

  4. Now you have to select a folder in which you can write your cloud function before deploying.

  5. Now Structure your Firebase project like shown below

  • Add Listeners by giving device token that is printed out in console when app opens.

Listeners

  • Add Notifications with title and body of notification

Notifications

  1. Open the terminal(default shell) from the folder and run the follwing commands-
  • $ npm install -g firebase-tools
  • $ firebase login
  • $ firebase init

  • Select 'Functions: Configure and deploy Cloud Functions' using space bar.

  • Select your project or create one

  • Select Javascript

  • Confirm ESLint so that you can see bugs before deploying and is the fastest technique

  • Confirm to install npm with dependencies

  • Add the following line in functions/index.js

admin.initializeApp(functions.config().firebase);

var notificationDoc;

exports.notificationTrigger = functions.firestore.document(
    'Notifications/{autoId}'
).onCreate(async (snapshot, context) => {
    notificationDoc = snapshot.data();

    admin.firestore().collection('Listeners').get()
        .then((snapshots) => {
            var tokens = [];
            if (snapshots.empty) {
                return console.log('No Devices');
            }
            else {
                for (var token of snapshots.docs) {
                    tokens.push(token.data().deviceToken);
                }
                var payload = {
                    notification: {
                        title: "Title: " + notificationDoc.title,
                        body: "Body: " + notificationDoc.body,
                        sound: "default"
                    },
                    data: {
                        click_action: "FLUTTER_NOTIFICATION_CLICK",
                        sendername: "Nirmal",
                        message: notificationDoc.body
                    }
                };

                return admin.messaging().sendToDevice(tokens, payload)
            }
        })
        .then((response) => {
            return console.log("Succesfully pushed");
        })
        .catch((error) => {
            console.log(error);
        })
})
  • Make sure the node version is minimum 10 in functions/package.json or change it as below
"engines": {
    "node": "10"
  }
  • $ firebase deploy --only functions
  1. In your Firestore add a new document in Notifications with body and title.

  2. Now try sending notifications.

About

Pushing notifications using cloud functions to Flutter app when app is launched, resumed, running and closed.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages