0

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:

introducir la descripción de la imagen aquí

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);
}
4
  • estas utilizando pubsub? el 16 jul. 2021 a las 15:33
  • perdona mi ignorancia, ¿que es pubsub? aqui estoy utilizando cloud functions de firestore. el 16 jul. 2021 a las 20:00
  • puedes buscar pubsub publish and suscriptions de firestore el 16 jul. 2021 a las 20:16
  • gracias, pero entonces ¿ya no recomiendan utilizarlo asi directo con una cloud function y firestore? el 18 jul. 2021 a las 4:12

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.