首页 > 解决方案 > 在 Firebase 中使用函数向(分析)受众发送推送通知

问题描述

我已使用 Firebase 函数触发的 Firebase 云消息传递 (FCM) 成功发送通知:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const fs = require('fs');

admin.initializeApp();


exports.testNotif = functions.https.onRequest((request, response) => {

    const mySecretNumber = getRandomInt(147)

    var payload = {
      notification: {
        title: `You got a new Message`,
        body: `My highest break ${mySecretNumber}`,
        badge: mySecretNumber.toString(),
        sound: `notif1.aiff`,
      }
    };
 const ackString = `All done ${mySecretNumber}`;
 console.log(ackString)
 response.send(ackString);
 //send to all topic
 admin.messaging().sendToTopic(`all`, payload)
});


function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}

如您所见,我将通知发送到一个名为“全部”的主题。

在 Firebase 控制台中,您可以向可以创建的受众发送通知。就我而言,我根据分析模块中的用户属性创建了不同的受众。

是否也可以通过 Firebase Functions 向受众发送通知?

标签: firebasefirebase-cloud-messaging

解决方案


没有分析 API 可以检索属于特定分析受众的用户。也没有 FCM API 可以向此类受众发送通知。

如果您想向受众发送通知,则必须创建自己的方式来定义这些受众。最直接的方法是将 Google Analytics for Firebase 连接到 BigQuery,然后根据您在那里收到的分析事件定义受众。

另请参阅:如何通过 HTTP 向观众发送 Firebase 通知


推荐阅读