ARH
ARH

Reputation: 1716

Repeat alarm every day in Xamarin Forms in iOS

I am trying to repeat an alarm everyday at same time in my Xamarin Forms. However the iOS version doesn't repeat but it only occurs once at the specified date & time. I am converting DateTime to NSPDateComponents and passing it to UNCalendarNotificationTrigger to create the alert. The Datetime value is correct, but the repeat is not working.

public static NSDateComponents ToNSDateComponents(this DateTime dt)
{
  var d = new NSDateComponents
  {
     Hour = dt.Hour,
     Minute = dt.Minute,
     Second = dt.Second,
     Month = dt.Month,
     Year = dt.Year,
     Day = dt.Day,
     TimeZone = NSTimeZone.SystemTimeZone
  };
  return d;
}

// notifyDate returns (e.g 16/11/2020 10:00 so I want the alarm to repeat every day at 10:00)
var trigger = UNCalendarNotificationTrigger.CreateTrigger(notifyDate.ToNSDateComponents(), true);
var requestID = "request_" + id;
var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
if (err != null){
}
});

Upvotes: 1

Views: 290

Answers (1)

Junior Jiang
Junior Jiang

Reputation: 12723

You are setting a special date, therefore it only can repeat once.

Here is the sample to repeat an alarm everyday at 09:30, you could have a look:

// 1
var dateComponents = new NSDateComponents();
dateComponents.Hour = 9;
dateComponents.Minute = 30;
var trigger = UNCalendarNotificationTrigger.CreateTrigger(dateComponents, true);

// 2
var content = new UNMutableNotificationContent();
content.Title = "Daily reminder";
content.Body = "Enjoy your day!";

var requestID = "request_" + id;
var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);

// 3
UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
    if (err != null)
    {
    }
});

Upvotes: 1

Related Questions