Parsing error while deploying firebase - javascript

Code
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref(`/Notifications/{user_id}/{notification_id}/`).onWrite((change,context) =>{
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification to send to ', user_id);
if(!change.after.val()){
return console.log("A Notification has been deleted from the database", 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 : ', from_user_id);
const userQuery = admin.database().ref('UserData/${from_user_id}/name').once('value');
return userQuery.then(userResult => {
const userName = userResult.val();
const deviceToken = admin.database().ref(`/UserData/${user_id}/TokenID`).once('value');
return deviceToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title: '${userName}',
body: "You have recieved a new Message",
icon: "default",
click_action : "com.appmaster.akash.messageplus_TARGET_NOTIFICATION"
},
data : {
from_user_id : from_user_id,
from_user_name : userName
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response =>{
return console.log('This was the notofication Feature');
});
});
});
I have no idea whats causing the error because i don't even understand the errors in command prompt and it doesn't even show where the error is or what it is... Can someone help me out with this please

You're using template literals, however it looks like you're using standard quotes in some places and not the back tick `. I can also see that you're referencing your user_id variable, however it isn't preceded by the $ symbol with curly braces
template literals need to look like this
`string here blah blah with ${variableHere} `

Related

How can i send different type of Notificaton with FirebaseCloud sendNotification Function

I have this function which sends notification once there is certain change in my Db node. but i want to send multiple type of notifications for different actions. but when i deploy my function it overrides prior one and then i have tried all functions in single script but its not working properly
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/All_Users/{reciever_user_id}/CouponsUsedNotifications/{notification_id}')
.onWrite((data, context ) => {
const reciever_user_id = context.params.reciever_user_id;
const notification_id = context.params.notification_id;
console.log('Notification sent to : ', reciever_user_id)
if(!data.after.val()){
console.log('Notification Deleted : ', notification_id)
return null;
}
const deviceToken = admin.database().ref(`/All_Users/${reciever_user_id}/Credentials/device_token`).once('value');
return deviceToken.then(result =>
{
const token_id = result.val()
const payload = {
notification:
{
title: "Coupon Used",
body: `Your Coupon has been used..!!`,
icon: "default"
}
}
return admin.messaging().sendToDevice(token_id, payload).then(
Response => {
console.log('Notification Sent')
}
)
})})
exports.sendNotification = functions.database.ref('/All_Users/{reciever_user_id}/UserApprovalNotifications/{notification_id}')
.onWrite((data, context ) => {
const reciever_user_id = context.params.reciever_user_id;
const notification_id = context.params.notification_id;
console.log('Notification sent to : ', reciever_user_id)
if(!data.after.val()){
console.log('Notification Deleted : ', notification_id)
return null;
}
const deviceToken = admin.database().ref(`/All_Users/${reciever_user_id}/Credentials/device_token`).once('value');
return deviceToken.then(result =>
{
const token_id = result.val()
const payload = {
notification:
{
title: "Profile Approved",
body: `Your Profile has been Approved..!!`,
icon: "default"
}
}
return admin.messaging().sendToDevice(token_id, payload).then(
Response => {
console.log('Notification Sent')
}
)
})})
Found the solution...as i am not javascript developer thats why i was struggling but the solution i found is pretty simple. just install the files to new folder.
exports.sendNotification = function.
the sendNotification is the name of function just change it and deploy it

On Deploying the Firebase Cloud Function getting an error of "Each then should return a value or throw"

I am new to Cloud Functions so I having issues with below code, the error is in the last part where the console.log is mentioned, please assist what shall I been done to deploy the Function successfully, as I am following a tutorial there is no such error for the name.
'use-strict'
const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
exports.sendNotifications = functions.firestore.document("users/{user_id}/notifications/{notification_id}").onWrite(event => {
const user_id = event.params.user_id;
const notification_id = event.params.notification_id;
return admin.firestore().collection("users").doc(user_id).collection("notifications").doc(notification_id).get().then(queryResult => {
const from_user_id = queryResult.data().from;
const message = queryResult.data().message;
const from_data = admin.firestore().collection("users").doc(from_user_id).get();
const to_data = admin.firestore().collection("user").doc(user_id).get();
return Promise.all([from_data, to_data]).then(result => {
const from_name = result[0].data().name;
const to_name = result[1].data().name;
const token_id = result[1].data().token_id;
const payload = {
notification: {
title: "Notification from:" + from_name,
body: message,
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload).then(result =>{
console.log("notifcation sent");
});
});
});
});
By chaining your Promises and returning null in the last then(), as follows, you should solve your problem:
exports.sendNotifications = functions.firestore.document("users/{user_id}/notifications/{notification_id}").onWrite(event => {
const user_id = event.params.user_id;
const notification_id = event.params.notification_id;
return admin.firestore().collection("users").doc(user_id).collection("notifications").doc(notification_id).get()
.then(queryResult => {
const from_user_id = queryResult.data().from;
const message = queryResult.data().message;
const from_data = admin.firestore().collection("users").doc(from_user_id).get();
const to_data = admin.firestore().collection("user").doc(user_id).get();
return Promise.all([from_data, to_data]);
})
.then(result => {
const from_name = result[0].data().name;
const to_name = result[1].data().name;
const token_id = result[1].data().token_id;
const payload = {
notification: {
title: "Notification from:" + from_name,
body: message,
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload)
})
.then(messagingResponse => {
console.log("notification sent");
return null; //Note the return null here, watch the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/
});
});
You may have a look at the corresponding MDN documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#Chaining
Also, note that, in your code, it seems that you are not using the to_name constant.

TypeError in firebase function while sending notification

Code
'use 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("A Notification has been deleted from the database", notification_id);
}
const deviceToken = admin.database().ref(`/UserData/${user_id}/TokenID`).once('value');
return deviceToken.then(result =>{
const token_id = result.val();
const payload ={
notification: {
title: "Friend request",
body: "You have recieved a new Friend Request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response =>{
return console.log('This was the notofication Feature');
});
});
});
Error
It just sucks that all code for an app can be done in android using java but the functions should be in javascript... New to the language so not sure what the error even means... Can someone help me solve it please?
Change this:
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("A Notification has been deleted from the database", notification_id);
}
into this:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref(`/Notifications/{user_id}/{notification_id}/`).onWrite((change,context) =>{
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification to send to ', user_id);
if(!change.after.val()){
return console.log("A Notification has been deleted from the database", notification_id);
}
more info here:
https://firebase.google.com/docs/functions/beta-v1-diff

Firebase Cloud Messaging Cannot read property 'receiver_id' of undefined

I've got a problem with Firebase Cloud Messaging notifications. When I want to send friend request in my Android app the other client doesn't receive a notification about this. And Firebase Functions log says:
TypeError: Cannot read property 'receiver_id' of undefined
at exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:9:35)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:109:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:139:20)
at /var/tmp/worker/worker.js:730:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
Here is JavaScript code:
'use strict'
const functions = require('firebase-functions');
const admin = require ('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/Notifications/{receiver_id}/{notification_id}')
.onWrite(event => {
const receiver_id = event.params.receiver_id;
const notification_id = event.params.notification_id;
console.log('We have a notification to send to: ', receiver_id);
if (!event.data.val) {
return console.log('A notification has been deleted from database: ', notification_id);
}
const deviceToken = admin.database().ref(`/Users/${receiver_id}/device_token`).once('value');
return deviceToken.then(result => {
const token_id = result.val();
const payload = {
notification:
{
title: "Friend Request",
body: "you have received a new friend request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log('This was the notification feature.');
});
});
});
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/Notifications/{receiver_id}/{notification_id}').onWrite((change, context) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification : ', user_id);
const afterData = change.after.val();
if (!afterData){
return null;
}
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: ', from_user_id);
const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
return deviceToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title: "New Friend Request",
body: "You've received a new Friend Request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log('This was the notification feature');
});
});
});
});
Try this code. It is taken from here uploaded by #xxxQDAxxx.

Cant send push in firebase .sendToDevice()

I'm simply trying to make a web push notifications work but I cant seem to get past the .sendToDevice() method, it runs but I dont receive any push notifications. Can someone please tell/show me what im doing wrong here...
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotifications = functions.database.ref('/notifications/{notificationId}')
.onWrite((change, context) => {
console.info("Running..");
// Only edit data when it is first created.
if (change.before.exists()) {
return null;
}
// Exit when the data is deleted.
if (!change.after.exists()) {
return null;
}
// Grab the current value of what was written to the Realtime Database.
const NOTIFICATION_KEY = context.params.notificationId
const NOTIFICATION_DATA = change.after.val();
const payload = {
notification: {
title: `New Message from ${NOTIFICATION_DATA.user}!`,
body: NOTIFICATION_DATA.message,
icon: NOTIFICATION_DATA.userProfileImg,
click_action: `https://google.com`
}
};
return admin.database().ref('/tokens').once('value').then((data) => {
if ( !data.val( ) ) return;
const snapshot = data.val();
const tokens = [];
for(let key in snapshot) {
tokens.push( snapshot[key].token);
}
console.info(tokens);
console.info(snapshot);
return admin.messaging().sendToDevice(tokens, payload);
})
});

Categories

Resources