Search code examples
androidfirebasekotlingoogle-cloud-firestorefirebase-notifications

How to notify a store owner when the estimated delivery time is over in an e-commerce app using firebase


I need some ideas on how I can approach for a feature in a firebase based multi vendor e-commerce app. I want to send a notification to the store owner when the estimated delivery time is over.

When users place a order from a store, an estimated time like 50 minutes will be added to the order info. Now I have to schedule a notification for the owner if it's over 50 minutes and the order status is still pending, the owner will get a notification.

I don't have any code to attach because I am still thinking the approach. My idea is that every store owner will be subscribed to a topic as the storeId or I can send notification using the token of the owner. So only store owner will receive the notification.

But should I schedule the time using a cloud function for every orders or there are other ways to accomplish this?


Solution

  • But should I schedule the time using a cloud function for every orders or there are other ways to accomplish this?

    The notification sending should be triggered by a back-end and Cloud Functions is clearly the most convenient one. You could set up your own server to do so but using a serverless platform like Cloud Functions is much easier.

    There are two possible options:

    • Use a scheduled Cloud Function that checks, each minute, if there are orders for which the estimated delivery time is over, and if it is the case, sends the notification(s). The drawbacks of this approach are: Each minute a Cloud Function is triggered (but there is a generous free tier of 2M invocations/month); similarly, each minute a Firestore query is executed, costing at least one read if the query returns nothing (again, there is a generous free tier of 50K reads/day); finally, you cannot run a schedule Cloud Function with a higher frequency than once a minute (but in your case this is not a real problem).
    • For each order, schedule a Cloud Function to run in exactly 50 minutes, as explained in this article titled "How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL)". Drawback: The implementation is a bit more complex than the simple scheduled Cloud Function approach but you avoid the drawbacks of this approach, as listed above.