Search code examples
push-notificationdialogflow-esactions-on-googlegoogle-api-java-client

User-Id for Push-Notification on Actions for Google


I try to make a push notification for my google assistant app. I used the sendNotification Code form the google developer site: https://developers.google.com/actions/assistant/updates/notifications

I am coding Java. Everything is working, expect getting the correct user id. When I hardcode my user it works, but how do I get the user id in the code?

I tried following code:

Argument arg_userId = request.getArgument(ConstantsKt.ARG_UPDATES_USER_ID);

String userId = request.getUser().getUserId();

--> I get "java.lang.reflect.InvocationTargetException"

String userId = arg_userId.getRawText();

--> same Exception


Solution

  • There are two problems with the approach you're taking to get the notification ID:

    1. The ID attached to the user object is deprecated and probably unavailable.
    2. This wasn't the ID you wanted anyway.

    In the response where the user finalizes the notification, that response includes an ID which you should get and store. Since you're using Java, the code might look something like this:

    ResponseBuilder responseBuilder = getResponseBuilder(request);
    
    Argument permission = request.getArgument(ConstantsKt.ARG_PERMISSION);
    if (permission != null) {
      Argument userId = request.getArgument(ConstantsKt.ARG_UPDATES_USER_ID);
      // code to save intent and userID in your db
      responseBuilder.add("Ok, I'll start alerting you.").endConversation();
    } else {
      responseBuilder.add("Ok, I won't alert you.");
    }
    return responseBuilder.build();