将新 child 添加到数据库时的 Firebase 推送通知
Firebase Push Notification when a new child is added to the Database
-
javascript
-
firebase-realtime-database
-
google-cloud-functions
-
firebase-cloud-messaging
-
firebase-admin
我希望每次在 Firebase 数据库中创建新的 child 时都收到通知。这是我到目前为止得到的。使用这行代码,您会在创建新的 child 时收到通知。但问题是通知总是带有 Title: "Title", Body: "Come check it" 是。现在我的问题是如何创建具有值城市和时间的通知(请参阅下面的结构)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendPushNotification = functions.database.ref('/Rollerbanken/{Id}').onCreate(event => {
const payload = {
notification: {
title: 'Title',
body: 'come check it',
badge: '0',
sound: 'default',
}
};
return admin.database().ref('fcmToken').once('value').then(allToken => {
if(allToken.val()) {
const token = Object.keys(allToken.val());
return admin.messaging().sendToDevice(token, payload).then(response => {
});
};
});
});
我的结构:
{
"Rollerbanken" : {
"-KuKDXL2pY9MMtw551ZI" : {
"Extrainformatie" : "",
"Latitude" : "51.9145932124898",
"Longitude" : "5.86974696138047",
"Staater" : "Staat er",
"Staaternietmeer" : "",
"City" : "Overbetuwe",
"Time" : "15 : 43",
"TijdControle" : "15 : 43",
"TijdControleniet" : "",
"TypeControle" : "Rollerbank"
}
}
希望你能帮帮我!
基本上,您需要为要发送的每个通知修改有效负载对象。好消息是因为它只是一个对象,您可以轻松访问它,所以您所要做的就是 payload.name = YOURDESIREDVALUEHERE
所以您需要做的是获取新密钥(令牌)并使用它来访问您的对象。除非我记错了 Object.keys 生成一个数组,所以你的访问密钥应该是 token[0],然后用它来访问你的值,就像这样 allToken.val()[token[0]]["City"]
您的代码如下所示:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendPushNotification = functions.database.ref('/Rollerbanken/{Id}').onCreate(event => {
const payload = {
notification: {
title: 'Title',
body: 'come check it',
badge: '0',
sound: 'default',
}
};
return admin.database().ref('fcmToken').once('value').then(allToken => {
if(allToken.val()) {
const token = Object.keys(allToken.val());
payload.notification.title = allToken.val()[token[0]]["City"] + allToken.val()[token[0]]["Time"] //change here
return admin.messaging().sendToDevice(token, payload).then(response => {
});
};
});
});
javascript
firebase-realtime-database
google-cloud-functions
firebase-cloud-messaging
firebase-admin
我希望每次在 Firebase 数据库中创建新的 child 时都收到通知。这是我到目前为止得到的。使用这行代码,您会在创建新的 child 时收到通知。但问题是通知总是带有 Title: "Title", Body: "Come check it" 是。现在我的问题是如何创建具有值城市和时间的通知(请参阅下面的结构)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendPushNotification = functions.database.ref('/Rollerbanken/{Id}').onCreate(event => {
const payload = {
notification: {
title: 'Title',
body: 'come check it',
badge: '0',
sound: 'default',
}
};
return admin.database().ref('fcmToken').once('value').then(allToken => {
if(allToken.val()) {
const token = Object.keys(allToken.val());
return admin.messaging().sendToDevice(token, payload).then(response => {
});
};
});
});
我的结构:
{
"Rollerbanken" : {
"-KuKDXL2pY9MMtw551ZI" : {
"Extrainformatie" : "",
"Latitude" : "51.9145932124898",
"Longitude" : "5.86974696138047",
"Staater" : "Staat er",
"Staaternietmeer" : "",
"City" : "Overbetuwe",
"Time" : "15 : 43",
"TijdControle" : "15 : 43",
"TijdControleniet" : "",
"TypeControle" : "Rollerbank"
}
}
希望你能帮帮我!
基本上,您需要为要发送的每个通知修改有效负载对象。好消息是因为它只是一个对象,您可以轻松访问它,所以您所要做的就是 payload.name = YOURDESIREDVALUEHERE
所以您需要做的是获取新密钥(令牌)并使用它来访问您的对象。除非我记错了 Object.keys 生成一个数组,所以你的访问密钥应该是 token[0],然后用它来访问你的值,就像这样 allToken.val()[token[0]]["City"]
您的代码如下所示:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendPushNotification = functions.database.ref('/Rollerbanken/{Id}').onCreate(event => {
const payload = {
notification: {
title: 'Title',
body: 'come check it',
badge: '0',
sound: 'default',
}
};
return admin.database().ref('fcmToken').once('value').then(allToken => {
if(allToken.val()) {
const token = Object.keys(allToken.val());
payload.notification.title = allToken.val()[token[0]]["City"] + allToken.val()[token[0]]["Time"] //change here
return admin.messaging().sendToDevice(token, payload).then(response => {
});
};
});
});