user2828442
user2828442

Reputation: 2515

How to add event in native calendar in Ionic

I am inserting an event in android using below code, how can I include firstReminderMinutes and secondReminderMinutes in my code ?

I have just hard coded one event in the constructor.

home.ts code:

import { Component } from '@angular/core';
import { NavController,AlertController } from 'ionic-angular';
import { Calendar } from '@ionic-native/calendar';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  event;

  constructor(public navCtrl: NavController,private calendar: Calendar,public alertCtrl: AlertController) {

    this.event = { title: "Event created from Quick Task", location: "Jalandhar", message: "Visit", startDate: "17 Mar 2018 13:30", endDate: "16 Mar 2018 14:30" };

    // this.calendar.createCalendar('MyCalendar').then(
    //   (msg) => { console.log(msg); },
    //   (err) => { console.log(err); }
    // );

    this.calendar.createEvent( this.event.title, this.event.location, this.event.message, new Date(this.event.startDate), new Date(this.event.endDate)).then(
      (msg) => {
        let alert = this.alertCtrl.create({
          title: 'Success!',
          subTitle: 'Event saved successfully',
          buttons: ['OK']
        });
        alert.present();
        this.navCtrl.pop();
      },
      (err) => {
        let alert = this.alertCtrl.create({
          title: 'Failed!',
          subTitle: err,
          buttons: ['OK']
        });
        alert.present();
      }
    );

  }



}

Should I use this.calendar.createCalendar instead of this.calendar.createEvent.

Upvotes: 0

Views: 5488

Answers (1)

Leonardo Gabriel
Leonardo Gabriel

Reputation: 347

To include firstReminderMinutes and secondReminderMinutes when you create an event, you have use the method createEventWithOptions, like the example below

// create an event silently (on Android < 4 an interactive dialog is 
   shown which doesn't use this options) with options:

var calOptions = window.plugins.calendar.getCalendarOptions(); // grab the defaults
calOptions.firstReminderMinutes = 120; // default is 60, pass in null for no reminder (alarm)
calOptions.secondReminderMinutes = 5;

// Added these options in version 4.2.4:
calOptions.recurrence = "monthly"; // supported are: daily, weekly, monthly, yearly
calOptions.recurrenceEndDate = new Date(2016,10,1,0,0,0,0,0); // leave null to add events into infinity and beyond
calOptions.calendarName = "MyCreatedCalendar"; // iOS only
calOptions.calendarId = 1; // Android only, use id obtained from listCalendars() call which is described below. This will be ignored on iOS in favor of calendarName and vice versa. Default: 1.

// This is new since 4.2.7:
calOptions.recurrenceInterval = 2; // once every 2 months in this case, default: 1

// And the URL can be passed since 4.3.2 (will be appended to the notes on Android as there doesn't seem to be a sep field)
calOptions.url = "https://www.google.com";

// on iOS the success handler receives the event ID (since 4.3.6)
window.plugins.calendar.createEventWithOptions(
    title,eventLocation,notes,startDate,
    endDate,calOptions,success,error
);

you can check all informations of ionic nativ calendar at https://github.com/EddyVerbruggen/Calendar-PhoneGap-Plugin

Upvotes: 1

Related Questions