In this tutorial, we will go through the necessary steps to send Google Action Push Notifications as well as how to send them outside of a live session.
- Ask For Permission
- Send the Notification
- Send the Notification Outside a Session
The Google Action push notifications feature allows you to send notifications, which if tapped, trigger a predefined intent of your Action.
To be able to send notifications to a user they have to first explicitly opt-in to receive them for that specific intent. These intents have to be configured previously to be eligible as intents supporting push notifications.
In this tutorial, we will first take care of the configuration, go through the process for a user to opt-in and after that discuss one possible way to send notifications outside of a live session.
Note: Push notifications aren't currently supported on voice-activated speakers.
For an intent to be eligible to be used in push notifications you have to set it as one of the intents, which can be triggered by implicit invocations inside your Dialogflow Agent:
After you've done that, you can find the intent as one of your
Actions on your Google Actions
Select the intent and at the bottom of the tab you will find the
User engagement section, where you can enable the push notifications as well as set a content title:
After you've done that, you have to get a service account key for your Google Action as you will need it later on to get the access token needed to send out notifications.
For that, you have to first go the following url (replace the
your-project-id part with your project's id) and enable the
Actions API: https://console.developers.google.com/apis/api/actions.googleapis.com/overview?project=your-project-id.
Now, go over to https://console.developers.google.com/apis/credentials?project=your-project-id, again replace the
your-project-id part, and create a new service account key:
Project Owner as the role and
JSON as the key type:
Now that you got your service account key as well, the necessary configuration is done.
Before we can send our user the notification, we have to first ask them to opt-in to receive notifications. For that, we first use suggestion chips to ask if they are interested and after that send out the permission request using the
The response to the question will be mapped to the Jovo built-in
ON_PERMISSION intent, where you can check wether the permission was granted using
Go through this small interaction to provide the permission we need to send the notification to your dev account later on. While doing that also save the
userId attribute (
originalDetectIntentRequest.payload.user.userId) somewhere as we will need it later on.
Now we can go through and send the notification. The notification object we will send to the user has the following structure and attributes:
||Specifies the title of the notification||
||Specifies the user who will receive the notification||
||Specifies the intent which will be executed after the user tapped on the notification||
||Specifies the locale||
The intent we set here, has to be the one the user subscribed to, any other intent won't work.
To send the notification, we have to access the
$notification module, which needs the
googleapis package to work. So first, install the package:
Besides that, we have to add the module manually to the
google-platform-googleassistant plugin in our
app.js file as it is not included by default:
That's all, now we can access the
Last but not least, we have to authorize ourselves, which will get us the access token needed to send out the notification.
To get the access token we use the
private_key from the service account key we got at the beginning of the tutorial:
With the access token and the notification object we have everything we need at last:
But, there is one issue. How are you going to trigger the intent to send out the event? You might find a creative solution where you somehow route through the intent sending the notification in the middle of a session, but even then you are bound to there actually being a session in the first place, which pretty much nullifies to actual benefits of notifications, namely messaging your user while they're not using your Action.
In the next section, we will go through one possible way to send out notifications outside a session.
We won't go through the initial trigger to send out the notification but rather walk over the steps to trigger your Jovo project, which will send the notification. One possible trigger could be a AWS Lambda function triggered every X minutes by a CloudWatch event.
To trigger your project we will send out a request, where the intent is set to the one containing the code to send out the notification.
Here's a sample request we can use:
The only thing you would have to add would be your own dev account's user id.
To trigger the intent, you simply use this request as the body of a https request sent to your endpoint:
Simply call the function and parse the stringified request object as a parameter to trigger your Google Action's
For testing purposes, we use the Jovo Webhook as it is the most convenient one for local testing. If you. later on, host your project on AWS Lambda, you might use an AWS API Gateway which you point to the Lambda function and send the request to the API endpoint. Technically it works the same for every other cloud service provider. You simply want to send the https request to the endpoint, where your project is hosted on.
That's it, you made it to the end!