Send Push Notifications With Your Google Action

by Kaan Kilic on Feb 27, 2019

Send Google Assistant Push Notifications with Jovo

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.

Learn how to send notifications with your Alexa Skill here.

Introduction

Docs: Google Action Push Notifications

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.

Configuration

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:

Dialogflow Implicit Invocation

After you've done that, you can find the intent as one of your Actions on your Google Actions Actions tab:

Google Action 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:

Google Action Action User Engagement

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.

Google Actions API

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:

Google Service Account

Select Project Owner as the role and JSON as the key type:

Google Create Service Account

Google Create Service Account 2

Google Create Service Account Key

Now that you got your service account key as well, the necessary configuration is done.

Ask For Permission

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 askForNotification(intent) method.

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 this.$googleAction.isPermissionGranted():

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.

Permission Interaction

Send the Notification

Now we can go through and send the notification. The notification object we will send to the user has the following structure and attributes:

Name Description Value Required
title Specifies the title of the notification string Yes
userId Specifies the user who will receive the notification string Yes
intent Specifies the intent which will be executed after the user tapped on the notification string Yes
locale Specifies the locale string Yes

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 $googleAction object's $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 $notification module.

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 client_email and 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.

Send the Notification 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 SendNotificationIntent:

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.

To test everything, simply create a new javascript file and add the code above. Run the code and you should receive a notification on your Google Assistant device.

That's it, you made it to the end!

Any questions? Please let us know in the comments below 👇. You can also reach us on Twitter or Slack.


Kaan Kilic

Technical Content Marketing Associate at Jovo

Comments and Questions

Any specific questions? Just drop them below or join the Jovo Community Forum.

Join Our Newsletter

Be the first to get our free tutorials, courses, and other resources for voice app developers.