Firestore发票新增时触发Dialogflow Intent推送通知并展示详情咨询
实现方案:Firestore发票新增触发Dialogflow推送通知并跳转详情
Got it, let's break down exactly how to implement this workflow step by step—since you already have push permissions and Firestore set up, we can focus on the trigger and notification flow.
1. 前置确认工作
先确保这些基础条件已经到位:
- 用户已授权推送权限,且**
users/{userId}文档中存储了FCM token**(字段名比如fcmToken) - 发票集合
invoices/{invoiceId}结构明确,包含userId(关联所属用户)、amount、dueDate、detailsUrl(或其他详情所需字段)等核心信息
2. 创建Firestore新增触发器(Cloud Functions)
我们需要用Cloud Functions监听invoices集合的新文档插入事件,这是整个流程的启动开关。
代码示例(Node.js)
const functions = require("firebase-functions"); const admin = require("firebase-admin"); const { SessionsClient } = require("@google-cloud/dialogflow-cx"); admin.initializeApp(); const dialogflowClient = new SessionsClient({ projectId: "你的项目ID" }); exports.sendInvoiceNotification = functions.firestore .document("invoices/{invoiceId}") .onCreate(async (snap, context) => { const invoiceData = snap.data(); const userId = invoiceData.userId; // 1. 获取用户的FCM Token和Dialogflow会话ID const userDoc = await admin.firestore().doc(`users/${userId}`).get(); if (!userDoc.exists) { functions.logger.error(`未找到用户 ${userId}`); return; } const { fcmToken, dialogflowSessionId } = userDoc.data(); if (!fcmToken || !dialogflowSessionId) { functions.logger.error(`用户 ${userId} 缺少FCM Token或Dialogflow会话ID`); return; } // 2. 调用Dialogflow触发指定Intent const sessionPath = dialogflowClient.projectLocationAgentSessionPath( "你的项目ID", "你的地域", // 比如us-central1 "你的Dialogflow Agent ID", dialogflowSessionId ); const request = { session: sessionPath, queryInput: { text: { text: "invoice_new_trigger", // 与Dialogflow中Intent的触发短语对应 }, languageCode: "zh-CN", // 根据你的用户语言调整 }, // 传递发票参数给Dialogflow queryParams: { parameters: { invoiceId: context.params.invoiceId, amount: invoiceData.amount, dueDate: invoiceData.dueDate, }, }, }; try { const [response] = await dialogflowClient.detectIntent(request); functions.logger.info(`Dialogflow Intent已触发: ${response.queryResult.intent.displayName}`); } catch (error) { functions.logger.error("触发Dialogflow Intent失败:", error); } });
3. 在Dialogflow中配置推送通知Intent
接下来要在Dialogflow(CX/ES均可,这里以CX为例)创建专门处理发票通知的Intent:
- 创建Intent:命名为
invoice_new_notification,设置触发短语为invoice_new_trigger(和上面Cloud Function中传递的文本一致) - 配置参数:添加
invoiceId、amount、dueDate等参数,确保能接收Cloud Function传递的数据 - 设置推送响应:启用Dialogflow的推送通知功能,绑定FCM,通知内容可以设置为:
您有一张新的月度发票:金额*{{amount}}*,到期日{{dueDate}}。点击查看详情。
- 配置深层链接:在推送通知的动作中,设置点击后的跳转目标——App端跳转到发票详情页面并携带
invoiceId参数;Web端跳转到详情URL(比如/invoices/{{invoiceId}})
4. 客户端处理推送点击事件
当用户点击推送通知时,需要在客户端(App/Web)实现跳转逻辑:
Android端示例
在Firebase Messaging Service中处理:
override fun onMessageReceived(remoteMessage: RemoteMessage) { remoteMessage.notification?.let { notification -> val intent = Intent(this, InvoiceDetailActivity::class.java) intent.putExtra("INVOICE_ID", remoteMessage.data["invoiceId"]) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE) val notificationBuilder = NotificationCompat.Builder(this, "INVOICE_CHANNEL") .setContentTitle(notification.title) .setContentText(notification.body) .setSmallIcon(R.drawable.ic_notification) .setContentIntent(pendingIntent) .setAutoCancel(true) NotificationManagerCompat.from(this).notify(1, notificationBuilder.build()) } }
Web端示例
在Service Worker中处理:
self.addEventListener('notificationclick', (event) => { event.notification.close(); const invoiceId = event.notification.data.invoiceId; event.waitUntil( clients.openWindow(`/invoices/${invoiceId}`) ); });
5. 测试与调试
- 手动在Firestore的
invoices集合中新增测试文档,检查Cloud Functions是否触发 - 查看Dialogflow会话日志,确认Intent是否被正确调用、参数是否传递成功
- 在用户设备上验证推送通知是否收到,点击后能否正确跳转到发票详情页
内容的提问来源于stack exchange,提问作者sastra2007
相关产品推荐
相关产品推荐

