我如何获得菜单点击事件

时间:2013-11-26 14:22:23

标签: java google-glass google-mirror-api

我使用 mirror api 创建了一个自定义菜单 菜单在 MainServlet

上创建了方法
public List<MenuItem> makeDealMenu(String appBaseUrl) {
    String dealMenuIconUrl = appBaseUrl + "static/images/deal_50.png";

    MenuValue dealMenuValue = new MenuValue();
    dealMenuValue.setDisplayName("DEAL");
    dealMenuValue.setIconUrl(dealMenuIconUrl);

    List<MenuValue> dealMenuValueList = new ArrayList<MenuValue>();
    dealMenuValueList.add(dealMenuValue);

    MenuItem dealMenuItem = new MenuItem();
    dealMenuItem.setAction("CUSTOM");
    dealMenuItem.setId("dealMenu");
    dealMenuItem.setValues(dealMenuValueList);

    List<MenuItem> customMenuItemList = new ArrayList<MenuItem>();
    customMenuItemList.add(dealMenuItem);

    return customMenuItemList;
}

doPost 方法我调用 MirrorClient

MirrorClient.insertSubscription(credential,
                WebUtil.buildUrl(request, "/notify"), userId, "timeline");

MirrorClient 中定义方法insertSubscription

public static Subscription insertSubscription(Credential credential,
        String callbackUrl, String userId, String collection)
        throws IOException {
LOG.info("Attempting to subscribe verify_token " + userId
        + " with callback " + callbackUrl);

callbackUrl = callbackUrl.replace("appspot.com", "Appspot.com");

Subscription subscription = new Subscription();

subscription.setCollection(collection);
subscription.setCallbackUrl(callbackUrl);
subscription.setUserToken(userId);

return getMirror(credential).subscriptions().insert(subscription)
        .execute();

}

然后在 NotifyServlet 以这种方式接收事件..

        JsonFactory jsonFactory = new JacksonFactory();
        Notification notification = jsonFactory.fromString(notificationString,
                Notification.class);

if (notification.getUserActions().contains(
                    new UserAction().setType("CUSTOM"))) {

                String selectedCustomMenuItemId = notification.getItemId();
                if ("dealMenu".equals(selectedCustomMenuItemId)) {

                    LOG.info("********** I am here in event");
                }
            }

Google Cloud Console 中,我设置了回调网址

http://localhost:8080/oauth2callback
https://mirrornotifications.appspot.com/forward?url=http://localhost:8080/notify
http://localhost:8080

如何从我的Servlet获取菜单的点击事件或操作?请有人帮忙......

1 个答案:

答案 0 :(得分:0)

从镜像api java sample app您可以看到NotifyServlet实施。 (或者您从quickstart samples找到相关示例项目的类型服务器。)

首先,您必须定义镜像api的通知回调。然后,您必须订阅 register 以获取通知。在此之后,您的玻璃器皿的所有菜单选择将通过镜像api传递给您的通知回调(通知的servlet)。

如果你的servlet是用Java编写的,请在你的通知callBack上试试这个:

JsonFactory jsonFactory = new JacksonFactory();
// notificationString is parsed form httpRequest's inputstream which is send from Mirror API
Notification notification = jsonFactory.fromString(notificationString, Notification.class);
if (notification.getUserActions().contains(new UserAction().setType("CUSTOM").setPayload("dealMenu")) {
    // User selected CUSTOM menu item on your glassware        
} 

修改:定义您的通知回调网址https。从这个:

http://localhost:8080/notify

对此:

https://mirrornotifications.appspot.com/forward?url=http://localhost:8080/notify
  

要在生产环境中订阅通知,您必须这样做   提供带有有效SSL证书的回调URL来处理   通知。出于开发目的,您可以使用订阅   谷歌提供的代理服务器将通知转发给   非SSL回调URL。   https://developers.google.com/glass/tools-downloads/subscription-proxy

Edit2 我对示例java项目进行了一些修改,使其适用于localhost上的通知。您可能希望将以下代码放在MirrorClient类的insertSubscription方法中:

// To work with notifications, modify the notify callback's url by adding subscription-proxy
// callbackUrl = "https://mirrornotifications.appspot.com/forward?url=" + callbackUrl;
if("http://localhost:8080/notify".equals(callbackUrl)) {
    callbackUrl = "https://mirrornotifications.appspot.com/forward?url=" + callbackUrl;
}