Search code examples
node.jsactions-on-googledialogflow-es-fulfillment

Send a push notification to opted-in users at the time which they select


After taking the user's permission to send push notifications and saving the userID in firestore, how can I use it to send notifications to them at a time of their choice daily ?

For example : If the user selects 8:00 AM , I want to be able to send them the push notification everyday at 8 am. I can't write the code to send the notification in the action itself. What will I have to setup to accomplish this?


Solution

  • Once the user has granted you permission to send push notifications, you can use the Actions API to send the push notification.

    The setup would be as follows.

    Storing user preferences

    1) Dialogflow agent asks the user when they want to be notified

    2) User says they want to be notified at 8:00 am

    3) Dialogflow detects the answer and makes a fulfillment request to your webhook (The webhook can be the inline editor in dialogflow (Functions) or another server)

    4) In the handler of the intent, you have to store the UserId and the time they selected in your database (in this case, firestore)

    5) Dialogflow agent gives the final answer to the user and the interaction ends

    Sending the push notification

    1) Identify if you have to send a notification to a user; this is the tricky part.

    It is not possible to give you a definitive solution on this, because it heavily depends on the requirements of your system and the technologies that you are using/want to use.

    Basically, you can use cron jobs, background infinite processes, pub/sub, cloud scheduler, or any other tool that allows you to constantly read information from your database to determine which users need to be notified. I recommend you to check this answer where is discussed an approach to tackle this problem using Firestore.

    Notice that you don't necessarily need another server running, you can use the Functions used for the fulfillment.

    For example. You can set up Cloud Scheduler to run each hour, and make a request to your Functions. In the request, you read from Firestore and extract the entities that need to be notified that hour.

    2) Once you have the users that you want to notificate, you need to use the Actions API to send the push notification. Once again, this request can come from your fulfillment server, or another server used in the system.