Firebase useful codes to get notification
'user strict'const functions = require('firebase-functions');const admin=require('firebase-admin');admin.initializeApp(functions.config().firebase);exports.sendNotification=functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite(event => {const user_id = event.params.user_id;const notification_id = event.params.notification_id;console.log('We have a notification to send to ', user_id);if (!event.data.val()) {return console.log('ANotification has been deleted from db :',notification_id);}const fromUser=admin.database().ref(`/notifications/${user_id}/${notification_id}/`).once('value');return fromUser.then(fromUserResult => {const from_user_id=fromUserResult.val().from;console.log('You have a new notification from id :',from_user_id);const userQuery=admin.database().ref(`Users/${from_user_id}/`).once('value');return userQuery.then(userQueryResult => {const userName=userQueryResult.val().name;console.log('You have a new notification from name :',userName);const deviceToken=admin.database().ref(`/Users/${user_id}/`).once('value');return deviceToken.then(result => {const token_id=result.val().device_token;console.log('ANotification OCCURED from token id :',token_id);const payLoad={notification:{title:"Friend Requset",body:`${userName} has sent you request`,icon:"default",click_action:"com.mamstricks.mypets.pets_TARGET_NOTIFICATION"},data: {from_user_id:from_user_id}};console.log('ANotification OCCURED from payload :',payLoad);return admin.messaging().sendToDevice(token_id,payLoad).then(response => {console.log('this was the notification features');});});});});});
0 Comments:
Post a Comment