FCM

Firebase Cloud Messaging

FCM Provider from expresso-provider

firebase admin to provider FCM

Using commonjs

const { FCM } = require('expresso-provider')

Using ES6

import { FCM } from 'expresso-provider'

Initialize FCM

// config/fcm.ts

import { FCM } from 'expresso-provider'
import * as admin from 'firebase-admin'
import path from 'path'

const serviceAccountPath = path.resolve(
  `${process.cwd()}/public/gcp/fcm_serviceAccount.json`
)

export const fcmService = new FCM({
  options: { credential: admin.credential.cert(serviceAccountPath) },
})

fcmService.initialize()

FCM Send To Device

import { fcmService } from 'config/fcm'

const payload = {
    'text': 'any value'
}

const dataFcm = await fcmService.sendToDevice({
    appName: 'My App',
    title: 'Your App Notif',
    deviceTokens: ['device_token_1', 'device_token_2'],
    message: 'Welcome to my App',
    type: 'Notif',
    data: JSON.stringify(payload),
})

console.log({ dataFcm })

Last updated