File

src/message/service/mesage.service.ts

Index

Properties
Methods

Constructor

constructor(firebase, userService: UserService)
Parameters :
Name Type Optional
firebase No
userService UserService No

Methods

Async sendMessageToDevice
sendMessageToDevice(deviceId: string, data: Record)
Parameters :
Name Type Optional
deviceId string No
data Record<string | string> No
Returns : Promise<Record<string, >>

Properties

Private Readonly logger
Default value : new Logger(MessageService.name)
import { Inject, Injectable, Logger } from '@nestjs/common';
import { UserService } from '../../user/service/user.service';
import { DICTIONARY } from '../constant/dictionary.constant';

@Injectable()
export class MessageService {
  private readonly logger = new Logger(MessageService.name);

  constructor(
    @Inject(DICTIONARY.FIREBASE)
    private readonly firebase,
    private readonly userService: UserService
  ) { }

  async sendMessageToDevice(
    deviceId: string,
    data: Record<string, string>
  ): Promise<Record<string, unknown>> {
    const user = await this.userService.findByDeviceId(deviceId);

    if (user?.profile?.allowNotifications === false) {
      return;
    }

    const payload: Record<string, Record<string, string>> = {
      notification: {
        title: data.title,
        body: data.message,
      },
      data: {
        source: 'backend',
        type: data.type,
      },
    };

    if (data.sound) {
      payload.notification.sound = data.sound;
    }

    return this.firebase
      .messaging()
      .sendToDevice(
        deviceId,
        {
          ...payload,
        },
        {
          priority: 'high',
        }
      )
      .then((result) => {
        if (result.successCount === 1) {
          Logger.log(`Push notification has been sent: `, JSON.stringify({ deviceId, ...payload }));

          return result;
        }

        Logger.error(`Push notification has NOT been sent: `, JSON.stringify({ deviceId, ...payload }));

        throw result;
      })
      .catch((error) => {
        this.logger.error(error, JSON.stringify(payload), deviceId);
      });
  }
}

results matching ""

    No results matching ""