nachofest
nachofest

Reputation: 63

Firebase Cloud Function not Publishing

I am trying to write a cloud function which gets executed every time a sensor reading in the collection "sensor-readings" gets created:

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


      exports.createNotification = functions.firestore
          .document('sensor-readings/{sensorId}')
          .onCreate((snap, context) => {

            const payload = {
              notification: {
                title: 'New news',
                body: "Body Test"
                }
            };

            // perform desired operations ...
          return  admin.messaging().sendToTopic("topic",payload);
          });

The cloud function gets executed everytime a sensor reading is created but when I try to use gcloud pubsub subscriptions pull --auto-ack MySub to test the outcome of the function, there is no message being published to the topic.

Any ideas? Thanks

Upvotes: 0

Views: 58

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

This code is working with a product called Firebase Cloud Messaging, which is meant for sending message to web and mobile applications:

admin.messaging().sendToTopic("topic",payload);

But your command line is working with Google Cloud Pubsub, which is a completely different product:

gcloud pubsub subscriptions pull --auto-ack MySub

You can't use FCM to send messages to a pubsub topic. Again, they are completely different. And you can't use gcloud to see what's happening with FCM messages. You would need to do that in your web or mobile app.

If you want to send a messages to a pubsub topic, you should use the Google Cloud SDK for that, not the Firebase Admin SDK.

Upvotes: 1

Related Questions