我如何获得菜单点击事件 [英] How I get menu click event

查看:195
本文介绍了我如何获得菜单点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

菜单在 MainServlet 上创建了方法

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

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 以这种方式接收事件..

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

Google Cloud Console 我设置了回调网址

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

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

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

推荐答案

来自镜像api java示例应用程序,您可以看到 NotifyServlet 实现。 (或者您从快速入门示例中找到相关示例项目的类型服务器)。

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).

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

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.

如果您的servlet是用Java编写的,请试试这个在你的通知回叫中:

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        
} 

编辑:从此定义您的通知回调网址https。 :

Define your notification callback url https. from this:

http://localhost:8080/notify

对此:

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




To订阅生产环境中的通知,您必须
提供带有有效SSL证书的回调URL来处理
通知。出于开发目的,您可以使用Google提供的订阅
代理服务器将通知转发到
非SSL回调网址。
HTTPS://developers.go ogle.com/glass/tools-downloads/subscription-proxy

Edit2 我修改了示例java项目有点使它适用于localhost上的通知。您可能希望将以下代码放在 MirrorClient 类的 insertSubscription 方法中:

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

这篇关于我如何获得菜单点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆