1

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?

HixField
  • 3,538
  • 1
  • 28
  • 54

1 Answers1

1

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.

Also see: How to send firebase notifications to audience via HTTP

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Well that's a shame; would have been nice. Do you know if the exports.myevent = functions.analytics.event('myEvent').onLog((event) => {...} works for all sort of events or only for purchasing events? – HixField Apr 29 '18 at 22:18
  • You can only trigger Cloud Functions on conversion events. But the BigQuery export will contain all Analytics events. – Frank van Puffelen Apr 29 '18 at 22:59