Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Calendar API - Push notifications not working

I'm using the Google Calendar API and am trying to receive push notifications when a calendar event is triggered https://developers.google.com/calendar/v3/push

I think everything is setup correctly...

gapi.client.calendar.events.watch({
  calendarId: 'primary',
  resource: {
    id: uuid,
    type: 'web_hook',
    address: window.location.href,
  },
}, (err, response) => {
  if (err) {
    console.log('err:', err);
  } else {
    console.log('response:', response);
  }
}).then((res) => {
  console.log('res:', res);
});

But I guess not. I get a 200 response when I call the above code

{
 "kind": "api#channel",
 "id": "...",
 "resourceId": "...",
 "resourceUri": "https://content.googleapis.com/calendar/v3/calendars/primary/events?alt=json&maxResults=250&alt=json",
 "expiration": "1554203159000"
}

I believe I should also be receiving a sync message, but I am not (https://developers.google.com/calendar/v3/push#sync)

To test things I am modifying an event within the calendar itself (changing the title, date, deleting, etc), and I expect something to happen in my browser, but nothing.

I'm not familiar with Push notifications in general, so not exactly sure what to expect.

I'm already authenticated and displaying events as per https://developers.google.com/calendar/quickstart/js

What am I missing? Any help is really appreciated!

Thanks in advance!

like image 573
DanV Avatar asked Apr 01 '19 11:04

DanV


1 Answers

I suspect you are miss understanding exactly what push notifications is.

There are two primary ways to track when a resource has changed. You can poll that resource often and check for any changes in the resource.

For example Your application could run every five minutes and make a request to Google asking to have the event returned to you. When that event is returned you will then check if there are any changes in the event created by the user. This method of checking for changes is very time consuming and requires resources to constantly poll the server looking for changes. A better way of doing it is using Push notifications

Push notifications notify your application when a change has been made.

This document describes how to use push notifications that inform your application when a resource changes.

Its set up by enabling a watch

POST https://www.googleapis.com/calendar/v3/calendars/[email protected]/events/watch
Authorization: Bearer auth_token_for_current_user
Content-Type: application/json

{
  "id": "01234567-89ab-cdef-0123456789ab", // Your channel ID.
  "type": "web_hook",
  "address": "https:/examle.com/notifications", // Your receiving URL.
  ...
  "token": "target=myApp-myCalendarChannelDest", // (Optional) Your channel token.
  "expiration": 1426325213000 // (Optional) Your requested channel expiration time.
}

This tells Googles servers that you would like to be notified when ever someone makes a change to the event. This sets up a web hook to https:/examle.com/notifications which will be notified as soon as there is a change to the event.

Name of the event, date time are normally changes i dont think you will get a push notification if someone else is added to the event.

What this is not

The server is NOT going to send you a message 10 minutes before the event is due. Thats user notification and something completely different and not part of the Google Calendar api.

like image 57
DaImTo Avatar answered Sep 17 '22 08:09

DaImTo