Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set reminder on device calendar using xamarin.forms cross platform C#

Set reminder on device calendar using xamarin.forms cross platform c#, Event add and calendar show by using XamForms.Controls.Calendar plugin, but now I want to set reminder on perticular device calendar in xamarin forms using cross platform

like image 709
sonu Avatar asked Feb 21 '17 06:02

sonu


1 Answers

You can not use/access calendar directly from the Xamarin.Forms project, for that you need to use the DependancyService and you need to write code for each platform. I attach the code for reference.

Windows //https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn495339.aspx

public async Task AddAppointment(ESF.Core.Models.ESFPortal_Events appointment)
{
    var appointmentRcd = new Windows.ApplicationModel.Appointments.Appointment();
    var date = appointment.ExpireDate.Value.Date;
    var time = appointment.ExpireDate.Value.TimeOfDay;
    var timeZoneOffset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
    var startTime = new DateTimeOffset(date.Year, date.Month, date.Day, time.Hours, time.Minutes, 0, timeZoneOffset);
    appointmentRcd.StartTime = startTime;

    // Subject
    appointmentRcd.Subject = appointment.Title;
    // Location
    appointmentRcd.Location = appointment.WhereWhen;
    // Details
    appointmentRcd.Details = appointment.Description;
    // Duration          
    appointmentRcd.Duration = TimeSpan.FromHours(1);
    // All Day
    appointmentRcd.AllDay = false;
    //Busy Status
    appointmentRcd.BusyStatus = Windows.ApplicationModel.Appointments.AppointmentBusyStatus.Busy;
    // Sensitivity
    appointmentRcd.Sensitivity = Windows.ApplicationModel.Appointments.AppointmentSensitivity.Public;
    Rect rect = new Rect(new Point(10, 10), new Size(100, 200));
string retVal = await AppointmentManager.ShowAddAppointmentAsync(appointmentRcd, rect, Windows.UI.Popups.Placement.Default);
    return !string.IsNullOrEmpty(retVal);
}

Android //http://developer.xamarin.com/guides/android/user_interface/calendar/ - Also used the Android Docs for extra understanding of variables

public async Task<bool> AddAppointment(ESF.Core.Models.ESFPortal_Events appointment)
{
    Intent intent = new Intent(Intent.ActionInsert);
    intent.PutExtra(CalendarContract.Events.InterfaceConsts.Title, appointment.Title);
    intent.PutExtra(CalendarContract.Events.InterfaceConsts.Description, appointment.WhereWhen + " " + appointment.Description);
    intent.PutExtra(CalendarContract.Events.InterfaceConsts.Dtstart, GetDateTimeMS(appointment.ExpireDate.Value));
    intent.PutExtra(CalendarContract.Events.InterfaceConsts.Dtend, GetDateTimeMS(appointment.ExpireDate.Value.AddHours(1)));
    intent.PutExtra(CalendarContract.ExtraEventBeginTime, GetDateTimeMS(appointment.ExpireDate.Value));
    intent.PutExtra(CalendarContract.ExtraEventEndTime , GetDateTimeMS(appointment.ExpireDate.Value.AddHours(1)));
    intent.PutExtra(CalendarContract.Events.InterfaceConsts.EventTimezone, "UTC");
    intent.PutExtra(CalendarContract.Events.InterfaceConsts.EventEndTimezone, "UTC");
    intent.SetData(CalendarContract.Events.ContentUri);
    ((Activity)Forms.Context).StartActivity(intent);
    return true;
}

IOS //http://developer.xamarin.com/guides/ios/platform_features/introduction_to_event_kit/

protected EKEventStore eventStore;
public AppointmentServiceh_iOS()
{
    eventStore = new EKEventStore();
}
public async Task<bool> AddAppointment(ESF.Core.Models.ESFPortal_Events appointment)
{
    var granted = await eventStore.RequestAccessAsync(EKEntityType.Event);//, (bool granted, NSError e) =>
    if (granted.Item1)
    {
        EKEvent newEvent = EKEvent.FromStore(eventStore);
        newEvent.StartDate = DateTimeToNSDate(appointment.ExpireDate.Value);
        newEvent.EndDate = DateTimeToNSDate(appointment.ExpireDate.Value.AddHours(1));
        newEvent.Title = appointment.Title;
        newEvent.Notes = appointment.WhereWhen;
        newEvent.Calendar = eventStore.DefaultCalendarForNewEvents;
        NSError e;
        eventStore.SaveEvent(newEvent, EKSpan.ThisEvent, out e);
        return true;
    }
    else
    {
        new UIAlertView("Access Denied", "User Denied Access to Calendar Data", null, "ok", null).Show();
        return false;
    }
}
public DateTime NSDateToDateTime(NSDate date)
{
    double secs = date.SecondsSinceReferenceDate;
        if (secs < -63113904000)
            return DateTime.MinValue;
        if (secs > 252423993599)
            return DateTime.MaxValue;
        return (DateTime)date;
    }

    public NSDate DateTimeToNSDate(DateTime date)
    {
        if (date.Kind == DateTimeKind.Unspecified)
            date = DateTime.SpecifyKind(date, DateTimeKind.Local);
        return (NSDate)date;
    }
like image 195
Chandresh Khambhayata Avatar answered Nov 04 '22 09:11

Chandresh Khambhayata