Buen dia, estoy intentando crear una cloud function utilizando como BD firestore, me funciona, pero tarda como unos 5 minutos en llegar la notificacion y no se si sea algun problema en la cloud function o algo estoy codificando algo mal, me hace ruido el mensaje: funtion reurned undefined ... no se si estoy provocando algun problema :(, en el log de firebase tengo estos mensajes:
Este es mi codigo en la cloud function:
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() {
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
// return "OK";
}
// 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);
}