Wednesday, July 24, 2013

Google Calendar API Push notifications: don’t call us, we’ll call you

Author PhotoBy Piotr Stanczyk, Tech Lead, Google Calendar APIs

If you've developed an application integrated with Google Calendar, you know that you need to periodically poll for event changes to ensure your application stays in sync. Today we’re launching Push notifications for Calendar API. This change makes periodic polling unnecessary. Push notifications significantly reduce the amount of time that the app is out of sync with the server. For mobile devices, this can lead to big savings in data usage and power consumption.

The only thing an app needs to do to get the new push functionality is to subscribe to a calendar of interest. When a calendar changes, we’ll notify your app and the app does an API call to get the update. If you use one of the Google API client libraries it’s very easy to utilize push notifications.

As an example, let’s assume you have a calendar my_calendar@my-host.com. Your app is hosted on a server with my-host.com domain and push notifications should be delivered to an HTTPS web-hook https://my-host.com/notification:
Map<String, String> params = new HashMap<String, String>();
params.put("server", "my-host.com");
Channel request = new Channel()
.setId("unique_subscription_ID")
.setType("web_hook")
.setAddress(String.format("https://my-host.com/notification"))
.setParams(params);
service.events().watch("my_calendar@my-host.com", request).execute();
From now on, every time my_calendar@my-host.com changes, Google Calendar server will trigger a web-hook callback at https://my-host.com/notification. All the app needs to do is request an incremental sync as it did before:
changes = service.events().list("my_calendar@my-host.com")
.setUpdatedMin(lastSynchonizationTime).execute();
If you are interested in using this new feature, please refer to Google API v3 documentation for Push.


Piotr Stanczyk is a Tech Lead for Google Calendar APIs. His current focus is to provide the next generation Calendar API which makes lives of developers easier.

Posted by Scott Knaster, Editor

No comments:

Post a Comment