FCM notification not working when app is kill/closed

@Anithakor0610 sending the notification from the postman but unable to receiving the device. what i am doing wrong here.

Please help me.

@Anithakor0610 can you please share me your functions/index.js file code. Here am writing the code for sending the push notification

if i use headers in this file getting eerror. How to declare headers here

let functions = require(‘firebase-functions’);
let admin = require(‘firebase-admin’);

admin.initializeApp(functions.config().firebase);

//const serviceAccount = require(’./src/pages/login’);

var companyId = “58e5165fe4b0766cc6b3354c”;

// exports.sendPush = functions.database.ref(’/’).onCreate((change, context) => {
// console.log("cahnge "+change.ref.parent.child(‘uppercase’).set(change.val()));
// return change;
// });

exports.sendPush = functions.database.ref(’/’ + ‘{companyId}’ + ‘/customerFeedback/{id}’).onWrite((change, context) => {

console.log("Context " + context.params.companyId);
console.log("Data from ipad == " + JSON.stringify(change));

const mId = change.after.val();
console.log("data from ipad resp " + JSON.stringify(mId));
// console.log('ipad response data ’ +mId.responses[0].responseData.length);
console.log("response " + mId.responses[0].responseData[0].answers[0].answerText);

// var answerThreshold = mId.responses[0].responseData[i].answerThreshold;
var queText = [];
var i;
// mId.responses[0].responseData[i].answerThreshold
for (i = 0; i < mId.responses[0].responseData.length; i++) {

if (mId.responses[0].responseData[i].answers[0].answerText <= mId.responses[0].responseData[i].answerThreshold) {
  //if(mId.responses[0].responseData[i].answers[0].answerText == 2){
  queText[i] = mId.responses[0].responseData[i].questionText;
  console.log("i = " + i + "; queText length = " + queText.length);

  if (i == queText.length-1) {
  console.log("call the function");
  let tokens;
  var cityRef = admin.firestore().collection("devices").doc(mId.managerId);
  console.log("data from firestore " + JSON.stringify(cityRef));

  var getDoc = cityRef.get()
    .then(doc => {
      if (!doc.exists) {
        console.log('No such document!');
      } else {
        // console.log('Id coming from firestore :', doc.data().managerID);
        console.log("query text " + queText);
        console.log(doc.data().managerID + " == " + mId.managerId)
        if (doc.data().managerID == mId.managerId) {

          tokens = doc.data().token;
          let msg = 'Customer has given negative feedback for ' + queText;

          let payload = {
            notification: {
              title: 'Customer Feedback',
              body: msg,
              sound: 'default',
              click_action: "FCM_PLUGIN_ACTIVITY",
               forceStart:“1” 
            },
            data: {
              Message: msg,
            }
          };

          //  console.log("Token " + tokens);

          admin.messaging().sendToDevice(tokens, payload)

          //   this.http.post('https://fcm.googleapis.com/fcm/send', JSON.stringify(body), option)
          // .map(res => res.json())
          // .subscribe(data => {
          //   console.log(data);    
          // });
        }
      }
    })
    .catch(err => {
      console.log('Error getting document', err);
    });
}

}

}
return getDoc;

});

Hello, @kattasrinu ,i have used headers for the “content-type” and “Authorization key of firebase”.

let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('Authorization', 'key='+"YOUR API AUTHORIZATION KEY HEAR)"

im also not getting notification when app is closed.
can anyone help me?

hello @HarshadD can you share your code.

In my case problem was due to battery optimization for my ionic app
Thanks !

yeh,some android phone has that battery optimization problem.

Hi. how do you get the device token?

“forceStart”: “1”
worked like a charm. Thanks much!!

I am also have same problem.please help me to solve.

1 Like

So what is the code from mobile side to receive message.

Sir its working if app is kill then redirect to specific page from notification?