发送Firebase云消息时如何触发云功能?

时间:2019-03-27 12:18:56

标签: firebase firebase-cloud-messaging google-cloud-functions

一旦用户创建新的Firebase云消息(FCM),我就试图触发Firebase云功能。

documentation page确切描述了要做什么。

这是我在服务器端的代码:

侧面说明: subscribeToTopic 可以使用一个令牌或一组令牌。

admin.messaging().subscribeToTopic(fcmToken, "/topics/news")
.then(res => {
    console.log('res: ', res);
})
.catch(error =>{
    console.log('error: ', error);
});

假设fcmToken是一个有效的令牌,是从我的设备(客户端)创建的,我已对其进行了仔细检查。

现在,要测试它是否有效:我使用firebase提供的通知网络控制台创建一个新通知,然后选择标题和正文,然后选择图像中的主题。 choosing a topic for the message

问题是根本没有触发云功能。

1 个答案:

答案 0 :(得分:2)

根据文档(https://firebase.google.com/docs/functions/),Firebase Cloud Messaging(FCM)没有Cloud Function后台触发器。

您可以在发送消息的同时并行地在Firebase数据库(Firestore或实时数据库)中编写内容,然后对该数据库使用onCreate()触发器。

您可以在https://firebase.google.com/docs/functions/firestore-events处找到Cloud Firestore触发器的文档,在https://firebase.google.com/docs/functions/database-events处可以找到实时数据库的文档