Friday 2 June 2017

How to check if Firebase Cloud Message is received by user immediately

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



via David

No comments:

Post a Comment