Opus1217 Opus1217 - 4 months ago 10x
Android Question

Is there a way to set up a push notification from Google Calendar to my android app?

I am using the Google Calendar API with the READ_CALENDAR permission to query certain soccer games from a calendar and display them integrated with results from my referee app. Currently I query the calendar onConnected which is not often enough if I add a new game in the user's calendar.

Obviously I can requery the calendar API whenever I display data, but I was hoping to listen for a push notification from the Calendar API saying that a new event had been added. The API here: https://developer.android.com/guide/topics/providers/calendar-provider.html doesn't mention anything.

However, here: https://developers.google.com/google-apps/calendar/v3/push talks about push notifications to your own server. Does anybody know a way to do the equivalent from an app?

UPDATE from answer. Does this code look reasonable, especially with respect to leaking resources?

private void updateGamesFromCalendar() {
//get Calendar games if allowed
if (checkCalendar()) {
//Look for events with the keyword list in the Title, and dates between today-7 and today+7
long nowMs = System.currentTimeMillis();
String[] eventSelectionArgs = new String[]{Long.toString(nowMs - TimeUnit.DAYS.toMillis(7)),Long.toString(nowMs + TimeUnit.DAYS.toMillis(7)) };
if ((mEventsCursor != null) && (mCalendarObserver != null)) mEventsCursor.unregisterContentObserver(mCalendarObserver);
mEventsCursor = getContentResolver().query(RefWatchSettings.uriEvents, RefWatchSettings.EVENT_PROJECTION, RefWatchSettings.whereClauseEvents, eventSelectionArgs, RefWatchSettings.orderByEvents);
if (mCalendarObserver == null) mCalendarObserver = new CalendarEventObserver(null);

//delete future Calendar games and replace with the new query



Calendar events are retrieved with queries (see Google Doc). Queries return Cursor objects (see Cursor JavaDoc). And Cursor objects can be observed with registerContentObserver(). So make a query on calendar events, register a Content Observer and let your Cursor open. You should receive updates of events through your Content Observer.