Send push notifications in Firebase using function

2019-08-02 03:38发布

I have successfully send notifications using Firebase Cloud Messaging (FCM) triggered by a Firebase function:

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));
}

As you can see I am sending the notifications to a topic called 'all'.

In the firebase console you can send notifications to an audience that you can create. In my case I have created different audiences based on user properties from the analytics module.

Is it possible to also send notifications via Firebase Functions to audiences?

1条回答
2楼-- · 2019-08-02 04:31

There is no Analytics API to retrieve the users that fall into a specific analytics audience. Nor is there an FCM API to send a notification to such an audience.

If you want to send notifications to audiences, you'll have to create your own way of defining these audiences. The most direct way is to connect Google Analytics for Firebase to BigQuery, and then define the audience on the analytics events you receive there.

查看更多
登录 后发表回答