I am trying to send push notifications to users when they receive a message, The notification is successful with showing a banner however it is silent and does not vibrate.
I am using Atom as the server to send the notification and the code looks like,
exports.observeMessages = functions.database.ref('/messages/{messageId}').onCreate((snapshot, context) => {
const fromId = snapshot.val().fromId;
const toId = snapshot.val().toId;
console.log('LOGGER --- fromId is ' + fromId);
console.log('LOGGER --- toId is ' + toId)
var message = snapshot.val();
return admin.database().ref('/users/' + toId).once('value', snapshot => {
var userWhoRecieved = snapshot.val();
return admin.database().ref('/users/' + fromId).once('value', snapshot => {
var userThatSent = snapshot.val();
var payload = {
notification: {
title: userThatSent.name + ' sent you a message',
body: message.text
},
sound: 'default'
};
admin.messaging().sendToDevice(userWhoRecieved.fcmToken, payload)
.then(function(response) {
console.log(response.results[0].error);
console.log('Succesfully sent message:', response);
})
.catch(function(error) {
console.log(response.results[0].error);
console.log('Error sending message', error);
});
})
})
})
below is how I am trying to get the notification to send the default sound and vibrate.
var payload = {
notification: {
title: userThatSent.name + ' sent you a message',
body: message.text
},
sound: 'default'
};
When I send a message using the code above, I see the following error in my firebase functions log
I have tried a few other methods but those methods won't even get pushed through the terminal. What is the correct way to get this method implemented?
the issue with sound inside notification object
var payload = {
notification: {
title: userThatSent.name + ' sent you a message',
body: message.text
sound: 'default'
},
};
The lines of code above are how I am getting the error below.
/Users/wilcox.323/Downloads/OddJobs-master/functions/index.js:219
sound: 'default'
^^^^^
SyntaxError: Unexpected identifier
at wrapSafe (internal/modules/cjs/loader.js:1116:16)
at Module._compile (internal/modules/cjs/loader.js:1164:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Module.require (internal/modules/cjs/loader.js:1089:19)
at require (internal/modules/cjs/helpers.js:73:18)
at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:15:15
at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:53:3)
at Module._compile (internal/modules/cjs/loader.js:1200:30)
ODEE236182:OddJobs-master wilcox.323$
