I am writing an android application where I need to send a notification based on some condition.
For example, when notiType = home then send other message in notification. If notiType = inBetween then send another message
I have written the cloud function for this but getting an error while deploying.
Here is the cloud function :
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
/* Listens for new messages added to /messages/:pushId and sends a notification to users */
exports.pushNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite(event => {
console.log('Push notification event triggered');
/* Grab the current value of what was written to the Realtime Database
*/
const userId = event.params.user_id;
const notificationId = event.params.notification_id;
const deviceToken = admin.database().ref(`Notifications/${userId}/${notificationId}/deviceToken`).once('value');
const childName = admin.database().ref(`Notifications/${userId}/${notificationId}/childName`).once('value');
const notificationType = admin.database().ref(`Notifications/${userId}/${notificationId}/type`).once('value');
return Promise.all([deviceToken, childName, notificationType]).then(result => {
const token = result[0].val();
const name = result[1].val();
const type = result[2].val();
/* Create a notification and data payload. They contain the notification information, and message to be sent respectively */
const payload;
switch (type) {
case "home":
payload = {
notification: {
title: 'App Name',
body: `${name} is reached at home`,
sound: "default"
}
};
break;
case "between":
payload = {
notification: {
title: 'App Name',
body: `${name} stuck on the way for some reason`,
sound: "default"
}
};
break;
case "school":
payload = {
notification: {
title: 'App Name',
body: `${name} reached at school`,
sound: "default"
}
};
break;
};
return admin.messaging().sendToDevice(token, payload).then(response => {
return null;
});
});
});
Getting this error :
Please correct me where I am going wrong. Using Firebase -tools version 5.0.1
JavaScript is telling you this line is invalid:
const payload;
You can't declare a const variable without also giving it a value immediately. Since you are conditionally giving it a value later, perhaps you should use let payload; instead.
Related
Getting Error- SyntaxError: Unexpected identifier
****const {
TurnContext,
MessageFactory,
TeamsActivityHandler,
CardFactory,
ActionTypes
} = require('botbuilder');
class BotActivityHandler extends TeamsActivityHandler {
constructor() {
super();
/* Teams bots are Microsoft Bot Framework bots.
If a bot receives a message activity, the turn handler sees that incoming activity
and sends it to the onMessage activity handler.
Learn more: https://aka.ms/teams-bot-basics.
NOTE: Ensure the bot endpoint that services incoming conversational bot queries is
registered with Bot Framework.
Learn more: https://aka.ms/teams-register-bot.
*/
// Registers an activity event handler for the message event, emitted for every incoming message activity.
this.onMessage(async (context, next) => {
TurnContext.removeRecipientMention(context.activity);
switch (context.activity.text.trim()) {
case 'Hello':
await this.mentionActivityAsync(context);
break;
case 'Bye':
await this.mentionActivityAsync1(context);
break;
default:
// By default for unknown activity sent by user show
// a card with the available actions.
const value = { count: 0 };
const card = CardFactory.heroCard(
'Lets talk...',
null,
[{
type: ActionTypes.MessageBack,
title: 'Say Hello',
value: value,
text: 'Hello'
}]);
await context.sendActivity({ attachments: [card] });
break;
}
await next();
});
}
/**
* Say hello and # mention the current user.
*/
async mentionActivityAsync(context) {
const TextEncoder = require('html-entities').XmlEntities;
const mention = {
mentioned: context.activity.from,
text: `<at>${ new TextEncoder().encode(context.activity.from.name) }</at>`,
type: 'mention'
};
const replyActivity = MessageFactory.text(`Hi ${ mention.text }`);
replyActivity.entities = [mention];
await context.sendActivity(replyActivity);
}
}
async mentionActivityAsync1(context) {
const TextEncoder = require('html-entities').XmlEntities;
const mention = {
mentioned: context.activity.from,
text: `<at>${ new TextEncoder().encode(context.activity.from.name) }</at>`,
type: 'mention'
};
const replyActivity = MessageFactory.text(`Bye ${ mention.text }`);
replyActivity.entities = [mention];
await context.sendActivity(replyActivity);
}
module.exports.BotActivityHandler = BotActivityHandler;****
I want to send notification to users when they receive new messages with the below JavaScript code
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.pushNotification = functions.database.ref('/messages/{user_id}/{message_id}').onWrite( (change, context) => {
const user_id = context.params.user_id;
const message_id = context.params.message_id;
console.log('We Have A Notification for :', user_id);
if (!change.after.val()){
return console.log("A Notification Has Been Deleted From The Database: ", message_id)
}
const fromUser = admin.database().ref(`/messages/${user_id}/${message_id}`).once('value');
return fromUser.then(fromUserResult => {
const from_user_id = fromUserResult.val().from;
console.log("You have new notification from : ", from_user_id)
const userQuery = admin.database().ref(`/Users/${from_user_id}/name`).once('value');
const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
return Promise.all([userQuery, deviceToken]).then(result => {
const userName = result[0].val();
const token_id = result[1].val();
const payload = {
notification: {
title: "Chat+",
body: `You have a new notification from ${userName}`,
icon: "default",
click_action: "com.mani.eric.quickch_TARGET_NOTIFICATION"
},
};
return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{
console.log('this is the notification')
});
});
});
});
the notification actually gets delivered but on both devices(sender and receiver gets same notification) with the user name of the sender as null.
my question now is, how can i retrieve the sender user name and display the notification only on the receivers device?
You have a type on the path that triggers the function:
functions.database.ref('/messages/{user_id/{message_id}')
Should be:
functions.database.ref('/messages/{user_id}/{message_id}')
So with a closing parenthesis after user_id.
Please read how to create a minimal, complete, verifiable example, as the code you shared is quite a bit more complex than needed to reproduce the problem. For example, your console.log('We Have A Notification for :', user_id); already should show that user_id is null, so the code after that can't work, and is irrelevant to the problem. Reducing the scope of the problem this way increases the chances that you'll find the cause yourself. Or at worst, it reduces the code we need to look at, which increases the chance that somebody will spot the problem and answer.
I am receiving default push notification instead of my own payload data
in notification.
Here you can see the notification in this Picture below
I am getting no errors on firebase log, also receiving data which I want to send through push notification
Here you can see
and here is the code of index.js file of firebase cloud function
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotifications = functions.database.ref(`messages/{notificationId}`).onCreate((event) => {
const receiverId = event.val().recId;
const payload = {
notification: {
title: `New Message from ${event.val().sndrName}!`,
body: event.val().message,
status: "New message",
icon: 'icon-192x192.png'
}
}
console.info(payload);
let tokensList = [];
return admin.database().ref('fcmtokens').orderByValue().equalTo(receiverId).once('value').then((token) => {
console.info(token.val());
if(token.val()) {
tokensList = (Object.keys(token.val()));
console.info(tokensList);
return admin.messaging().sendToDevice(tokensList,payload);
}
})
})
I am very new to firebase cloud functions please tell if I am doing something wrong,TIA
I am using firebase cloud functions to send a user push notifications. I dont understand JS well but I would like to be able to auto-increment the apps badge number through the notification payload and increase the number by 1 for each notification recieved. This is what I have now. I have read the documentation for firebase but I dont think I have enough JS understanding to figure out what they are describing.
exports.sendPushNotificationLikes = functions.database.ref('/friend-like-push-notifications/{userId}/{postId}/{likerId}').onWrite(event => {
const userUid = event.params.userId;
const postUid = event.params.postId;
const likerUid = event.params.likerId;
if (!event.data.val()) {
return;
}
// const likerProfile = admin.database().ref(`/users/${likerUid}/profile/`).once('value');
const getDeviceTokensPromise = admin.database().ref(`/users/${userUid}/fcmToken`).once('value');
// Get the follower profile.
const getLikerProfilePromise = admin.auth().getUser(likerUid);
return Promise.all([getDeviceTokensPromise, getLikerProfilePromise]).then(results => {
const tokensSnapshot = results[0];
const user = results[1];
if (!tokensSnapshot.hasChildren()) {
return console.log('There are no notification tokens to send to.');
}
const payload = {
notification: {
title: 'New Like!',
body: '${user.username} liked your post!',
sound: 'default',
badge: += 1.toString()
}
};
const tokens = Object.keys(tokensSnapshot.val());
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload).then(response => {
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove);
});
});
});
Thanks in advance for any help
I'm guessing this is what the issue is:
const payload = {
notification: {
title: 'New Like!',
body: '${user.username} liked your post!',
sound: 'default',
badge: += 1.toString()
}
};
Assume you have a notification count property available in your schema say notificationCount then you can do this:
const payload = {
notification: {
title: 'New Like!',
body: `${user.username} liked your post!`,
sound: 'default',
badge: Number(notificationCount++) // => notificationCount + 1
}
};
Also on this body: '${user.username} liked your post!', this will be saved as "user.username like your post!". This is not the behaviour you want, what you should be doing is this:
body: `${user.username} liked your post!`
Assuming this is the line in question:
badge: += 1.toString()
Careful of type conversion assumptions. Adding "1" + "1" will give you "11", not "2". Why not try something like:
badge: `${targetUser.notificationCount + 1}`
This is assuming the notificationCount is a key in your schema, and that it is typed as a string. You will need to persist the target user's notification count somewhere so it can be incremented when a new notification comes in. It could also be an integer and then the string interpolation is unnecessary, i.e.:
badge: targetUser.notificationCount + 1
Also, be aware that your string interpolation here needs to be wrapped in backticks instead of single quotes, i.e.:
body: `${user.username} liked your post!`
I can't tell how the interactions are mapped in your database. This approach requires persisting and updating the notification count of the target user.
Possible duplicate. Not sure.
connections: {
connectionID : {
userID: true,
anotherUserID: true
},
users: {
userID : {
deviceToken : "tokenID",
name : "Display Name"
},
anotherUserID : {
deviceToken : "tokenID",
name : "Display Name"
}
}
and so on and so forth.
This is my index.js:
exports.sendConnectionNotification = functions.database.ref('/connections/{connectionID}/{userID}').onWrite(event => {
const parentRef = event.data.ref.parent;
const userID = event.params.userID;
const connectionID = event.params.connectionID;
// If un-follow we exit the function.
if (!event.data.val()) {
return console.log('Connection', connectionID, 'was removed.');
}
// Get the list of device notification tokens.
const getDeviceTokensPromise = admin.database().ref('/users/${userID}/deviceToken').once('value');
// Get the user profile.
const getUserProfilePromise = admin.auth().getUser(userID);
and it continues. I am getting this error in my logcat:
Error: Firebase.child failed: First argument was an invalid path: "/users/${userID}/deviceToken". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
at Error (native)
at Ge (/user_code/node_modules/firebase-admin/lib/database/database.js:111:59)
at R.h.n (/user_code/node_modules/firebase-admin/lib/database/database.js:243:178)
at Fd.h.gf (/user_code/node_modules/firebase-admin/lib/database/database.js:91:631)
at exports.sendConnectionNotification.functions.database.ref.onWrite.event (/user_code/index.js:31:51)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:35:20
at process._tickDomainCallback (internal/process/next_tick.js:129:7)
I do not understand why Firebase is not able to reach the node. Clearly, my path is valid. Where am I going wrong? Sorry, I happen to start learning Firebase Functions just today.
**EDIT 1: **
After replacing:
const getDeviceTokensPromise = admin.database().ref('/users/${userID}/deviceToken').once('value');
with
const getDeviceTokensPromise = admin.database().ref(`/users/${userID}/deviceToken`).once('value');
I have gotten a new error. My console log displays:
There are no notification tokens to send to.
Here is my full index.js:
// // Create and Deploy Your First Cloud Functions
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
/**
* Triggers when a user gets a new follower and sends a notification.
*
* Followers add a flag to `/followers/{followedUid}/{followerUid}`.
* Users save their device notification tokens to `/users/{followedUid}/notificationTokens/{notificationToken}`.
*/
exports.sendConnectionNotification = functions.database.ref('/connections/{connectionID}/{userID}').onWrite(event => {
const parentRef = event.data.ref.parent;
const userID = event.params.userID;
const connectionID = event.params.connectionID;
// If un-follow we exit the function.
if (!event.data.val()) {
return console.log('Connection', connectionID, 'was removed.');
}
// Get the list of device notification tokens.
const getDeviceTokensPromise = admin.database().ref(`/users/${userID}/deviceToken`).once('value');
// Get the user profile.
const getUserProfilePromise = admin.auth().getUser(userID);
return Promise.all([getDeviceTokensPromise, getUserProfilePromise]).then(results => {
const tokensSnapshot = results[0];
const user = results[1];
// Check if there are any device tokens.
if (!tokensSnapshot.hasChildren()) {
return console.log('There are no notification tokens to send to.');
}
console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.');
console.log('Fetched user profile', user);
// Notification details.
const payload = {
notification: {
title: `${user.userNickName} is here!`,
body: 'You can now talk to each other.'
}
};
// Listing all tokens.
const tokens = Object.keys(tokensSnapshot.val());
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload).then(response => {
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove);
});
});
});
You can do use (`) instead of (') as i was also having same problem and solved by using this.
thanks
Change
const getDeviceTokensPromise = admin.database().ref('/users/${userID}/deviceToken').once('value');
to
const getDeviceTokensPromise = admin.database().ref('/users/' + userID + '${userID}/deviceToken').once('value');
'/users/${userID}/deviceToken' is not a valid path.
but '/users/123456/deviceToken' where 123456 represents the user ID, is.
maybe you are using single quote instead of back-ticks.
https://developers.google.com/web/updates/2015/01/ES6-Template-Strings
so the path is not concatenated in a right way.