【问题标题】:Push notifications with sound and vibration通过声音和振动推送通知
【发布时间】:2020-08-12 02:35:29
【问题描述】:

我正在尝试在用户收到消息时向他们发送推送通知,通知成功并显示横幅,但它是静音且不振动的。

我使用 Atom 作为服务器来发送通知,代码看起来像,

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);
   });
   })
   })
  })

以下是我尝试获取通知以发送默认声音和振动的方式。

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

当我使用上面的代码发送消息时,我在我的 firebase 函数日志中看到以下错误

我尝试了其他一些方法,但这些方法甚至不会通过终端推送。实现此方法的正确方法是什么?

the issue with sound inside notification object 

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

上面的代码行是我得到下面错误的方式。

/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$ 

【问题讨论】:

    标签: ios node.js swift notifications google-cloud-functions


    【解决方案1】:

    是否振动取决于用户设置。您的应用程序不能取代用户在其设备设置中设置的值。

    您检查过您的设备设置吗?

    另外,请看这个问题:

    Push notifications don't vibrate and are silent

    如您所见,用户通过以正确格式发送文件名使事情正常进行。是否有可能您在有效载荷中错误地指定了声音?

    这是错误:

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

    声音应该在通知对象中。

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

    您可能会收到错误消息,因为您将声音键放在通知对象之外。如果你想让它工作,你的有效载荷应该是文档中描述的格式。

    编辑:

    在您更新的代码中,您忽略了用逗号分隔声音键值对。将您的代码更新为:

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

    【讨论】:

    • 是的!我的手机没有开启请勿打扰,也没有开启橙色开关。
    • @zachwilcox 如果您转到设置->通知->您的应用程序。声音是否启用?
    • 是的,它已启用!声音和横幅都打开了
    • 我只想得到默认的声音
    • @zachwilcox 发现了问题。请参阅我的更新答案。
    猜你喜欢
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多