Skip to content

Instantly share code, notes, and snippets.

@nabeelnazir163
Created April 12, 2018 06:16
Show Gist options
  • Save nabeelnazir163/48f50df6884708625b4dbdb383bf9bb8 to your computer and use it in GitHub Desktop.
Save nabeelnazir163/48f50df6884708625b4dbdb383bf9bb8 to your computer and use it in GitHub Desktop.
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/notification/chat_notifiation/{rec_user_id}/{sender_user_id}').onWrite((change, context) => {
const sender_user_id = context.params.sender_user_id;
const receiver_user_id = context.params.rec_user_id;
console.log('We have a notification from : '+ sender_user_id+ " | Receiced by " + receiver_user_id);
if(!change.after.val()){
return console.log('A Notification has been deleted from the database : ', sender_user_id);
}
const fromUser = admin.database().ref(`/users/${sender_user_id}/`).once('value');
return fromUser.then(fromUserResult => {
const fromUserName = fromUserResult.val().name;
const deviceTOken = admin.database().ref(`/users/${receiver_user_id}/fcmtoken`).once('value');
return deviceTOken .then(result => {
const token_id = result.val();
const payload = {
notification: {
title : "Ask Aalim",
body : `You have new message from ${fromUserName}`,
icon : "defaulf",
click_action : "com.zillion.android.AskAalim_Target_Notification"
},
data : {
emailforchat : sender_user_id
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response => {
return console.log('this was notification');
});
});
});
});
exports.sendNotification = functions.database.ref('/notification/notifications/{rec_user_id}/{notification_Id}').onWrite((change, context) => {
const notificationID = context.params.notification_Id;
const receiver_user_id = context.params.rec_user_id;
console.log('You have a notification : '+ receiver_user_id + ' and ' + notificationID );
if(!change.after.val()){
return console.log('A Notification has been deleted from the database : ', sender_user_id);
}
const senderUser = admin.database().ref(`/notification/notifications/${receiver_user_id}/${notificationID}`).once('value');
return senderUser.then(senderUserResult => {
const senderUserid = senderUserResult.val().fromUser;
const fromUser = admin.database().ref(`/users/${senderUserid}/`).once('value');
return fromUser.then(fromUserResult => {
const fromUserName = fromUserResult.val().name;
const deviceTOken = admin.database().ref(`/users/${receiver_user_id}/fcmtoken`).once('value');
return deviceTOken .then(result => {
const token_id = result.val();
const payload = {
notification: {
title : "Ask Aalim",
body : `${fromUserName} commentted on your post`,
icon : "defaulf",
click_action : "com.zillion.android.AskAalim_Target_Notification"
},
data : {
emailforchat : senderUserid
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response => {
return console.log('this was notification');
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment