0

I have a cloud Firestore function that send notification to users.

[ANDROID]

It is working great, but I'am receiving the notifications only when app is in backround (pause or killed).

Here is a simplified version of my function :

    'use strict'


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


exports.sendNotification = functions.database.ref('/Notifications/{receiver_user_id}/{notification_id}')
.onWrite((data, context) =>
{
    const receiver_user_id = context.params.receiver_user_id;
    const notification_id = context.params.notification_id;


console.log('We have a notification to send to :' , receiver_user_id);


if (!data.after.val()) 
{
    console.log('A notification has been deleted :' , notification_id);
    return null;
}

const DeviceToken = admin.database().ref(`/Users/${receiver_user_id}/device_token`).once('value');

return DeviceToken.then(result => 
{
    const token_id = result.val();

    const payload = 
    {
        notification:
        {
            title: "New Chat Request",
            body: `You have a new chat Request`,
            icon: "default"
        }
    };

    return admin.messaging().sendToDevice(token_id, payload)
    .then(response => 
        {
            console.log('This was a notification feature.');
        });
});
});

I don't have any particular java code because I only need the notifications to be displayed for now. Does anyone have a idea to archieve this issue in a simple way?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ImNeos
  • 527
  • 6
  • 17

1 Answers1

0

FCM Notifications are handled by the system when your app is backgrounded, and displayed in the system tray on Android. If you want to handle the notifications when your app is active, you will have to write code for that by overriding onMessageReceived.

Also see:

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