CloudFunctions: Request is sending twice - firebase

I have an issue and I need help since I'm learning.
I have a flutter app that saves on Firebase/Firestore data, when a user requests a friendship I add it on both sender and target user, changing the IDs of sendTo and sentBy.
My problem is that CloudFunctions detect well that 2 collections from different users have been changed and notify me 2x (target user). So code is fine but should only notify once/target user
I'm using FCM to send local notifications.
exports.sendRequestNotification = functions.firestore
.document('users/{userId}/requests/{requestId}')
.onCreate((snap, context) => {
const docReq = snap.data()
/*console.log(docReq)*/
const sentBy = docReq.sentBy
const sentTo = docReq.sentTo
const contentRequest = docReq.code
if(contentRequest !== null){
// Get push token user to (receive)
admin
.firestore()
.collection('users')
.where('userId', '==', sentTo)
.get()
.then(querySnapshot => {
querySnapshot.forEach(userTo => {
/*console.log(`Found request user to: ${userTo.data().userId}`)*/
if (userTo.data().pushToken) {
// Get info user from (sent)
admin
.firestore()
.collection('users')
.where('userId', '==', sentBy)
.get()
.then(querySnapshot2 => {
querySnapshot2.forEach(userFrom => {
/*console.log(`Found request user from: ${userFrom.data().userId}`)*/
const payload = {
notification: {
title: `${userFrom.data().nickname}`,
body: contentRequest,
badge: '1',
sound: 'default'
}
}
// Let push to the target device
admin
.messaging()
.sendToDevice(userTo.data().pushToken, payload)
.then(response => {
/*console.log('Successfully sent request:', response)*/
})
.catch(error => {
console.log('Error sending request:', error)
})
})
})
} else {
console.log('User request or token not found')
}
})
})
return null
}
})

It is not very clear from your code why it would send the notification twice (since you check that userTo.data().userId !== sentBy). But what is sure is that you are not returning a Promise that resolves when all the asynchronous operations (get() and sendToDevice()) are completed.
I would suggest you watch the official Video Series (https://firebase.google.com/docs/functions/video-series/) which explain very well this point about returning Promises for background functions (in particular the ones titled "Learn JavaScript Promises").
In particular, you will see in the videos that if you don't return a Promise, the Cloud Function may terminate before asynchronous operations are completed, potentially resulting in some inconsistent (not logical) results .
So, you should give a try with the following adapted code, which returns the promises chain:
exports.sendRequestNotification = functions.firestore
.document('users/{userId}/requests/{requestId}')
.onCreate((snap, context) => {
const db = admin.firestore();
const docReq = snap.data();
/*console.log(docReq)*/
const sentBy = docReq.sentBy;
const sentTo = docReq.sentTo;
// Get push token user to (receive)
return db.collection('users')
.where('userId', '==', sentTo)
.get()
.then(querySnapshot => {
//We know there is only one document (i.e. one user with this Id), so lets use the docs property
//See https://firebase.google.com/docs/reference/js/firebase.firestore.QuerySnapshot.html#docs
const userTo = querySnapshot.docs[0];
if (userTo.data().pushToken && userTo.data().userId !== sentBy) {
// Get info user from (sent)
return db.collection('users')
.where('userId', '==', sentBy)
.get();
} else {
console.log('User request or token not found')
throw new Error('User request or token not found');
}
})
.then(querySnapshot => {
const userFrom = querySnapshot.docs[0];
const payload = {
notification: {
title: `${userFrom.data().nickname}`,
body: `requestNotify`,
badge: '1',
sound: 'default'
}
}
return admin
.messaging()
.sendToDevice(userTo.data().pushToken, payload);
})
.catch(error => {
console.log('Error:', error);
return false;
})
})

Related

Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array. (Firebase cloud functions)

I am making a chat application where I am trying to send push notifications to when one user sends a message to the other user.
I am using firebase cloud functions with JavaScript and in the firebase functions log I can see that it's able to get the user who sends the message and the user who receives the message. But for some reason It's showing me that the token is empty.
Even though I have a token and I am able to print it in the log(The screenshot of firebase cloud functions log where you can see that token is being printed and I have partially marked over it).
For more reference I am also attaching the screenshot of my two collections in firebase -
All the users collection where the push token is being saved.(The structure is users>uid>{user_info}).
The chat collection screenshot where you can see the structure of chats.(The structure here is chatroom>chatid>chats>{chat_documents}).
My index.js file in functions folder for firebase is below-
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()
exports.sendNotification = functions.firestore
.document('chatroom/{chatId}/chats/{message}')
.onCreate((snap) => {
console.log('----------------start function--------------------')
const doc = snap.data()
console.log(doc)
const idFrom = doc.idFrom
const idTo = doc.idTo
const contentMessage = doc.mesaage
console.log(`Starting to push token`)
// Get push token user to (receive)
admin
.firestore()
.collection('users')
.where('uid', '==', idTo)
.get()
.then(querySnapshot => {
querySnapshot.forEach(userTo => {
console.log(`Found user to: ${userTo.data().name}`)
console.log(`Found user to: ${userTo.data().pushtoken}`)
try {
// Get info of the user who is sending the message
admin
.firestore()
.collection('users')
.where('uid', '==', idFrom)
.get()
.then(querySnapshot2 => {
querySnapshot2.forEach(userFrom => {
console.log(`Found user from: ${userFrom.data().name}`)
const payload = {
notification: {
title: `"${userFrom.data().name}"`,
body: contentMessage,
badge: '1',
sound: 'default'
}
}
// Lets push to the target device
admin
.messaging()
.sendToDevice(payload,userTo.data().pushtoken)
.then(response => {
console.log('Successfully sent message:', response)
})
.catch(error => {
console.log('Error sending message:', error)
})
})
})
} catch(e){
console.log('Can not find pushToken target user')
}
})
})
return null
})
What might be the problem here as I am not able to figure it out? Your help would be really appreciated. Also kindly let me know if you want any more information regarding the same.

Better code to push notification: firestore - cloud function

I made a cloud function (using google :() that sends push notifications when adding a document in firestore, but I have the error that you see in the image and the notifications do not arrive but I do not understand what may be wrong in my code, can someone help me?
mi code:
exports.cambiaColeccion = functions.firestore
.document('sendMessage/{docId}')
.onCreate((snap, context) => {
const nuevoMensaje= snap.data();
console.log('id', nuevoMensaje);
console.log('titulo', nuevoMensaje.titulo)
enviaMensage();
});
async function enviaMensage() {
console.log('en enviaMensaje');
const payload ={
notification: {
title: "Titulo del mensaje",
body: "Texto del mensaje ... ",
sound: 'default',
badge: '1',
}
// Get the list of device tokens.
const allTokens = await admin.firestore().collection('FCMTokens').get();
const tokens = [];
allTokens.forEach((tokenDoc) => {
tokens.push(tokenDoc.id);
});
if (tokens.length > 0) {
// Send notifications to all tokens.
const response = await admin.messaging().sendToDevice(tokens, payload);
//await cleanupTokens(response, tokens);
console.log('Notifications have been sent and tokens cleaned up.');
}
return true
}
// Cleans up the tokens that are no longer valid.
function cleanupTokens(response, tokens) {
// For each notification we check if there was an error.
const tokensDelete = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
const deleteTask = admin.firestore().collection('FCMTokens').doc(tokens[index]).delete();
tokensDelete.push(deleteTask);
}
}
});
return Promise.all(tokensDelete);
}
You need to return a Promise in your Cloud Function, in such a way the Cloud Functions instance running your function does not shut down before your function successfully reaches its terminating condition or state. See the doc for more details.
In your case you are not returning anything in the Cloud Function itself. Since async functions always return a Promise, you can adapt your code as follows:
exports.cambiaColeccion = functions.firestore
.document('sendMessage/{docId}')
.onCreate((snap, context) => {
const nuevoMensaje = snap.data();
console.log('id', nuevoMensaje);
console.log('titulo', nuevoMensaje.titulo)
return enviaMensage();
});
async function enviaMensage() {
console.log('en enviaMensaje');
const payload = {
notification: {
title: "Titulo del mensaje",
body: "Texto del mensaje ... ",
sound: 'default',
badge: '1',
}
}
// Get the list of device tokens.
const allTokens = await admin.firestore().collection('FCMTokens').get();
if (allTokens.size > 0) { // allTokens is a QuerySnapshot
const tokens = allTokens.docs.map(tokenDoc => tokenDoc.id);
await admin.messaging().sendToDevice(tokens, payload);
}
}
Note that it would be good to add some try/catch block in order to capture and debug potential errors.
Update following your comment on the cleanupTokens function.
Your cleanupTokens function is correct. It is asynchronous since it returns a Promise (returned by Promise.all(tokensDelete);).
The way you call it should work correctly:
const allTokens = await admin.firestore().collection('FCMTokens').get();
if (allTokens.size > 0) { // allTokens is a QuerySnapshot
const tokens = allTokens.docs.map(tokenDoc => tokenDoc.id);
const response = await admin.messaging().sendToDevice(tokens, payload);
await cleanupTokens(response, tokens);
}

“user Does not exists” Firebase

I started this tutorial (https://www.freecodecamp.org/news/react-native-firebase-tutorial/) on Firebase and React Native. Everything is working well overall.
But I have this error: “User does not exist anymore.” for the Login.
However, users are well rooted in Firebase.
const onLoginPress = () => {
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then((response) => {
const uid = response.user.uid
const usersRef = firebase.firestore().collection('users')
usersRef
.doc(uid)
.get()
.then(firestoreDocument => {
if (!firestoreDocument.exists) {
alert("User does not exist anymore.")
return;
}
const user = firestoreDocument.data()
navigation.navigate('Home', {user})
})
.catch(error => {
alert(error)
});
})
.catch(error => {
alert(error)
})
}
With
const usersRef = firebase.firestore().collection('users')
usersRef
.doc(uid)
.get()
.then(firestoreDocument => {
if (!firestoreDocument.exists) {
alert("User does not exist anymore.")
return;
}
const user = firestoreDocument.data()
navigation.navigate('Home', {user})
})
you actually query the user document with the id corresponding to the user's uid in the users collection.
This document is normally created by the onRegisterPress() function in the tutorial. If you get the "User does not exist anymore." message, it means that the user document is not present in the collection.
So you need to check why this is the case: the onRegisterPress() function was not called? The doc was deleted? There are security rules that prevent creating the document? etc...

How do I resolve FCM Cloud function npm errors?

error Expected catch() or return promise/catch-or-return
23:13 error Each then() should return a value or throw promise/always-return
28:13 error Expected catch() or return promise/catch-or-return
28:13 warning Avoid nesting promises promise/no-nesting
33:21 error Each then() should return a value or throw promise/always-return
45:19 warning Avoid nesting promises promise/no-nesting
45 :19 warning Avoid nesting promises promise/no-nesting
48:27 error Each then() should return a value or throw '
Above code is error content (terminal Mac Os), since terminal deployed, this error code shows up.
I follow firebase contents and read some time, but I don't know.
Firebase justify:
"Send messages to specific devices
To send a message to one specific device, forward the device's registration token as shown below. For more information about registration tokens, see About Client Settings by Platform."
I am using FCM push message, but only deployed, this code show
this code is showing push message to specified person's uid.
Some error distress me, and this is my code(exactly this is other person code (flutter FCM demo)
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()
exports.sendNotification = functions.firestore
.document('messages/{groupId1}/{groupId2}/{message}')
.onCreate((snap, context) => {
console.log('----------------start function--------------------')
const doc = snap.data()
console.log(doc)
const idFrom = doc.idFrom
const idTo = doc.idTo
const contentMessage = doc.content
// Get push token user to (receive)
admin
.firestore()
.collection('user')
.where('uid', '==', idTo)
.get()
.then(querySnapshot => {
querySnapshot.forEach(userTo => {
console.log(`Found user to: ${userTo.data().nickname}`)
if (userTo.data().pushToken && userTo.data().chattingWith !== idFrom) {
// Get info user from (sent)
admin
.firestore()
.collection('users')
.where('id', '==', idFrom)
.get()
.then(querySnapshot2 => {
querySnapshot2.forEach(userFrom => {
console.log(`Found user from: ${userFrom.data().nickname}`)
const payload = {
notification: {
title: `You have a message from "${userFrom.data().nickname}"`,
body: contentMessage,
badge: '1',
sound: 'default'
}
}
// Let push to the target device
admin
.messaging()
.sendToDevice(userTo.data().pushToken, payload)
.then(response => {
console.log('Successfully sent message:', response)
})
.catch(error => {
console.log('Error sending message:', error)
})
})
})
} else {
console.log('Can not find pushToken target user')
}
})
})
return null
})

Firestore data not changed in Vuex

I have this Vuex action that should change a users status on logout but since false values aren't save in Firestore, it isn't working
logoutUser({commit, state}) {
let user = firebase.auth().currentUser;
db
.collection('users')
.where('user_id', '==', user.uid)
.get()
.then(snapshot => {
snapshot.forEach((doc) => {
db
.collection('users')
.doc(doc.id)
.update({
is_online: false
});
localStorage.setItem('firebase_user', null);
firebase
.auth()
.signOut()
.then(() => {
commit('SET_AUTHENTICATED', false);
commit('SET_CURRENT_USER', null);
});
});
});
}
I am just starting with Firestore so I would appreciate any assistance or recommendations ... Thanks!!!
The following should work (not tested). You need to chain the promises returned by the asynchronous methods (update() and signOut()).
I make the assumption that there is only one user with user.uid in the users collection, therefore I use snapshot.docs[0] to get the unique user document, instead of using forEach()
logoutUser({ commit, state }) {
const user = firebase.auth().currentUser;
db.collection('users')
.where('user_id', '==', user.uid)
.get()
.then(snapshot => {
const userDocSnapshot = snapshot.docs[0];
return userDocSnapshot.ref.update({
is_online: false
});
})
.then(() => {
return firebase.auth().signOut();
})
.then(() => {
localStorage.setItem('firebase_user', null);
commit('SET_AUTHENTICATED', false);
commit('SET_CURRENT_USER', null);
});
}

Resources