DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • From APIs to Event-Driven Systems: Modern Java Backend Design
  • Understanding Java Signals
  • JQueue: A Library to Implement the Outbox Pattern
  • Cordova: Communicating Between JavaScript and Java

Trending

  • The Context Window Trap: Why More Context Doesn’t Mean Better AI
  • Nobody Designs an RBAC Mess; Everyone Ends Up With One
  • Beyond @Transactional: Solving the Dual-Write Problem in Distributed Microservices
  • Going All In On the JVM 25 Virtual Threads

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Alexa Proactive Notification Java Client Sender

Alexa Proactive Notification Java Client Sender

By 
Xavier Portilla Edo
DZone Core CORE ·
Apr. 02, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

This article aims at simplifying sending proactive events to an Alexa Skill from an external Java process.

Based on the video, From Zero to Hero, Part 13: Proactive Events Notifications by German Viscuso, and the Github project by Luca Rosellini  written in NodeJS. I have created a Java client so that it is easy to send notifications from any Java/Kotlin-based backend or, for example, from any Android device.

This code performs the following operations:

  1. Obtain an authentication token from Alexa by using the skill's client_id and client_secret. You can obtain these in the Build Tab — Permissions section of your skill in the Alexa developer console.
  2. Use the authentication token obtained in the previous step to broadcast notifications to those users of the skill that granted permissions to notifications in the Alexa app. It sends events following the AMAZON.MessageAlert.Activated schema.

Adding Proactive Events to Your Skill

Remember that if you want to send proactive events to your Skill, you need to modify the Skill's manifest (skill.json) to add notification permissions to the Skill and declare the schema(s) of the proactive events your Skill is allowed to send. Using SMAPI, you need to:

  • Add the permissions object to the skill's manifest property (skill.json):
JSON
xxxxxxxxxx
1
 
1
"permissions": [
2
    {
3
        "name": "alexa::devices:all:notifications:write"
4
    }
5
]
6


  • Add the events object to the skill's manifest property:
JSON
xxxxxxxxxx
1
15
 
1
"events": {
2
  "publications": [
3
    {
4
      "eventName": "AMAZON.MessageAlert.Activated"
5
    }
6
  ],
7
  "endpoint": {
8
    "uri": "** TODO: REPLACE WITH YOUR Lambda ARN after created **"
9
  },
10
  "subscriptions": [
11
    {
12
      "eventName": "SKILL_PROACTIVE_SUBSCRIPTION_CHANGED"
13
    }
14
  ]
15
}


  • Redeploy your skill by running:
Shell
 
xxxxxxxxxx
1
 
1
ask deploy -t skill


If you want to declare that the Skill accepts more than one schema, just add them into events.publications above and remember to change the template in your Event class from package com.xavidop.alexa.model.event.

The process is more thoroughly described in the official Alexa Proactive Events API documentation.

You can find a working example on how to configure your Skill for Proactive Events in the official Alexa Github repository: https://github.com/alexa/alexa-cookbook/tree/master/feature-demos/skill-demo-proactive-events.

Prerequisites

  1. You need Java >= 1.8 to run the code and Maven to download the required dependencies.
  2. How to install these two tools goes beyond the scope of this document.

Install

To download project dependencies simply add this dependency to your pom.xml file:

XML
xxxxxxxxxx
1
 
1
<dependency>
2
  <groupId>com.xavidop.alexa</groupId>
3
  <artifactId>alexa-proactive-event-sender</artifactId>
4
  <version>LATEST</version>
5
</dependency>


How to Use It

After adding the dependency, you can use the client, as shown below:

Java
 
xxxxxxxxxx
1
13
 
1
String clientId = "YOUR-CLIENT";
2
String secretId = "YOUR-SECRET";
3

          
4
AlexaProactiveEventSenderClient client = new     AlexaProactiveEventSenderClient(clientId, secretId);
5

          
6
ProactiveEvent event = new ProactiveEvent();
7
event.getEvent().getPayload().getMessageGroup().getCreator().setName("Test");
8

          
9
URLRegion urlRegion = new URLRegion();
10
urlRegion.setRegion(Region.NA);
11
urlRegion.setEnvironment(Environment.DEV);
12

          
13
client.sendProactiveEvent(event, urlRegion);


  • Environment: whether the target events will be sent to the live or development endpoints. Allowed values are dev and pro.
  • Region: identifies the region of the Alexa endpoint to use to send proactive events. Allowed values are EU (Europe), NA (North America), and FE (Far East). Remember: if your users are located in NA, and you are sending events trough the EU endpoint, users located in NA won't receive any notifications.

These are the values by default of an event when you create it:

JSON
 
xxxxxxxxxx
1
25
 
1
{
2
        "timestamp": "",
3
        "referenceId": "UUID-AUTOGENERATED",
4
        "expiryTime": "",
5
        "event": {
6
          "name": "AMAZON.MessageAlert.Activated",
7
          "payload": {
8
            "state": {
9
              "status": "UNREAD",
10
              "freshness": "NEW"
11
            },
12
            "messageGroup": {
13
              "urgency": "URGENT",
14
              "creator": {
15
                "name": ""
16
              },
17
              "count": 1
18
            }
19
          }
20
        },
21
        "relevantAudience": {
22
            "type": "Multicast",
23
            "payload": {}
24
        }
25
    }


That's all!

You can find all the releases and source code in my GitHub.

Happy coding!

Java (programming language) Event

Opinions expressed by DZone contributors are their own.

Related

  • From APIs to Event-Driven Systems: Modern Java Backend Design
  • Understanding Java Signals
  • JQueue: A Library to Implement the Outbox Pattern
  • Cordova: Communicating Between JavaScript and Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook