0

I have an iOS app that sends a time sensitive push notification from an event via Cloud Firestore-triggered function.

In my function, I do a simple get operation prior to sending out the push notification, and it gets delivered from 30 sec up to 1 min. Can anyone advise on improving the speed?

I've looked at the online documentation talking about setting a minimum # of instances to reduce a cold start. I've also looked at this SO answer. As someone that's new to this, I admittedly am having difficulty following and pinpointing my advised next step. Any help and guidance would be extremely appreciated.

  exports.pushNotification = functions.firestore.document('/users/{userId}').onUpdate((change, context) => {  
var recipientUid = context.params.userId;

return admin.firestore().collection('users').doc(recipientUid).get().then(doc => {
  const userData = doc.data();
  var fcmToken = userData.fcmToken;

      var message = {
        notification: {
          title: sendingTitle,
          body: sendingMessage
        },
        apns : {
          payload : {
            aps : {
              badge : 1,
              sound: "default"
            }
          }
        },
        token: fcmToken
      };

      admin.messaging().send(message)
      .then((response) => {

        console.log('Successfully sent push notification message:', response);
        return;
      })
      .catch((error) => {
        console.log('Error sending message:', error);
        return;
      });
    })
  });
Chris
  • 431
  • 8
  • 33

1 Answers1

1

One possible cause is that you don't correctly manage the life cycle of your Cloud Function: as a matter of fact you are not returning the Promises chain, as shown below (see the comments in the code):

exports.pushNotification = functions.firestore.document('/users/{userId}').onUpdate((change, context) => {
    var recipientUid = context.params.userId;

    return admin.firestore().collection('users').doc(recipientUid).get()
        .then(doc => {
            const userData = doc.data();
            var fcmToken = userData.fcmToken;

            var message = { ... };

            return admin.messaging().send(message)   // !!! See the return here
        })    // And see also that we close the then block here
        .then((response) => {
            console.log('Successfully sent push notification message:', response);
            return;
        })
        .catch((error) => {
            console.log('Error sending message:', error);
            return;
        });
});

For more details on how to correctly manage the life cycle, I would suggest you watch the 3 videos about "JavaScript Promises" from the Firebase video series as well as read the following doc. Also, reading the doc on how to chain Promises will help.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • thank you for the answer. i did try to deploy my function file with the stated adjustment, but i am receiving a syntax error around closing the block with "})". – Chris May 31 '22 at 22:15
  • I've double checked the code in the anwser and it looks correct. At which line, exactly, do you get the error? – Renaud Tarnec Jun 01 '22 at 17:08
  • the closing parentheses where you commented "and see also that we close the then block here" – Chris Jun 01 '22 at 18:12
  • The syntax is totally correct IMO. I guess that you replaced the value of `message` in my answer by a real object. – Renaud Tarnec Jun 01 '22 at 18:53
  • What happens if you replace the two `return;`s by `return null;`? It should not make any difference but just trying… – Renaud Tarnec Jun 01 '22 at 18:54
  • yes the value of message is what i had posted. i tried return null but i am receiving the same syntax error when deploying. – Chris Jun 01 '22 at 20:02
  • You can try to remove everything after this line ("and see also that we close the then block here"), it should work (the console logs will not be executed) – Renaud Tarnec Jun 01 '22 at 20:44
  • Which JavaScript linter are you using? – Renaud Tarnec Jun 01 '22 at 20:45
  • not sure how to check which javascript linter i am using, but it deployed after removing the console log blocks. after testing, still not any faster. :( – Chris Jun 01 '22 at 23:32
  • setting a minimum # of instances will clearly reduce the cold starts but has a price (approx 90$ a month) – Renaud Tarnec Jun 02 '22 at 04:43
  • yeah, that's disappointing to hear, but really appreciate your attention on this. – Chris Jun 02 '22 at 05:13