如何在Firebase Cloud函数中指定声音和click_action [英] How to specify sound and click_action in firebase cloud function

查看:62
本文介绍了如何在Firebase Cloud函数中指定声音和click_action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下功能(node.js v8):

I tried with the following function (node.js v8):

exports.sendComNotification = functions.firestore
  .document('Comunicados/{comID}')
  .onUpdate((snap, context) => {
    console.log('Com triggered');
    const newValue = snap.after.data();
    const msg = newValue.title;
    var message = {
      notification: {
        title: 'Comunicado da Diretoria!',
        body: msg,
        badge: '1',
        sound: 'default',
        click_action: 'FLUTTER_NOTIFICATION_CLICK',
      },
      topic: "Comunicados"
    };
    return admin.messaging().send(message)
      .then((response) => {
        console.log('Successfully sent message:', response);
        return
      })
      .catch((error) => {
        console.log('Error sending message:', error);
        return
      });

  });

但这在功能日志中给出以下错误:

But that gives the following error in the fucntions log:

Error sending message: {
    Error: Invalid JSON payload received.Unknown name "badge"
    at 'message.notification': Cannot find field.
    Invalid JSON payload received.Unknown name "sound"
    at 'message.notification': Cannot find field.
    Invalid JSON payload received.Unknown name "click_action"
    at 'message.notification': Cannot find field.
    at FirebaseMessagingError.FirebaseError[as constructor](/srv/node_modules / firebase - admin / lib / utils / error.js: 42: 28)

如果我取出徽章",声音"和"click_action",则可以,但是接收时没有声音,并且onResume(flutter应用程序)中定义的动作也不会触发.设置声音和click_action道具的正确方法是什么?预先感谢.

If I take out "badge", "sound" and "click_action", it works, but then there's no sound upon receival and the actions defined in onResume (flutter app) don't fire either, of course. What would be the correct way of setting the sound and click_action props? Thanks in advance.

推荐答案

好吧,我终于设法在这里和那里收集点点滴滴,以使其正常工作.不明白为什么文档没有一条清晰而直接的途径来达到如此普遍的要求.

Ok, I finally managed to gather bits and pieces here and there to make it work. Don't understand why the docs do not have a clear and direct path to such a common requirement.

exports.sendComNotification = functions.firestore
  .document('Comunicados/{comID}')
  .onCreate((snap, context) => {
    const doc = snap.data();
    const msg = doc.title;
    var message = {
      notification: {
        title: 'Comunicado da Diretoria!',
        body: msg
      },
      data: {
        title: 'Comunicado',
        body: msg
      },
      android: {
        notification: {
          sound: 'default',
          click_action: 'FLUTTER_NOTIFICATION_CLICK',
        },
      },
      apns: {
        payload: {
          aps: {
            badge: 1,
            sound: 'default'
          },
        },
      },
      topic: "Comunicados"
    };


    return admin.messaging().send(message)
      .then((response) => {

        console.log('Successfully sent message:', response);
        return
      })
      .catch((error) => {
        console.log('Error sending message:', error);
        return
      });

  });

这篇关于如何在Firebase Cloud函数中指定声音和click_action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆