[英]Integrate firebase notificaiton in nest js
I am trying to create fcm API using nest js which I am using in a flutter.我正在尝试使用我在颤振中使用的 nest js 创建 fcm API。 I am new to Nest js and I have implemented the code using this .
我是 Nest js 的新手,我已经使用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 main.ts 文件
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创建一个模块来初始化 Firebase 应用 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然后以这种方式使用它 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之后,您可以像在代码中一样在任何您想要的服务中使用它作为 admin.messaging().sendtodevice
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.