How I get menu click event

送分小仙女□ 提交于 2019-12-04 14:14:37

问题


I created a custom menu using mirror api.
menu created method on 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;
}

From doPost method I call MirrorClient

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

In MirrorClient define method 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();

}

then in NotifyServlet receive the event this way..

        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");
                }
            }

In Google Cloud Console I set callback url

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

How can I get menu's click event or action from my Servlet? Please somebody help....


回答1:


From mirror api java sample app you can see NotifyServlet implementation. (Or what type server you have find the relevant sample project from quickstart samples).

Firstly you have to define your notification callback to the mirror api. Then you must subscribe register for notifications. After this all menu selections for your glassware are going to be passed to your notification callback(servlet for notifications) througt mirror api.

If your servlet is written on Java try this at your notification 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        
} 

Edit: Define your notification callback url https. from this:

http://localhost:8080/notify

To this:

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

To subscribe to notifications in a production environment, you must provide a callback URL with a valid SSL certificate to handle the notification. For development purposes, you can use a subscription proxy server provided by Google that forwards notifications to a non-SSL callback URL. https://developers.google.com/glass/tools-downloads/subscription-proxy

Edit2 I modified sample java project a little bit to make it work for notifications on localhost. You may want to put below code to MirrorClient class's insertSubscription method:

// 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;
}


来源:https://stackoverflow.com/questions/20219643/how-i-get-menu-click-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!