Google Calendar API-推送通知不起作用

Dan*_*anV 7 javascript google-calendar-api gapi

我正在使用Google Calendar API,并试图在触发日历事件时接收推送通知https://developers.google.com/calendar/v3/push

我认为一切设置正确...

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);
});
Run Code Online (Sandbox Code Playgroud)

但我想不是。当我调用上面的代码时,我得到200响应

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

我相信我也应该收到同步消息,但我没有(https://developers.google.com/calendar/v3/push#sync)

为了测试,我正在修改日历本身中的一个事件(更改标题,日期,删除等),并且我希望浏览器中会发生一些事情,但是什么也没发生。

我一般不熟悉Push通知,因此不完全确定会发生什么。

我已经通过身份验证并按照https://developers.google.com/calendar/quickstart/js显示事件

我想念什么?任何帮助都非常感谢!

提前致谢!

DaI*_*mTo 5

我怀疑您没有准确理解推送通知是什么。

有两种主要方法可以跟踪资源何时发生更改。您可以经常轮询该资源并检查资源中是否有任何更改。

例如,您的应用程序可以每五分钟运行一次,并向 Google 发出请求,要求将事件返回给您。返回该事件后,您将检查用户创建的事件是否有任何更改。这种检查更改的方法非常耗时,并且需要资源不断轮询服务器以查找更改。更好的方法是使用推送通知

当发生更改时,推送通知会通知您的应用程序。

本文档介绍如何使用推送通知在资源更改时通知您的应用程序。

它是通过启用手表来设置的

POST https://www.googleapis.com/calendar/v3/calendars/my_calendar@gmail.com/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.
}
Run Code Online (Sandbox Code Playgroud)

这会告诉 Google 的服务器,当有人对活动进行更改时,您希望收到通知。这会设置一个到 https://examle.com/notifications 的网络挂钩,一旦事件发生变化,就会收到通知。

活动名称、日期时间通常会发生变化,我认为如果其他人添加到活动中,您不会收到推送通知。

这不是什么

服务器不会在活动截止前 10 分钟向您发送消息。这是用户通知,是完全不同的东西,不属于 Google Calendar api 的一部分。