How to specify sound and click_action in firebase cloud function
Asked Answered
P

1

8

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)

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.

Partly answered 5/12, 2019 at 22:12 Comment(5)
In your code, I see you refer to admin.messaging(). Is there more technology at play here than just firebase and cloud functions? Where is admin defined?Superconductivity
badge, sound and click_action are legacy API fields. You probably need to use them with a legacy API method like admin.messaging().sendToTopic(). See firebase.google.com/docs/cloud-messaging/…Sonorant
@Superconductivity it's declared at the beginning of index.js const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp();Partly
@HiranyaJayathilaka. The doc you point to doesn't say anything about "sound" or "click_action". If this code of mine is for the legacy API, what would be the code for the current API? Searched the docs to find a complete node.js example showing how to send to topics and this is the only one I've found.Partly
That page lists methods from the both the legacy API and the new API. It also links out to firebase.google.com/docs/cloud-messaging/http-server-ref which outlines the legacy API message format. You will badge, click_action etc there.Sonorant
P
18

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

  });
Partly answered 9/12, 2019 at 1:20 Comment(2)
Brilliant! Exactly what I needed bro.Brinkman
Thanks for this answer. It really pointed me in the right direction towards solving my issue.Valina

© 2022 - 2024 — McMap. All rights reserved.