使用 typescript 和 if 语句的云函数
cloud function using typrescript and if statment
你好我是云函数的新手,我想问一下是否有一些为什么在一个 if 语句中得到多个主题
这是我的云功能:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
const fcm = admin.messaging();
export const sendToTopic = functions.firestore
.document("Doctor2019/{documentId}")
.onCreate(async snapshot => {
const payload: admin.messaging.MessagingPayload = {
notification: {
title: 'NEW POST!',
body: `Click here to see New Post`,
icon: 'your-icon-url',
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
};
return fcm.sendToTopic('Doctor2019', payload);
});
问题是我有多个主题我想做的是检查其他集合上文档的创建并基于此发送通知,我真的不知道该怎么做,任何帮助?
我看到您想将消息发送到 FCM 上与不同文档创建相关的不同主题。
您不能使用一个函数来实现这一点,因为该函数与特定集合上的文档创建相关联。您需要做的是为不同的集合创建不同的函数。
如果我正确理解了您的目标,您可以使用 {wildcard}
代替文档 ID 以及 代替集合 ID。然后你使用context
对象获取集合ID的值如下:
export const sendToTopic = functions.firestore
.document("{collectionId}/{documentId}")
.onCreate(async (snap, context) => { // Note the addition of context
const collectionId = context.params.collectionId;
const payload: admin.messaging.MessagingPayload = {
notification: {
title: 'NEW POST!',
body: `Click here to see New Post`,
icon: 'your-icon-url',
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
};
return fcm.sendToTopic(collectionId, payload);
});
如果您有不应触发消息的根集合,只需调整您的数据模型并使需要触发消息的集合成为特定文档的子集合。类似于:
export const sendToTopic = functions.firestore
.document("messagingTriggers/triggers/{collectionId}/{documentId}")
.onCreate(async (snap, context) => {...});
然后在 users
集合中创建任何文档都不会触发 Cloud Function。
你好我是云函数的新手,我想问一下是否有一些为什么在一个 if 语句中得到多个主题 这是我的云功能:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
const fcm = admin.messaging();
export const sendToTopic = functions.firestore
.document("Doctor2019/{documentId}")
.onCreate(async snapshot => {
const payload: admin.messaging.MessagingPayload = {
notification: {
title: 'NEW POST!',
body: `Click here to see New Post`,
icon: 'your-icon-url',
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
};
return fcm.sendToTopic('Doctor2019', payload);
});
问题是我有多个主题我想做的是检查其他集合上文档的创建并基于此发送通知,我真的不知道该怎么做,任何帮助?
我看到您想将消息发送到 FCM 上与不同文档创建相关的不同主题。
您不能使用一个函数来实现这一点,因为该函数与特定集合上的文档创建相关联。您需要做的是为不同的集合创建不同的函数。
如果我正确理解了您的目标,您可以使用 {wildcard}
代替文档 ID 以及 代替集合 ID。然后你使用context
对象获取集合ID的值如下:
export const sendToTopic = functions.firestore
.document("{collectionId}/{documentId}")
.onCreate(async (snap, context) => { // Note the addition of context
const collectionId = context.params.collectionId;
const payload: admin.messaging.MessagingPayload = {
notification: {
title: 'NEW POST!',
body: `Click here to see New Post`,
icon: 'your-icon-url',
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
};
return fcm.sendToTopic(collectionId, payload);
});
如果您有不应触发消息的根集合,只需调整您的数据模型并使需要触发消息的集合成为特定文档的子集合。类似于:
export const sendToTopic = functions.firestore
.document("messagingTriggers/triggers/{collectionId}/{documentId}")
.onCreate(async (snap, context) => {...});
然后在 users
集合中创建任何文档都不会触发 Cloud Function。