简体   繁体   English

如何使用 Google Calendar API v3 (JavaScript) 删除事件通知

[英]How to remove event notification using Google Calendar API v3 (JavaScript)

I have the following JSON我有以下 JSON

var resource = {
  "summary": "Sample Event",
  "start": {
    "dateTime": sample1
  },
  "end": {
    "dateTime": twoHoursLater
  },
  "reminders": {
    "useDefault": false,
    "overrides": [
      {
        "method": "popup",
        "minutes": "5"
      },
      {
        "method": "email",
        "minutes": "5"
      }

    ]
  }
};

I can insert it to Google Calendar using the code below我可以使用下面的代码将它插入到谷歌日历

function addEvent() {
  gapi.client.load('calendar', 'v3', function() {
    var request = gapi.client.calendar.events.insert({
      'calendarId':   calendarId,
      'resource':     resource
    });
    request.execute(function(resp) {
      console.log(resp);
    });
  });
}

In Google Calendar website it appears as:在 Google 日历网站中,它显示为:

5 minutes before, as email
5 minutes before

As you can see the event the I have has reminders, meaning it will notify via Popup and Email, 5 minutes before the event starts.如您所见,我的活动有提醒,这意味着它将在活动开始前 5 分钟通过 Popup 和 Email 通知。

Now, the thing is I want to remove this event reminder programatically using the API.现在,问题是我想使用 API 以编程方式删除此事件提醒。

What I have tried so far:到目前为止我尝试了什么:

Test 1:测试 1:

Based on the documentation https://developers.google.com/calendar/concepts/reminders基于文档https://developers.google.com/calendar/concepts/reminders

It says "To revert back to the default set of reminders, perform an update setting reminders.useDefault back to true. "它说“要恢复为默认提醒集,请执行更新设置reminders.useDefault回到true.

var resource3 = {
  "summary": "Sample Event xx",
  "start": {
    "dateTime": sample1
  },
  "end": {
    "dateTime": twoHoursLater
  },
  "reminders": {
    "useDefault": true
  }
};

function updateEvent(eventId) {
  gapi.client.load('calendar', 'v3', function() {
    var request = gapi.client.calendar.events.patch({
      'calendarId':   calendarId,
      'eventId':     eventId,
      'resource':     resource3
    });
    request.execute(function(resp) {
      console.log(resp);
    });
  });
}

The code above will return an error Cannot specify both default reminders and overrides at the same time.上面的代码将返回错误Cannot specify both default reminders and overrides at the same time. in the console.在控制台中。

Test 2:测试 2:

Based on an accepted answer on stackoverflow Google Calendar API: How to disable Notifications?基于 stackoverflow 上已接受的答案Google 日历 API:如何禁用通知?

It says "Therefore I think you can just set "useDefault": false and don't supply any overrides, and it should result in the event having no reminders."它说“因此我认为你可以设置"useDefault": false并且不提供任何覆盖,它应该会导致事件没有提醒。”

var resource3 = {
  "summary": "Sample Event xx",
  "start": {
    "dateTime": sample1
  },
  "end": {
    "dateTime": twoHoursLater
  },
  "reminders": {
    "useDefault": false
  }
};

function updateEvent(eventId) {
  gapi.client.load('calendar', 'v3', function() {
    var request = gapi.client.calendar.events.patch({
      'calendarId':   calendarId,
      'eventId':     eventId,
      'resource':     resource3
    });
    request.execute(function(resp) {
      console.log(resp);
    });
  });
}

The patching/updating works on other fields such as summary , start , and end .修补/更新适用于其他字段,例如summarystartend But it does not remove the event reminder.但它不会删除事件提醒。

Test 3:测试 3:

This is what I have experimented on so far.到目前为止,这是我已经试验过的。 I removed the reminders completely.我完全删除了提醒。 Still no luck.仍然没有运气。 Same output as in Test 2.与测试 2 中的相同 output。

var resource3 = {
  "summary": "Sample Event xx",
  "start": {
    "dateTime": sample1
  },
  "end": {
    "dateTime": twoHoursLater
  }
};

unction updateEvent(eventId) {
  gapi.client.load('calendar', 'v3', function() {
    var request = gapi.client.calendar.events.patch({
      'calendarId':   calendarId,
      'eventId':     eventId,
      'resource':     resource3
    });
    request.execute(function(resp) {
      console.log(resp);
    });
  });
}

Do you have any idea on how to remove the event reminder?您知道如何删除事件提醒吗?

So after working around with it, I successfully removed the event notification using the API.因此,在解决它之后,我成功地使用 API 删除了事件通知。 The answer is almost close as stated in Test 2 and it is done by using the code below:答案几乎接近测试 2 中所述,它是通过使用以下代码完成的:

var resource3 = {
  "summary": "Sample Event xx",
  "start": {
    "dateTime": sample1
  },
  "end": {
    "dateTime": twoHoursLater
  },
  "reminders": {
    "useDefault": false,
    "overrides": []
  }
};

function updateEvent(eventId) {
  gapi.client.load('calendar', 'v3', function() {
    var request = gapi.client.calendar.events.patch({
      'calendarId':   calendarId,
      'eventId':     eventId,
      'resource':     resource3
    });
    request.execute(function(resp) {
      console.log(resp);
    });
  });
}

I just need to add the overrides and giving it an empty value.我只需要添加overrides并给它一个空值。 I hope this helps if you encountered the same question.如果您遇到同样的问题,我希望这会有所帮助。

When patching using the libraries you can't just set overrides to null, you need to use Data.nullOf(ArrayList.class):使用库修补时,您不能仅将覆盖设置为 null,您需要使用 Data.nullOf(ArrayList.class):

 Event.Reminders reminders = new Event.Reminders()
                        .setUseDefault(false)
                        .setOverrides(Data.nullOf(ArrayList.class));
                newEvent.setReminders(reminders);

In my experience, patch / update for the event following the根据我的经验,为事件之后的事件打patch / update

reminders: {
  useDefault: false,
  overrides: []
}

only worked for Firefox, while Chrome kept stubbornly holding onto the defaults.仅适用于 Firefox,而 Chrome 一直顽固地保留默认值。 (It was super baffling, I checked it several times: the same event would have different reminders when viewing it in Firefox vs Chrome.) (超级莫名其妙,我查了好几遍:同一个事件在Firefox vs Chrome查看时提示不同。)

The way I got around it was by not creating the events in the first place, using the Calendar API:我解决它的方法是首先不创建事件,使用日历 API:

const event = {
  start: {...},
  end: {...},
  summary: 'My Event',
  reminders: {
    useDefault: false
  }
}
Calendar.Events.insert(event, calendarId)

This means I couldn't use the (slightly easier) Calendar service provided by Google in the app scripts but hey the API isn't too bad either.这意味着我无法在应用程序脚本中使用 Google 提供的(稍微简单一些的)日历服务,但是嘿 API 也不错。

Try this:尝试这个:

var resource3 = {
  "summary": "Sample Event xx",
  "start": {
    "dateTime": sample1
  },
  "end": {
    "dateTime": twoHoursLater
  },
  "reminders": {
    "useDefault": false,
    "overrides": []
  }
};

function updateEvent(eventId) {
  gapi.client.load('calendar', 'v3', function() {
    var request = gapi.client.calendar.events.patch({
      'calendarId':   calendarId,
      'eventId':     eventId,
      'resource':     resource3
    });
    request.execute(function(resp) {
      console.log(resp);
    });
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM