Search code examples
mongodbflutterpush-notificationfirebase-cloud-messagingnodejs-server

Perform an action when Firebase Notification is not received by the device


I am trying to check if a function or method is being invoked in the server and act accordingly.

I am creating a basic app that notifies people about their financial transactions. I am using Flutter for the app, NodeJS for the server side of things, Firebase Cloud Messaging for Push Notification and React in order to send the data. So far, I've been successful in sending the Notification and displaying a list of notification for the user when using the mobile application. The whole system works something like this:

  1. The message is composed and sent from the react website,
  2. The website calls a POST request from the server to send a notification,
  3. The POST request then calls the getMessaging() function from the Firebase API that sends the notification to that device
  4. The mobile application listens for a notification using onMessage() as well as onBackgroundMessage(). When a notification is received it again calls the server to store some notification data to the database.

The reason I am storing the notification in the database after it is received by the device is so that I know the status of the message I've sent i.e. "delivered". Now, I am aiming to send an SMS to the device if the notification is not "delivered" after a certain period of time. However, looking at the workflow, sending the notification and storing notification are two complete separate methods and thus, I am out of ideas. Is there any way to know if a method/function is being invoked and act accordingly. Or should I take some other approach to this scenario? (If yes, could you give some ideas?)


Solution

  • I am just trying to aid my future self in need. Thanks GrahamD for providing me with some ideas. The process of how my code manages it is below:

    sendNotification():

    This is a POST method that sends notification to the mobile application as well as store the data from that notification to a database. When storing to database, I modify my data model to take two more fields:

    1. status: with values like sent & delivered
    2. messageID: unique ID for each message generated by uuid.

    The action is still running with getMessaging.send().then() below.

    updateNotificationStatus()

    Instead of saving the notification data after being received by the application, I update just the status field using this function. Inside the Flutter app, whenever the application receives a message (onMessage or onBackgroundMessage from Firebase Cloud Messaging), I call this function as a PUT method to update the status from sent to delivered by using the messageId that was sent with the notification.

    getMessaging().send().then()

    Inside the sendNotification(), the callback .then() calls another method after the notification is sent. This middleware function gets invoked after the set period of time (set inside the code) to check the status of the messageId that was just sent. If the status has been changed to delivered, no action is taken. However, if the status remains unchanged (i.e: sent), then I can code the action I want to perform.

    The back and forth of function invocations is still a headache for me to explain as well as understand. Hopefully, this is understandable and can help out others looking for the answers.