【发布时间】:2018-05-12 12:07:43
【问题描述】:
我的 firebase 数据库中有两个对象:
语言环境:
"39A81620-80EB-411B-80E2-C482824B7EF5" : {
"Abilitato" : true,
"Cap" : "00193",
"Città" : "Roma",
"ColoreFont" : "Bianco",
"ColorePagina" : "Nero",
"Descrizione" : "",
"EmailLocale" : "test@gmail.com",
"Font" : "Deco Future Black",
"ImmagineCopertina" : "test_image",
"Indirizzo" : "Via Dei Cosmati 3",
"Latitudine" : 41.9053562,
"Longitudine" : 12.4732003,
"Nascosto" : false,
"Nome" : "GUS",
"PaginaFacebook" :"",
"Proprietario" : "Eoh5yGzaPxWtZRiq7HAZL5WRu592",
"Regione" : "Lazio",
"SitoWeb" : "",
"Telefono" : "06 8692 9033"
}
和乌腾特:
"DDyGMUkbWjf8ucEQxNaxvI1wWPS2" : {
"Cellulare" : "3300000000",
"Citta" : "Roma",
"Cognome" : "Zollo",
"Compleanno" : 864597600,
"ConversioniRimaste" : 2,
"FBLiked" : false,
"FBShared" : false,
"ImmagineProfilo" : "https://firebasestorage.googleapis.com/v0/b/hangover-e0428.appspot.com/o/Utenti%2FDDyGMUkbWjf8ucEQxNaxvI1wWPS2%2FFoto%2FImmagineProfilo%2FImmagineProfilo.jpg?alt=media&token=5e31ef8f-7ab5-4bee-aa48-8ee0687a23f1",
"LikeTotali" : 0,
"Nome" : "Davide",
"NuoviLike" : 0,
"Privacy" : false,
"PuntiHangover" : 0,
"Regione" : "Lazio",
"Sesso" : "Maschio",
"isPR" : false,
"token" : "eiL9ca2vXw8:APA91bFK4LHawfdqm_z0Ok0gRl-wHGaVhVjqNhjUXQtIJDqwqEAOKbJRUG1q8DkoviCBV1k4rYLlqmlCXaWiZQDBemJKH4rTb9sACawLs8D_7GE_TexmwHspYc8GsWxRAkPrjT3NbsUN"
}
每当区域设置从隐藏变为可见时,“Nascosto”属性从 true 变为 false 时,我想通知用户区域设置如此开放,所以我有这个云功能:
//Rileva quando un locale passa da nascosto a visisbile e manda una notifica a tutti gli utenti della regione
exports.riAperturaLocale = functions.database.ref("Locali/{IDLocale}/Nascosto").onWrite(event =>{
let stato_apertura = event.data.toJSON(); // valore di apertura o chiusura
let ID_Locale = event.params.IDLocale;
let nascosto = Boolean(stato_apertura);
if(!nascosto) // il locale sta aprendo
{
console.log("Il Locale sta aprendo...")
let locale = admin.database().ref("Locali").child(ID_Locale).once('value');
return locale.then(snap =>{
let dati_locale = snap.val();
let regione = dati_locale["Regione"];
ottieniUtentiRegioneLocale(dati_locale,regione);
})
}else{
console.log("Il Locale sta chiudendo...:");
}
return 0;
})
//Ottiene un elenco di utenti nella regione del locale passato come parametro
function ottieniUtentiRegioneLocale(Locale,RegioneLocale){
console.log("Notifico utenti per apertura locale...");
let tutti_utenti = admin.database().ref("Utenti").once('value');
return tutti_utenti.then(snap =>{
console.log("Leggo utenti");
var da_notificare = []// contiene gli Utenti da notificare
snap.forEach((child) => {
console.log("confronto utenti...");
let IDUtente = String(child.key); // ID dell'Utente corrente
let ValoriUtente = child.toJSON(); //Valori dell'utente che si sta scansionando
let regione_user = ValoriUtente["Regione"];
if(regione_user == RegioneLocale){
da_notificare.push(IDUtente);
}
})
if(da_notificare.length != 0){
da_notificare.forEach((user) => {
let img = Locale["ImmagineCopertina"];
// creo la notifica
let testo = Locale["Nome"] + " " + "di" + " " + Locale["Città"] + " ha aperto, "
+ "entra a scoprire tutti gli Eventi!";
notificaUtente(user,"Un nuovo Locale ha aperto!",testo,img,"notifica");
})
}else{
console.log("Non ci sono utenti da notificare...");
}
})
}
//Invia una notifica all'Utente passato come parametro
function notificaUtente(IDUtente,titolo,testo,URL,tipo){
console.log("mando notifica a: " + IDUtente);
let TokenDispositivo = admin.database().ref("Utenti").child(IDUtente).child("token").once('value');
return TokenDispositivo.then(snap =>{
let token = String(snap.val());
// Notification details.
const payload = {
notification: {
title: titolo,
body: testo,
sound: 'default',
badge:"1"
},
data:{"tipo":tipo,
"url":URL,
"testo":testo,
"titolo":titolo,
"mittente":IDUtente}
};
admin.messaging().sendToDevice(token, payload).then(response => {
const error = response.error;
if (error){
console.log("Errore notifica" + error);
}
})
})
}
请注意,notificaUtente() 对其他函数工作正常;每当调用这些函数时,它们应该扫描所有具有与 Locale 的“Regione”相同的“Regione”属性但在执行代码时它不会运行通过“console.log("Leggo utenti");”的用户。一行代码,这里是firebase控制台输出:
为什么?我是 node.js 的新手,我需要一点帮助,谢谢。
【问题讨论】:
标签: node.js firebase firebase-realtime-database google-cloud-functions