David
David

Reputation: 787

How to check FCM Message status?

How do I receive an Acknowledgement with FCM that a message was received? I have a progressive web app that checks if a user has a fcm token saved in the db, and uses FCM if they do and resorts to SMS (through twilio) if they do not. The problem is that ""Successfully sent fcm message" is logged even when the browser is closed, as messages are queued until the browser is reopened.

How can I tell that a message was actually received by the user, and isn't stuck waiting in a queue? If Chrome is closed, I want an SMS to send immediately.

const functions = require('firebase-functions');
const admin = require('firebase-admin');

const serviceAccount = require('./service-account.json');
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: `https://${process.env.GCLOUD_PROJECT}.firebaseio.com`
});

function sendFCM(uid, title, body) {
  const payload = {
    notification: {
      title: title,
      body: body,
      click_action: "https://www.google.com"
    }
  };

  const payloadOptions = {
    timeToLive: 0
  };

  getUserDetails(uid).then((details) => {
    if (details.fcmToken !== undefined) {
      // send fcm message
      return admin.messaging().sendToDevice(details.fcmToken, payload, payloadOptions)
        .then(response => {
          console.log("Successfully sent fcm message");
        })
        .catch((err) => {
          console.log("Failed to send fcm message");
          return sendSMS(details.phone_number, body);
        });;
    } else {
      console.log("No fcm token found for uid", uid);
      return sendSMS(details.phone_number, body);
    }
  })
}

Upvotes: 6

Views: 8653

Answers (1)

AL.
AL.

Reputation: 37778

There is currently no API or a part of the response from FCM that tells you if a message is currently in queue or is already sent.

However, you could make use of Delivery Receipts (Note: Implementing delivery receipts requires an XMPP Connection Protocol).

Upvotes: 3

Related Questions