私の雑誌のアプリケーションでは、Firebaseサービスを使用しています。このAndroidアプリケーションの1つの機能は、新しい記事が公開されるたびに新しい記事がすべてのデバイス。登録されているすべてのデバイスに通知を送信するためのメッセージング
私はこのようなDB内のすべてのデバイストークンを保存しています: FCMToken { ユーザーID:deviceToken }
を新しいノードがfirebaseデシベルで「公表」キーに追加されるたびので、FCM機能がトリガされると、メッセージは、すべてのデバイスに送信されます。
以下は、FCMの機能のためにJavaScriptで私のコードです:
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/published/{msg_id}').onWrite(event => {
const snapshot = event.data;
// Only send a notification when a new message has been created.
if (snapshot.previous.val()) {
return;
}
const msg_id = event.params.msg_id;
const msg_val=admin.database().ref(`messages/${msg_id}`).once('value');
return msg_val.then(msgResult =>{
const msg_title=msgResult.val().title;
const user_id=msgResult.val().userId;
console.log('msg title is',msg_title);
console.log('We have a new article : ', msg_id);
const payload={
data : {
title:"New Article",
body: msg_title,
msgid : msg_id,
userid : user_id
}
};
// const deviceToken = admin.database().ref('/FCMToken/{user_id}').once('value');
admin.database().ref('/FCMToken').on("value", function(dbsnapshot)
{
dbsnapshot.forEach(function(childSnapshot) {
//var childKey = childSnapshot.key;
const childData = childSnapshot.val();
const deviceToken=console.log("device token" + childSnapshot.val());
return admin.messaging().sendToDevice(childData,payload).then(response=>{
console.log("This was notification feature")
console.log("response: ", response);
})
.catch(function(error)
{
console.log("error sending message",error)
});
});
});
});
});
何らかの理由で、通知があります1つのデバイス(FCMノードの最初のトークン)にのみ送信されます。 更新: 私は自分のコードを更新して約束を使用していますが、何らかの理由でまだ動作していないので、最初のデバイストークンに通知を送信します。
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/published/{msg_id}').onWrite(event => {
const snapshot = event.data;
// Only send a notification when a new message has been created.
if (snapshot.previous.val()) {
return;
}
const msg_id = event.params.msg_id;
const msg_val=admin.database().ref(`messages/${msg_id}`).once('value');
return msg_val.then(msgResult =>{
const msg_title=msgResult.val().title;
const user_id=msgResult.val().userId;
console.log('msg title is',msg_title);
console.log('We have a new article : ', msg_id);
const payload={
data : {
title:"New Article",
body: msg_title,
msgid : msg_id,
userid : user_id
}
};
const promises=[];
// const deviceToken = admin.database().ref('/FCMToken/{user_id}').once('value');
admin.database().ref('/FCMToken').once('value').then(function(dbsnapshot)
{
dbsnapshot.forEach(function(childSnapshot) {
//var childKey = childSnapshot.key;
const childData = childSnapshot.val();
const deviceToken=console.log("device token" + childSnapshot.val());
const promise = admin.messaging().sendToDevice(childData,payload).then(response=>{
promises.push(promise)
console.log("This was notification feature")
console.log("response: ", response);
})
return Promise.all(promises)
.catch(function(error)
{
console.log("error sending message",error)
});
});
});
});
});
Responseオブジェクトは、この出力を与えている:応答:{結果:{エラー:[オブジェクト]}]、 canonicalRegistrationTokenCount:0、 failureCount:1、 successCount:0、 multicastId:6411440389982586000}
すべてのデバイスのFCMトークンを使用して送信することは、特定のトピックをサブスクライブするようにして、通知を送信代わりに。より効率的になります。 – kunwar97