私はFireBeansのクラウド機能の初心者です。私はそれを使って特定のユーザに通知を送るウェブアプリケーションを開発しています。しかし、問題は、私がユーザーにメッセージを送信したい相手を決定させたいということです。私がこれを行う方法を知ったところでは、ユーザーは自分のサイトのフォームを通じて受信者の電子メールを挿入できるようになりました。私はそれをデータベースに保存して、送信者ユーザーが挿入した同じ電子メールを持つ登録済みユーザーに以前に作成した通知を送信する機能を有効にできます。Firebase用クラウド機能を使用して通知を要求した別のユーザーが挿入した電子メールを使用して通知を送信するにはどうすればよいですか?
私はデータベースが変更されているので、ユーザーが受信者の電子メールでフォームを送信するたびに関数を起動する必要があることを知っています。しかし、私は挿入された電子メールを他のすべてのユーザーの電子メールと比較し、正しいユーザーのトークンだけをキャッチして通知を送信する方法を知らない。誰もこれを行う方法を知っていますか?
これらは私のデータベースの部分から私の私はベースとして、このquestionの一部を取ったコード、およびJSONです:
機能
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendPush =
functions.database.ref('/Messages/Receivers/{pushId}').onWrite(event => {
const snapshot = event.data;
const email = snapshot.val().email;
const getAllUsersPromise = admin.database().ref('Users/').once('value');
const payload = {
notification: {
title: 'You have a notification',
body: 'You received a new message'
}
};
return getAllUsersPromise.then(result => {
const userUidSnapShot = result;
const users = Object.keys(userUidSnapShot.val());
var AllUsersFCMPromises = [];
for (var i = 0;i<userUidSnapShot.numChildren(); i++) {
const user=users[i];
console.log('getting promise of user uid=',user);
AllUsersFCMPromises[i]= admin.database().ref(`/Users/${user}/email`).equalTo(email).once('value').then(token => {
var token = admin.database().ref(`/Users/${user}/token`).once('value');
return token;
});
}
return Promise.all(AllUsersFCMPromises).then(results => {
var tokens = [];
for(var i in results){
var usersTokenSnapShot=results[i];
console.log('For user = ',i);
if(usersTokenSnapShot.exists()){
if (usersTokenSnapShot.hasChildren()) {
const t= Object.keys(usersTokenSnapShot.val());
tokens = tokens.concat(t);
console.log('token[s] of user = ',t);
}
else{
}
}
}
console.log('final tokens = ',tokens," notification= ",payload);
return admin.messaging().sendToDevice(tokens, payload).then(response => {
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to uid=', tokens[index], error);
if (error.code === 'messaging/invalid-registration-token' || error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(usersTokenSnapShot.ref.child(tokens[index]).remove());
}
}
else{
console.log("notification sent",result);
}
});
});
});
});
});
JSON構造
{
"Messages" : {
"Receivers" : {
"-Ko-Gc8Ch58uYKGIT_Ze" : {
"email" : "[email protected]"
},
}
},
"Users" : {
"1rwdq0O9Iqdo1JUNauwmlC9HXfY2" : {
"apartamento" : "12",
"bloco" : "L",
"celular" : "148162491784",
"email" : "[email protected]",
"nome" : "josé",
"sobrenome" : "josé",
"telefone" : "418947912497",
"token" : "een1HZ0ZzP0:APA91bHaY06dT68W3jtlC3xykcnnC7nS3zaNiKBYOayBq-wuZsbv1DMFL8zE6oghieYkvvSn39bDCkXLtc3pfC82AGd8-uvmkuXCkPoTuzxMk14wVQNOB01AQ6L7bmsQBisycm2-znz7"
},
"CZv1oxUkfsVpbXNBMQsUZNzSvbt1" : {
"apartamento" : "8",
"bloco" : "P",
"celular" : "123456789",
"email" : "[email protected]",
"nome" : "Pedro",
"sobrenome" : "Henrique",
"telefone" : "99876543234",
"token" : "dvE4gBh1fwU:APA91bF9zLC1cOxT4jLsfPRdsxE8q0Z9P4uKuZlp8M5fIoxWd2MOS30u4TLuOQ4G2Sg0mlDqFMuzvjX3_ZSSi9XATyGtTtNse4AxwLYuD-Piw9oFn6Ma68nGfPSTnIEpvDYRwVnRI2e4"
},
}
}
私は自分自身をアンダーソンにすることができたと思うdとその後の感謝。
まず、お返事ありがとうございますが、私があなたの言ったことをやろうとしましたが、私はまだトークンを捕まえることができません。これを行うには、私はこれを試しています: 'var token = snap.val()。token;'、しかし、コンソールでその値を見ようとすると、 'undefined 'と表示されました。それで、私にそれをする正しい方法を示すことができますか? –