3

Ich habe eine Daten wie folgt aus:senden mehrere Feuerbasis Cloud Messaging auf einer Funktionen

notification 
    |----event01 
      |---- token : "gdTh21dG705ysFA91..." 
      |---- timestamp : 1513335600000 
      |---- name : "Name Event A" 
      |---- etc 
    |----event02 
      |---- token : "dG7058J1L8I:APA91..." 
      |---- timestamp : 1513335600000 
      |---- name : "Name Event B" 
      |---- etc 
    |----event03 
      |---- token : "dG7058J1L8I:APA91..." 
      |---- timestamp : 1513355000000 
      |---- name : "Name Event C" 
      |---- etc 

Ich muss mit token FCM an Benutzer senden, wenn timestamp gekommen ist, wird es mehr als 1 Ereignis mit dem gleichen timestamp aber verschiedene name, so kann ich nicht einfach die Nachricht mit Array von Token senden.

Ich versuche, die Nachricht so zu senden, aber wenn es mehr als 1 Ereignis mit demselben Zeitstempel gibt, wird nur die erste Nachricht gesendet, ohne Fehler.

, wie ich alle Nachrichten mit einer Funktion senden kann, kann das Ereignis mit dem gleichen Zeitstempel seine 2,3,4 ... oder 100.er

// Runs Promises in a pool that limits their concurrency. 
const promisePool = require('es6-promise-pool'); 
const PromisePool = promisePool.PromisePool; 

// Maximum concurrent message sending. 
const MAX_CONCURRENT = 3; 

/** 
* Send notification to user based on timestamp 
* Triggered when /variable/notification node updated 
* The node updated by C# service when the event is starting 
*/ 
exports.sendStartNotification = functions.database.ref('/variables/notification').onUpdate(event => { 
    const epoch = event.data.val(); 

    return admin.database().ref('/notification').orderByChild('timestamp').equalTo(epoch).once('value').then(snapshot => { 
     // Use a pool so that we send maximum `MAX_CONCURRENT` notification in parallel. 
     const promisePool = new PromisePool(() => { 
      snapshot.forEach(childSnapshot => { 

       let notif = childSnapshot.val(); 
       if (notif.token !== null && notif.token !== undefined && notif.token !== '') { 
        let payload = { 
         data: { 
          key: childSnapshot.key, 
          title: `Event ${notif.name} started`, 
          body: `Please check-in` 
         } 
        }; 

        // Send the message 
        return admin.messaging().sendToDevice(notif.token, payload).catch(error => { 
         console.log("Sending failed:", error); 
        }); 
       } 
      }); 
     }, MAX_CONCURRENT); 

     promisePool.start().then(() => { 
      console.log(`Sending success ${epoch}`); 
     }); 
    }); 
}); 

Antwort

0

Sie sind nicht ein Versprechen am Ende der Rückkehr Ihr Code, was bedeutet, dass er jederzeit beendet werden kann (oder länger als nötig weiterläuft).

return promisePool.start(); 

ich nie versprechen Pools verwendet haben, so würde auf jeden Fall überlegen eine regelmäßige Promise.all() zu sehen, ob das einen Unterschied macht:

var promises = []; 
snapshot.forEach(childSnapshot => { 

    let notif = childSnapshot.val(); 
    if (notif.token !== null && notif.token !== undefined && notif.token !== '') { 
     let payload = { 
      data: { 
       key: childSnapshot.key, 
       title: `Event ${notif.name} started`, 
       body: `Please check-in` 
      } 
     }; 

     // Send the message 
     promises.push(admin.messaging().sendToDevice(notif.token, payload)); 
    } 
}); 
return Promise.all(promises); 
+0

Vielen Dank für Ihre Antwort war eigentlich, dass mein erster Versuch 'Rückkehr promisePool.start(); 'Ich denke, das ist es, was die Funktion nach dem Senden der ersten Nachricht zurückgibt. Ich frage mich, ob es egal ist, ob ich 100 Nachrichten mit Promise.all sende. – SIRS

Verwandte Themen