Notifications about new articles, price changes, product offers are expanding interactivity scope of mobile applications.

A notification is a message that pops up on the user’s device. Notifications can be triggered locally by an open application, or they can be “pushed” from the server to the user even when the app is not running. They allow your users to opt-in to timely updates and allow you to effectively re-engage users with customized content.¹

Push notifications are great marketing tools because they help to increase user-to-app interactivity level.

In this article we are going to explore how we can send push notifications from NodeJS backend application to Flutter Android application, using FCM messages. Device tokens will be stored in MySQL database and in the NodeJS, we are going to validate input.

At the bottom of this article, you can see sample applications and feel free to check them out.

Creating NodeJS backend App

In this section, we are going to build NodeJS backend application that will be responsible for storing push device tokens and sending push notifications.

Backend app will have the following dependencies in ‘package.json’:

  "dependencies": {
    "axios": "^0.19.2",
    "body-parser": "^1.19.0",
    "cookie-parser": "^1.4.5",
    "dotenv": "^8.2.0",
    "ejs": "^3.1.3",
    "express": "^4.17.1",
    "express-session": "^1.17.1",
    "express-validator": "^6.6.1",
    "mysql2": "^2.1.0",
    "node-gcm": "^1.0.3",
    "sequelize": "^6.3.4"
  },
  "devDependencies": {
    "sequelize-cli": "^6.2.0"
  },

and by executing:

npm install

we are going to install these dependencies.

Next point is to create ‘App.js’, which will be the starting point of our NodeJS app:

const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql2');
const cookieParser = require('cookie-parser');
const path = require('path');
const session = require('express-session');
const app = express();

const { getHomePage, sendMessage, sendToken } = require('./routes/index');

const db = mysql.createConnection({
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PASS,
    database: process.env.DB_NAME
});
db.connect((err) => {
    if (err) {
        throw err;
    }
    console.log('connected to database');
});
global.db = db;

app.set('port', process.env.PORT);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true  }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
    secret: 'keyboard cat',
    saveUninitialized: true,
    resave: true
  }));

app.get('/', getHomePage);

app.post('/send-message', sendMessage);
app.post('/send-token', sendToken);

app.listen(process.env.PORT, () => {
    console.log(`server running on port: ${process.env.PORT}`);
});

#nodejs #flutter #push #push-notification #android

Sending Push Notifications from NodeJS backend App to Flutter Android App
71.75 GEEK