ebarooni / capacitor-calendar

The Capacitor Calendar Plugin enables full calendar functionality on iOS and Android, with added reminder support for iOS devices.
https://capacitor-calendar.pages.dev
MIT License
25 stars 8 forks source link

Event not being saved to Google Calendar with `createEvent` function on Android #137

Open scaabel opened 1 week ago

scaabel commented 1 week ago

Hi, thanks for making such a great package.

I'm on an Android emulator (Google Pixel 3A) and the default calendar app is Google Calendar. I'm facing an issue where the events that I'm trying to save are not actually being added to Google Calendar. The feature I'm building allows users to select multiple events to be synced to their calendar.

I think the issue is caused by the available calendars in when CapacitorCalendar.listCalendars() is called. I'm only getting the Holidays and Birthdays calendar in the list and both of these calendars are possibly read only. Here is the log of the list

list of calendars [{"id":"1","title":"Holidays in United States","color":"#16A765"},{"id":"2","title":"Birthdays","color":"#92E1C0"}].

Below is the implementation to handle the creation of events. There are no errors thrown except when I try to omit the calendarId.

const response = await Promise.allSettled(
      events.map((event: CalendarEvent) => {
        const eventParams = {
          calendarId,
          title: event.title,
          notes: event.notes,
          startDate: event.startDate,
          endDate: event.endDate,
          isAllDay: false,
          alertOffsetInMinutes: event?.alertOffsetInMinutes ?? [30, 1]
        }

        console.log('eventParams', JSON.stringify(eventParams))
        return CapacitorCalendar.createEvent(eventParams).catch((error) => {
          console.log(
            'error',
            JSON.stringify({
              title: event.title,
              error
            })
          )
          throw {
            title: event.title,
            error: error.message
          } as FailedEvent
        })
      })
    )

    console.log('response', JSON.stringify(response))

capacitor-calendar version: ^6.7.0 @capacitor/core version: ^5.7.3

Do let me know if there is something wrong with my implementation or if you require further information.

Thanks again for such an amazing package!

ebarooni commented 1 week ago

Hey @scaabel and thanks. My assumption is that on the emulator you are not signed in with any google account. And thats why listCalendars returns only the birthdays and holidays. These calendars are readonly and therefore cant be manipulated.

Your implementation seems to be correct but to be sure I would try just a basic event only with a title to see if everything works as expected.

What i would try is to login with a google account and the emulator (if you havent already) and to see if you have access to your own calendar and if you can add events to it using the method.

If this still didnt work:

What i can offer is to implement a way to know if a calendar is readonly or not (this is already supported on ios). And like this you can filter out the calendars that are readonly.

I noticed your capacitor core version is 5.x.x but you are using the plugin for version 6.x.x. Doesnt this create issues? My expectation is that these major version of these dependencies should always match.

scaabel commented 1 week ago

Hi @ebarooni, thanks for the prompt reply.

I've ensured that the emulator has logged in with a proper google account. I've also tried with the demo app provided and it is not working as expected even with the correct versions.

Yes, permissions were requested. The createEvent function works fine in my iOS app. This only happens in Android devices.

Additionally, when calling the CapactiorCalendar.listCalendars(), I'm only getting the calendars under Other calendars alongside Birthday. When I should be also getting all the calendars under My calendars. There might be some bug or unintended behaviour in regards of the authorization when getting the calendars (merely a speculation). image image

ebarooni commented 1 week ago

@scaabel I will test this behavior and get back to you here.

scaabel commented 6 days ago

@ebarooni thanks, really appreciate it.

ebarooni commented 5 days ago

Hi @scaabel , i tested the behaviors that you reported. However i was not able to reproduce any! For the tests i used the example app from the repo without any modifications. listCalendars returned all my available calendars and createEvent saved an event to my personal calendar (both android). For listCalendars i request first read access and then i invoked the method. The available calendars on my emulator according to the calendar app itself is the following:

Screenshot_20240925_211234

And i received all from the plugin as expected. And for creating an event, first i requested write access and then i invoked the method from the example app.

Can you maybe give me more info on your setup? Which framework? What version? What model is the emulator and which android version?

My emulator is a Pixel 7 Pro with API level 34.

ebarooni commented 4 days ago

@scaabel And also what do you get when you invoke getDefaultCalendar?

scaabel commented 4 days ago

Hi @ebarooni ,

I'm using react/nextjs alongside this package.

Here is my emulator setup: Pixel 3A, API 34

I've attached a screenshot based on the demo app.

Here are the results of the function invoked from the bottom to top:

  1. listCalendars
  2. getDefaultCalendar
  3. createEvent

I hope this information helps.

capacitor-calendar-errors

AhegaHOE commented 3 days ago

Hi @scaabel,

If you omitted the calendarId and no calendar is marked as IS_PRIMARY, which is indicated by getDefaultCalendar returning an empty object, then this behavior is expected. I recommend always providing a calendarId.

ebarooni commented 3 days ago

@AhegaHOE his issue is that listCalendars also does not return all available calendars. But now im not so sure why is this happening.

AhegaHOE commented 3 days ago

@ebarooni hmm that is odd. I have tested it myself with my personal account and it shows all my calendars... đŸ¤”

AhegaHOE commented 3 days ago

@scaabel could you make create a sample project with instructions on how reproduce this? I could also not reproduce it, just like @ebarooni.