Implementing In-App Notifications in NestJS with Firebase and MySQL
In-app notifications are a crucial feature for many applications, allowing developers to communicate with users and provide real-time updates. In this article, we’ll explore how to implement in-app notifications in a NestJS application using Firebase and MySQL.
Prerequisites
Before getting started, ensure you have the following:
- NestJS CLI installed globally
- A Firebase project with Cloud Messaging enabled
- MySQL installed and configured
Setting up Firebase Cloud Messaging
To begin, set up a Firebase project and enable Cloud Messaging. Follow the instructions in the Firebase documentation to create a service account and download the private key file. Rename the file to firebase-admin-sdk.json
and move it to the root of your application.
Install the firebase-admin
package using the following command:
bash
npm install firebase-admin
Creating NestJS User and Notification Resources
In a system that uses push notifications, user resources refer to the information stored and managed for each individual user. This may include data such as device type, push notification preferences, and contact information. Notification resources, on the other hand, refer to the logic and functionality used to enable, disable, and send push notifications.
Generate new NestJS resources using the following code snippets:
“`typescript
// user.entity.ts
import { Entity, Column, PrimaryGeneratedColumn } from ‘typeorm’;
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
email: string;
@Column()
username: string;
@Column()
status: string;
}
“`
“`typescript
// notification.entity.ts
import { Entity, Column, PrimaryGeneratedColumn } from ‘typeorm’;
@Entity()
export class Notification {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
body: string;
@Column()
status: string;
}
“`
Setting up MySQL
Before working on the notification resource, set up the MySQL database to store notification data. Install the mysql2
and typeorm
packages using the following commands:
bash
npm install mysql2 typeorm
Update the app.module.ts
file to include the TypeORM configuration:
“`typescript
// app.module.ts
import { Module } from ‘@nestjs/common’;
import { TypeOrmModule } from ‘@nestjs/typeorm’;
@Module({
imports: [
TypeOrmModule.forRoot({
type: ‘mysql’,
host: ‘localhost’,
port: 3306,
username: ‘root’,
password: ‘password’,
database: ‘notifications’,
entities: [User, Notification],
synchronize: true,
}),
],
})
export class AppModule {}
“`
Configuring User Services
Inject the NotificationService
into the UsersService
to enable, retrieve, and send notifications:
“`typescript
// users.service.ts
import { Injectable } from ‘@nestjs/common’;
import { InjectRepository } from ‘@nestjs/typeorm’;
import { Repository } from ‘typeorm’;
import { User } from ‘./user.entity’;
import { NotificationService } from ‘./notification.service’;
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User)
private readonly userRepository: Repository
private readonly notificationService: NotificationService,
) {}
// …
}
“`
Configuring User Controllers
Create endpoints for creating, updating, and retrieving user data, as well as enabling and disabling push notifications:
“`typescript
// users.controller.ts
import { Controller, Post, Put, Get, Body, Param } from ‘@nestjs/common’;
import { UsersService } from ‘./users.service’;
@Controller(‘users’)
export class UsersController {
constructor(private readonly usersService: UsersService) {}
// …
}
“`
Testing Controller Endpoints
Use a tool like Thunder Client to test the user controller endpoints. Create a user, update their profile, and enable push notifications to receive notifications.
In conclusion, implementing in-app notifications in a NestJS application using Firebase and MySQL requires setting up Firebase Cloud Messaging, creating user and notification resources, configuring MySQL, and injecting the notification service into the user service. By following these steps, developers can provide real-time updates to users