0

I am pretty new in Javascript and through some Google help I have written below function in index.js to send push message to my app when the Firestore table will be update.

Index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotificationToFCMToken = functions.firestore.document('REQ/{mId}/Req/{bId}').onWrite(async (event) => {
//const uid = event.after.get('userUid');
const title = event.after.get('personName');
const content = event.after.get('content');
const fcmToken = event.after.get('token');
//let userDoc = await admin.firestore().doc(`users/${uid}`).get();
//let fcmToken = userDoc.get('token');

var message = {
    notification: {
        title: title,
        body: content,
    },
    token: fcmToken,
}

let response = await admin.messaging().send(message);
console.log(response);
});

The above function is properly working and sending push message to the app and showing the notification. But the problem is it should come to the app inside FirebaseMessagingService class through which I can able to customize it.

Ranjit
  • 5,130
  • 3
  • 30
  • 66
  • 1
    Notification messages are handled by the system notification tray when your app isn't in the foreground. If you always want your app to handle the messages, send data messages instead. – Michael Aug 13 '20 at 07:25
  • @Michael It's not coming in when app is in the foreground as well. Seems like something I have to add in that js code which I am not able due to no knowledge in JavaScript. – Ranjit Aug 13 '20 at 12:29

3 Answers3

1

Firebase Cloud Messaging can be used to send two types of messages. From the documentation on messages types:

  1. Notification messages, sometimes thought of as "display messages." These are handled by the FCM SDK automatically.
  2. Data messages, which are handled by the client app.

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.

So if you want your application code to handle the message, you should send a data message.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1

You need to send a data message as mentioned in documentation here

// your message object should look like this
var message = {
    data: {
        title: title,
        body: content,
    },
    token: fcmToken,
}

Rohan Lamb
  • 141
  • 5
1

Two types:

1.Notification message:

var message = {
    notification: 
    {
        title: 'This is title',
        body: 'This is body!' 
    },
    condition: condition
};

This one specifies the predefined, user-visible key-value pairs of the notification payload. FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys.

2.Data message:

var message = {
  data: {
    score: '850',
    time: '2:45'
  },
  token: registrationToken
};

This one specifies the custom key-value pairs of the message's payload. Client app is responsible for processing data messages. Data messages have only custom key-value pairs.(See https://stackoverflow.com/a/39149287/9391162 for more details, I've used that).

So when you want to handle it in client, you'll use Data messages.

This link shows sending notification in back-end, Node.js is also included.

AMK
  • 662
  • 6
  • 16