Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting id of the current user from Cloud Functions and Firebase

I am using google cloud functions to register push notifications through firebase. In my app, i have a notifications reference that changes for a current user whenever they get a new follower or like, etc. As of right now I am able to send the notification to the phone whenever that whole reference child changes

For example, if any single post is liked, then it will send a notification. What I need to do is observe the current user to only send the notification that single person.

Here is my JavaScript file

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendPushNotification = functions.database.ref('/notification/{id}').onWrite(event => {
const payload = {
notification: {
    title: 'New message arrived',
    body: 'come check it',
    badge: '1',
    sound: 'default',
}
  };

  return admin.database().ref('fcmToken').once('value').then(allToken => {
    if (allToken.val()) {
        const token = Object.keys(allToken.val());
        return admin.messaging().sendToDevice(token, payload).then(response => {

        });
    }
  });

});

I would like to replace this line:

functions.database.ref('/notification/{id}').onWrite(event => {

With this:

functions.database.ref('/notification/{id}').(The current user ID).onWrite(event => {

How do I get the current users id?

like image 433
user8426652 Avatar asked Aug 21 '17 00:08

user8426652


People also ask

How do I find my current username for Firebase?

Get a user's profile // The user object has basic properties such as display name, email, etc. // you have one. Use User.getToken() instead. // The user object has basic properties such as display name, email, etc.

What does firebase auth () currentUser return?

auth(). currentUser during firebase initialization will return null.

How do you check if a user is authenticated in firebase?

You can easily detect if the user is logged or not by executing: var user = firebase. auth().


2 Answers

You seem very new to JavaScript (calling it JSON is sort-of a give-away for that). Cloud Functions for Firebase is not the best way to learn JavaScript. I recommend first reading the Firebase documentation for Web developers and/or taking the Firebase codelab for Web developer. They cover many basic JavaScript, Web and Firebase interactions. After those you'll be much better equipped to write code for Cloud Functions too.

Now back to your question: there is no concept of a "current user" in Cloud Functions. Your JavaScript code runs on a server, and all users can trigger the code by writing to the database.

You can figure out what user triggered the function, but that too isn't what you want here. The user who triggered the notification is not the one who needs to receive the message. What you want instead is to read the user who is the target of the notification.

One way to do this is to read it from the database path that triggered the function. If you keep the notifications per user in the database like this:

user_notifications
  $uid
    notification1: ...
    notification2: ...

You can trigger the Cloud Function like this:

exports.sendPushNotification = functions.database.ref('/user_notification/{uid}/{id}').onWrite(event => {

And then in the code of that function, get the UID of the user with:

var uid = event.params.uid;
like image 152
Frank van Puffelen Avatar answered Oct 19 '22 15:10

Frank van Puffelen


For Swift 3.0 - 4.0

You can do this:

import Firebase
import FirebaseAuth


class YourClass {

let user = Auth.auth().currentUser
let userID = user.uid

// user userID anywhere 

}
like image 36
J Arango Avatar answered Oct 19 '22 13:10

J Arango