Skip to content
Snippets Groups Projects
Commit 7795351b authored by priagungs's avatar priagungs
Browse files

add push notification in backend

parent e453b8ef
Branches
No related merge requests found
......@@ -24,6 +24,38 @@ let message = {
token: registrationToken
}
const db = admin.database();
let message_db = db.ref('message');
let chatrooms_db = db.ref('chatroom');
chatrooms_db.on('child_added', chatroom => {
console.log('chatroom added');
chatroom.ref.child('messages').on('child_added', chat => {
let chatref = message_db.child(chat.val());
console.log(chat.val());
chatref.on('value', chatSnap => {
console.log(chatSnap.val());
if (chatSnap.val()) {
let message = {
notification: {
title: chatSnap.child('nameSender').val(),
body: chatSnap.child('message').val()
},
token: chatSnap.child('receiver').val()
}
admin.messaging().send(message)
.then(response => {
console.log('Success : ' + response)
})
.catch(err => {
console.log('Error : ' + err)
});
}
});
console.log('chat added');
});
});
app.get('/send', function(req, res) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment