Push notifications with sound and vibration

Viewed 1275

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

image

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$ 
1 Answers

Whether to vibrate or not depends on user settings. Your application cannot supersede the values that a user sets in his device settings.

Have you checked your device settings ?

Also, please look at this question:

Push notifications don't vibrate and are silent

As you can see that user got things working by sending the file name in the correct format. Is it possible that you are specifying the sound incorrectly in the payload ?

Here is the error:

var payload = {
  notification: {
    title: userThatSent.name + ' sent you a message',
    body: message.text
    
  },
  sound: 'default'
};

Sound should be within the notification object.

See: https://firebase.google.com/docs/reference/admin/node/admin.messaging.NotificationMessagePayload

You are probably receiving the error, because you put the sound key outside the notification object. If you want it to work, your payload should be in the format described in the documentation.

EDIT:

In your updated code, you have neglected to separate the sound key value pair with a comma. Update your code to this:

var payload = {
  notification: {
    title: userThatSent.name + ' sent you a message',
    body: message.text,
    sound: 'default'
  },
};
Related