Google Calendar API - 推送通知无效

问题描述 投票:7回答:1

我正在使用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);
});

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

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

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

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

我一般不熟悉推送通知,所以不确定会发生什么。

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

我错过了什么?任何帮助真的很感激!

提前致谢!

javascript google-calendar-api gapi
1个回答
3
投票

我怀疑你很想知道究竟是什么推送通知。

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

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

Push notifications在做出更改后通知您的申请。

本文档介绍了如何使用推送通知,在资源发生变化时通知您的应用程序。

它通过启用手表来设置

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

这告诉Googles服务器,当有人对事件进行更改时,您希望收到通知。这将设置一个https:/examle.com/notifications的Web挂钩,一旦事件发生更改,将立即通知该挂钩。

事件的名称,日期时间通常会发生变化我不认为如果其他人被添加到事件中,您将获得推送通知。

这不是什么

服务器不会在事件发生前10分钟向您发送消息。这是用户通知和完全不同的东西,而不是Google Calendar api的一部分。

© www.soinside.com 2019 - 2024. All rights reserved.