如何在firebase数据库数据更改中使用cloudfunctions发送Firebase通知? [英] How can I send Firebase notifications using cloudfunctions on firebase database datachange?

本文介绍了如何在firebase数据库数据更改中使用cloudfunctions发送Firebase通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果Firebase数据库值被更改,我想创建一个云函数,然后发送Firebase通知?下面是我的Firebase数据库快照...就像我要发送liveurl更改通知一样

I'm trying to create a cloud function if firebase database value is changed and then I send a firebase notification? below is my firebase database snapshot...it's like I want to send notification on liveurl change

livestream
    |_ 
      liveurl: "https://www.youtube.com/watch?v=Z8agqyGIaD8"

推荐答案

这是Cloud Functions和Cloud Messaging用例的主要示例.实际上,如何使用Cloud Functions可以解决类似的情况? 在发生有趣的事情时通知用户部分的文档:

This is a prime example of a use case for Cloud Functions and Cloud Messaging. In fact, a similar scenario is covered in the What can I do with Cloud Functions? documentation under the Notify users when something interesting happens section:

  1. 该函数触发对存储跟随者的实时数据库路径的写操作.
  2. 该函数编写一条消息,以通过FCM发送.
  3. FCM将通知消息发送到用户的设备.
  1. The function triggers on writes to the Realtime Database path where followers are stored.
  2. The function composes a message to send via FCM.
  3. FCM sends the notification message to the user's device.

在GitHub上还有一个 FCM通知快速入门示例为此.

There is also a FCM Notifications quickstart sample available on GitHub for this.

作为一个简单的示例,您可以编写一个Cloud Function来执行此操作,例如:

As a quick example, you could write a Cloud Function to do this, something like:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.liveUrlChange = functions.database.ref('/livestream/liveurl').onWrite((event) => {
    // Exit if data is deleted.
    if (!change.after.exists()) return null;

    // Grab the current value of what was written to the Realtime Database.
    const value = change.after.val();
    console.log('The liveurl value is now', value);

    // Build the messaging notification, sending to the 'all' topic.
    var message = {
        notification: {
            title: 'Database change',
            body: 'The liveurl value has changed to ' + value
        },
        topic: 'liveurl_changed'
    };

    // Send the message.
    return admin.messaging().send(message)
        .then((message) => {
            console.log('Successfully sent message:', message);
        })
        .catch((error) => {
            console.error('Error sending message:', error);
        });
});

这是云函数方法,将对/livestream/liveurl的更改做出反应节点并使用Cloud Messaging将发送消息到主题(即liveurl_changed).

This is a Cloud Functions method that will react to changes to the /livestream/liveurl node in your database and use Cloud Messaging to send a message to a topic (namely liveurl_changed).

要使其正常工作,您需要:

In order for this to work, you'll need to:

  1. 将此功能部署到Cloud Functions
  2. 在应用程序中设置云消息
  3. 将客户端应用订阅到liveurl_changed主题
  1. Deploy this function to Cloud Functions
  2. Setup Cloud Messaging in your app
  3. Subscribe the client app to the liveurl_changed topic

这篇关于如何在firebase数据库数据更改中使用cloudfunctions发送Firebase通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆