1
votes

I'm trying to trigger a firebase cloud functions once a user creates a new firebase cloud message(FCM).

This documentation page describes exactly what to do.

Here's my code on the server side :

Side Note: subscribeToTopic can take one token or an array of tokens.

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

Assume fcmToken is a valid token, created from my device(client-side), i double checked it.

Now, to test if it works: I create a new notification using the notification web console provided by firebase, and after choosing title and body, i get to choose the topic like in the image. choosing a topic for the message

The problem is that the cloud function is not getting triggered at all.

1
I think you might be mistaken what subscribeToTopic does. It basically forces some client apps, identified by their tokens, to be subscribed to the given topic. It doesn't arrange for any future code to be run on your server side. - Doug Stevenson

1 Answers

2
votes

According to the documentation (https://firebase.google.com/docs/functions/) there isn't a Cloud Function background trigger for Firebase Cloud Messaging (FCM).

What you could is to write in the Firebase database (Firestore or Realtime Database) in parallel to the message sending, and then use a onCreate() trigger for this database.

You will find the doc for the Cloud Firestore triggers here https://firebase.google.com/docs/functions/firestore-events and for the Realtime Database ones here https://firebase.google.com/docs/functions/database-events