简体   繁体   中英

Integrate firebase notificaiton in nest js

I am trying to create fcm API using nest js which I am using in a flutter. I am new to Nest js and I have implemented the code using this . There is no error in my code but I don't receive the notification on my phone Below is my service file in which I have implemented the push notification

 async sendMessageToUser(registrationToken) {
     
      var message = {
        android: {
        ttl: 3600 * 1000, // 1 hour in milliseconds
  
        data: {
        title: "hiii",
        body: "hello",
       
        }
        
         },
        apns: {
        headers: {
        'apns-priority': '10'
        },
        payload: {
        "aps": {
        "alert": {
        "body": "hii",
        "title": "hello"
        },
        "sound": "dafault",
        
        }
        }
        },
        token: registrationToken
        };
      
      
    
      
      // Send a message to the device corresponding to the provided
      // registration token with the provided options.
      try{
        Logger.log("FIrst try catch");
        await admin.messaging().send(message);
        try {
          Logger.log("second try catch");
          
        } catch (error) {
          Logger.error(error);
        }
      }catch (error) {
        Logger.error(error);
      }
     
     
  }

main.ts file

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import admin, { ServiceAccount } from 'firebase-admin';

async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    app.useGlobalPipes(new ValidationPipe());
    
  
  // Set the config options
  const adminConfig: ServiceAccount = {
    "projectId":"my firebase projetId", 
    "privateKey": "private key".replace(/\\n/g, '\n'),
    "clientEmail": "firebase client email",
  };
  // Initialize the firebase admin app
  admin.initializeApp({
    credential: admin.credential.cert(adminConfig),
    databaseURL: "db url",
  });

  app.enableCors();
  await app.listen(3000);
}
bootstrap();

can someone tell me what's wrong with my code why I didn't receive the notification on my phone?

Create a Module to initialize Firebase App firebase.module.ts

import {ApiConfigService} from '@config/config.service'
import {Module} from '@nestjs/common'
import * as admin from 'firebase-admin'

@Module({})
export class FirebaseModule {
  constructor(configService: ApiConfigService) {
    admin.initializeApp({
      credential: admin.credential.cert(adminConfig),
      databaseURL: "db URL",
    })
  }
}

And then use it this way app.module.ts

@Module({
  imports: [
    FirebaseModule,
    ...
  ],
})
export class AppModule {}

After that, you are able to use it in any service that you want as admin.messaging().sendtodevice as you do on your code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM