Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify sound and click_action in firebase cloud function

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.

like image 489
Jaime Avatar asked Dec 05 '19 22:12

Jaime


People also ask

What are the two types of notifications in Firebase?

With FCM, you can send two types of messages to clients: Notification messages, sometimes thought of as "display messages." These are handled by the FCM SDK automatically. Data messages, which are handled by the client app.

How do I send a notification to a specific user?

It is important to note that a push notification must be sent to a device before you can send a notification to an individual user. All you have to do is specify the user's device token and app channel ID to the FCM server. If you want to know how to send push notifications to specific users. Contact WonderPush.

How do I send notifications from Firebase console to specific user?

You can send notification messages using the Notifications composer in the Firebase console. Though this does not provide the same flexibility or scalability as sending messages with the Admin SDK or the HTTP and XMPP protocols, it can be very useful for testing or for highly targeted marketing and user engagement.


1 Answers

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

  });
like image 59
Jaime Avatar answered Sep 27 '22 20:09

Jaime