1

I can create daily notifications but not weekly ones using swift 3 ios 10 User Notifications framework. Daily works fine but when I add in the .weekday parameter it doesn't send me a notification.

The code is:

        for i in 1 ... 7
        {
            let center =  UNUserNotificationCenter.current();

            let content = UNMutableNotificationContent();
            content.title = "Title";
            content.body = "content";

            content.sound = UNNotificationSound.default();

            var dateComponents = DateComponents();

            dateComponents.weekday = i;

            // hours is 00 to 11 in string format
            if (daynight == "am")
            {
                dateComponents.hour = Int(hours);
            }
            else
            {
                dateComponents.hour = Int(hours)! + 12;
            }

            // mins is 00 to 59 in string format
            dateComponents.minute = Int(mins);
            dateComponents.second = 0;

            let date = Calendar.current.date(from: dateComponents);

            // tiriggerDaily works without weekday for daily but adding weekday doesn't make it weekly                                
            let triggerDaily = Calendar.current.dateComponents([.weekday, .hour, .minute, .second], from: date!);
            let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true);


            let identifier = // a uuid;
            let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger);

            center.add(request, withCompletionHandler:
            {
                (error) in

                if let error = error
                {
                    // Error happened
                    print("Error: ");
                    print(error);
                }
            });

Need to get code to figure out weekly. Used this as guidance: https://useyourloaf.com/blog/local-notifications-with-ios-10/

An example of what I want is this:

A user selects Monday, 11:25pm and it is a Monday at 11:23pm.
Then the user should get a notification in two minutes plus one the following week and so on.

Some other notes/questions:

1. Do you have to wait a week for weekly notifications to start? Like in the example above will it start the following week?

Just a thought since I can't get this working right yet.

cdub
  • 24,555
  • 57
  • 174
  • 303
  • Possible Duplicate of https://stackoverflow.com/questions/6047117/how-can-i-create-local-notifications-in-ios – Karthick Ramesh Jul 02 '18 at 05:41
  • Not a duplicate i want weekly notifications not just any and can do daily ones – cdub Jul 02 '18 at 05:43
  • Can you please elaborate more? do you want to add notification daily which will repeat on same day next week? or just a single notification weekly? Example with values would be more clear – Van Jul 02 '18 at 06:02
  • I want a user to select a say Monday at 11:01pm and then the notification will repeat every monday at 11:01pm going forward. The user can select whatever date they want. (Something like this: https://stackoverflow.com/questions/45061324/repeating-local-notifications-for-specific-days-of-week-swift-3-ios-10) – cdub Jul 02 '18 at 06:09
  • Hi @cdub .. Have you got any solution . if yes kindly post here – Ravi Ojha Feb 05 '19 at 12:10

0 Answers0