How to setup 28 different local notifications that repeat every 28 days at 9AM [closed]

I am looking to setup a batch of 28 local notifications (1 each day) that will repeat again after 28 days until cancelled. These have to be set to be triggered at 9:00 AM everyday.

I have tried to set these notifications using the UNCalendarNotificationTrigger by giving datecomponents and adding a day by looping thru all the 28 and set repeating as true.

The downside is UNCalendarNotificationTrigger sets for the calendar month and not 28 days, so for day 29, 30 & 31 there are no notifications displayed. I need something that will trigger back to back.

This question has provided with some idea of how to use UNTimeIntervalNotificationTrigger, this works for one set of recurring notifications, but after that, they trigger at different intervals.

To make things easier to test, I have changed interval to 1 message every minute for 5 messages and then schedule 5 recurring messages using intervals. These recurring messages have repeat:true. After the 5 recurring messages, they don't follow the same pattern.

This code is in C# if you have a solution in swift, you are more than welcome to answer.

for (int i = 0; i < 5; i++)
{
    NSDateComponents DateComponents = new NSDateComponents();
    DateComponents.Minute = DateToSet.AddMinutes(i).Minute;
    DateComponents.Second = 0;
    UNMutableNotificationContent content = new UNMutableNotificationContent { Title = "Title " + i, Body = "Body " + i, Sound = UNNotificationSound.Default, CategoryIdentifier = "MyCategoryId" };
    UNCalendarNotificationTrigger trigger = 
    UNCalendarNotificationTrigger.CreateTrigger(DateComponents, false);
    UNNotificationRequest request = 
    UNNotificationRequest.FromIdentifier("MyCategoryId" + i.ToString(), content, trigger);
    UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
        {
            if (err != null)
            {
                Console.WriteLine("Error: {0}", err);
            }
            else
            {
                Console.WriteLine("Notification Scheduled: {0}", request);
             }
         });

     NSDate dd = DateToSet.AddMinutes(i).ToNSDate();
     var tt = NSCalendar.CurrentCalendar.DateByAddingUnit(NSCalendarUnit.Minute, 5, dd, NSCalendarOptions.None);
     //if DateToSet is later than current time, add that gap to the interval
     double AdditionalTime = 0;
     if (DateToSet.AddMinutes(i) > DateTime.Now)
     {
         AdditionalTime = (DateToSet.AddMinutes(i) - DateTime.Now).TotalSeconds;
     }
     var interval = tt.GetSecondsSince(dd) + AdditionalTime;

     UNMutableNotificationContent content1 = new UNMutableNotificationContent { Title = "Recurring Title", Body = "Recurring body " + i, Sound = UNNotificationSound.Default, CategoryIdentifier = "MyCategoryId" };
     var IntervalTrigger = UNTimeIntervalNotificationTrigger.CreateTrigger(interval, true);
     UNNotificationRequest RecurringReminderRequest = UNNotificationRequest.FromIdentifier("recurring_MyCategoryId" + i.ToString(), content1, IntervalTrigger);

UNUserNotificationCenter.Current.AddNotificationRequest(RecurringReminderRequest, (err) =>
                {
                    if (err != null)
                    {
                        Console.WriteLine("Error: {0}", err);
                    }
                    else
                    {
                        Console.WriteLine("RecurringReminderRequest Notification Scheduled: {0}", RecurringReminderRequest);
                    }
                });
            }

Any help is appreciated.



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation