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
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.
Related
I had in mind to create a cloud function that let a user read some of the user infos of another user under certaine conditions.
For example:
const user1 = ??? // user1 is the current user
const user1Data = await firestore().collection('Users').doc('user1.uid').get()
const user2 = ??? // user2 is the user whith user2.uid == user1Data.partnerUid
const user2Data = await firestore().collection('Users').doc('user2.uid').get()
if (user1Data.partnerEmail == user2.email && user1Data.partnerEmail == user2.email) {
// ...
// the endpoint deliver some of the user2 data to user1.
// ...
}
I have seen the documentation of Cloud functions:
https://firebase.google.com/docs/reference/functions/providers_auth_
I have seen that with the admin API we can call getUser:
admin.auth().getUser(uid)
The difference between functions.auth() and admin.auth() is not clear for me. Can we call admin within cloud functions ?
The difference between functions.auth() and admin.auth() is not clear for me.
When you import functions from firebase-functions, all that gets you is an SDK used for building the definition of functions for deployment. It doesn't do anything else. You can't access user data using functions.
When you import admin from firebase-admin, that gives you access to the Firebase Admin SDK that can actually manage user data in Firebase Authentication. You will want to use this to look up and modify users as needed, and it works just fine when running code in Cloud Functions.
The difference between functions.auth() and admin.auth() is not clear for me. Can we call admin within cloud functions ?
Basically functions.auth(), will let you trigger Cloud Functions in response to the creation and deletion of Firebase user accounts. For example, you could send a welcome email to a user who has just created an account in your app:
exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
// ...
});
functions.auth() is from the cloud function package:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
Using the above package you can preform firestore, database or auth triggers that will run in response to creating data in the database or creating a new user...
// The Firebase Admin SDK to access Cloud Firestore.
const admin = require('firebase-admin');
admin.initializeApp();
The firebase admin sdk is used to access the database from privileged environments example inside cloud functions.
Check the following links:
https://firebase.google.com/docs/functions/use-cases
https://firebase.google.com/docs/functions/auth-events
https://firebase.google.com/docs/admin/setup
https://firebase.google.com/docs/functions/auth-events
I tried to do my first project on Google Cloud Functions.
I tried the code provided by google :
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
// Take the text parameter passed to this HTTP endpoint and insert it into
// Cloud Firestore under the path /messages/:documentId/original
exports.addMessage = functions.https.onRequest(async (req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into Cloud Firestore using the Firebase Admin SDK.
const writeResult = await admin
.firestore()
.collection("messages")
.add({ original: original });
// Send back a message that we've succesfully written the message
res.json({ result: `Message with ID: ${writeResult.id} added.` });
});
Everything is working fine as expect.
But, I have to visit the url returned by the command:
$ firebase deploy --only functions
It returns me a url that i must visit to run the function.
My question is: for Database Triggers, do I have to open the url through the browser to execute the functions?
For Database Triggers, do I have to open the url through the browser
to execute the functions?
No, for database triggers (Firestore or the Realtime Database), which are background triggers, you don't need to call an URL: the Cloud Function is triggered accordingly to the handler used to define the Function (e.g. onCreate(), onUpdate(), etc. for Firestore or onWrite(), onCreate(), etc. for the Realtime Database).
There is actually no URL available for background triggered Cloud Functions.
I'm in the final stages of development for my new mobile app, but can't seem to find a way to allow Google Play Real-Time Developer Notifications to communicate to a firebase cloud function via the recommended Google Pub/Sub method.
The expected payload flow should go as follows:
User Purchases Subscription via Play Store > Play Store sends R-T Dev Notification to Pub/Sub > Pub/Sub sends message across to firebase cloud function > Cloud function runs with payload.
I currently have an Apple Developer webhook set-up in a similar way, which webhooks a receipt payload to an iOS cloud function I have setup.
During the pub/sub setup phase, the Pub/Sub page asks to verify the cloud function URL, which I cannot do as I am not the true webmaster of cloud function domain, hence halting me about halfway down the 'Add real-time developer notification' docs that google supplies.
Is there a way to get the RT notification to either a Pub/Sub cloud function or a HTTPS cloud function bypassing the google Pub/Sub and going directly to the webhook or another way to complete the flow above?
The ultimate aim is to provide a way to ensure the purchase made is actually a valid purchase, and not a forged request made by someone intercepting the client > server webhook and submitting one of their own accord.
After creating the new topic you DO NOT have to create manually a Pub/Sub subscription as explained in the documentation.
To make it work with firebase you have to deploy a new cloud function like this:
exports.YOUR_FUNCTION_NAME = functions.pubsub.topic('YOUR_TOPIC_NAME').onPublish((message) => {
// Decode the PubSub Message body.
const messageBody = message.data ? Buffer.from(message.data, 'base64').toString() : null;
// Print the message in the logs.
console.log(`Hello ${messageBody || 'World'}!`);
return null;
});
Please note that you need to replace YOUR_FUNCTION_NAME and YOUR_TOPIC_NAME.
When the deploy of this function finish, you will finally find the function in the subscription list.
At this point, you can edit the params of the automatically created subscription, and you will find the url endpoint already filled with an internal url.
Here you can find an example: how to setup a PubSub triggered Cloud Function using the Firebase SDK for Cloud Functions.
When connecting dialogflow with hangouts chat I am getting normal responses set in dialogflow. But messages from the webhook running on google actions sdk is not returning messages.
const functions = require('firebase-functions');
const app = dialogflow({debug: true});
const {dialogflow, BasicCard, Permission, Suggestions,} = require('actions-on-google');
app.intent('id', (conv, {name}) =>
{
conv.close(`People here are always available.So,you can call ${name} anytime`)
});
This works with google assistant and webchat. Only with Google chat the response is empty.
The actions-on-google library only works with the Google Assistant.
For other integrations, including Facebook, Slack, and Hangouts Chat, you need to either use the dialogflow-fulfillment library, or respond with JSON directly.
I want to send an HTTP Request from Android to a cloud function, post some values, then input these values into real time database.
index.js
const functions = require('firebase-functions');
exports.testPost = functions.https.onRequest((req, res) => {
console.log(req.body);
});
How can I accomplish this?
I see three steps in here:
Calling a Cloud Function from Android.
This is the same as calling any other HTTP URL from Android. See
Calling a Cloud Function from Android through Firebase
Parsing parameters from the call in your Cloud Function
A HTTP triggered Cloud Function is really just an Express handler. So parsing the post works the same as for other Express handlers. The Firebase documentation for HTTP functions has some examples and links to other documentation.
Posting to the database from a Cloud Functions
All the samples in the functions-samples repo include the Firebase Admin SDK. This SDK allows you to access many Firebase features, such as the database, from within your function. But there's also an example in this repo.