Notification Trickered Firebase Database Query
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendToATokenedDevice = functions.database.ref('/userReceiverList/{email}/{msgId}/')
.onCreate((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
console.log(`Database Trickered OnCreate Sending Notification ${context.params.email}`);
return admin.database().ref(`/users/${context.params.email}/firebaseToken`).once('value')
.then(snapshot2 => {
var token_device=snapshot2.val();
console.log('Token',token_device);
var payload = {notification : {
body : "มีวัตถุต้องสงสัย โปรดคลิกเพื่อเปิดแอพ",
title : "แอบดูค้นพบ"
},
token: token_device
};
return admin.messaging().send(payload);
})
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
return null;
}).catch((error) => {
console.log('Error sending message:', error);
});
});
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendToATokenedDevice = functions.database.ref('/userReceiverList/{email}/{msgId}/')
.onCreate((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
console.log(`Database Trickered OnCreate Sending Notification ${context.params.email}`);
return admin.database().ref(`/users/${context.params.email}/firebaseToken`).once('value')
.then(snapshot2 => {
var token_device=snapshot2.val();
console.log('Token',token_device);
var payload = {notification : {
body : "มีวัตถุต้องสงสัย โปรดคลิกเพื่อเปิดแอพ",
title : "แอบดูค้นพบ"
},
token: token_device
};
return admin.messaging().send(payload);
})
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
return null;
}).catch((error) => {
console.log('Error sending message:', error);
});
});
ความคิดเห็น
แสดงความคิดเห็น