2

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

Moddasir
  • 1,449
  • 13
  • 33

1 Answers1

0

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;
}
Devrim
  • 15,345
  • 4
  • 66
  • 74
  • What do u mean by register? there is any form like voice command to register my custom menu item to mirror api or just need to define my mirror client in my project – Moddasir Nov 29 '13 at 11:51
  • I think I miss the point, Now there is no error on log, but although I do not get notification. From where should I call the MirrorClient.insertSubscription() method. from my doPost method or where I generated the bundle timelineitem? – Moddasir Nov 29 '13 at 14:53
  • I can see the menu in my glass. after tap on menu the timeline shows a synchronizing icon and the timeline becomes first position. normally that timeline's position is 3. it becomes 1 after tap on menu. but i do not get event from notification servlet. change the call back url in google console like "https://mirrornotifications.appspot.com/forward?url=http://localhost:8080/notify". and also done what u tell me before. – Moddasir Dec 02 '13 at 09:22