0

I want to create an iOS-app which will send LocalNotifications at a specific time (Alarm-App).

My problem is that I want to set the Notifications at 00:01 o'clock for the current date. I don't know how to achieve this because background-work at a specific time is not allowed (Stackoverflow-Post).

EDIT:

It should be possible that it rings:

  • every day
  • at specific day of week (for example: every monday and friday)
  • first day rings, second day rings, third day rings not, fourth day rings not (this is the biggest problem)
  • sometimes I should skip a notification (because the user already got it)(big problem too)

In Android I solved this problem by setting the alarms for the current day at 00:01 o'clock

Is this also possible in iOS?

Community
  • 1
  • 1
Lee
  • 819
  • 7
  • 32
  • Please have a look at notifications first. You actually schedule them at a date of your choice. – shallowThought Jan 26 '17 at 09:17
  • Yes, but what if it should ring every day at 6 o'clock and I want to set the notification at midnight. – Lee Jan 26 '17 at 09:23
  • Than you set a notification for every day at 6 o'clock. – shallowThought Jan 26 '17 at 09:24
  • But when should I set the notification? – Lee Jan 26 '17 at 09:37
  • When the user sets the alarm. – shallowThought Jan 26 '17 at 09:39
  • The user wants to get a notification very day at 6 o'clock. He saves this. Here I can set the notification for this day but when do I set the event for the following days (tomorrow, in a month, in a year etc.) – Lee Jan 26 '17 at 09:41
  • See [this](http://stackoverflow.com/questions/30619998/repeating-local-notification-daily-at-a-set-time-with-swift) and do some research. – shallowThought Jan 26 '17 at 09:44
  • You actually want to schedule the notification at midnight, so that it rings at 6 at day ? – bhakti123 Jan 26 '17 at 16:08
  • Exactly. The problem is that it could ring every day at 6 o'clock or every friday at 6 o'clock or (the biggest problem) the first two days and the next three days not and so on. – Lee Jan 26 '17 at 17:53
  • So what tells you on which day it's supposed to ring and when not? Do you not know this beforehand? – Pekka Jan 26 '17 at 18:04
  • I know it beforehand. But what if I set up a UNCalendarNotificationTrigger for every day 6 o'clock and tomorrow it should not ring because the user wants to skip this notification? – Lee Jan 26 '17 at 18:12
  • I think I could solve this when it would be possible to set a start and endday of UNCalendarNotificationTrigger – Lee Jan 26 '17 at 18:18

1 Answers1

0

To achieve this, you will have to set the notification when the user sets the alarm. Instead of using UNTimeIntervalNotificationtrigger, you should use UNCalendarNotificationTrigger. Scheduling Calendar notifications, allows you to schedule them at a particular time and then repeat them accordingly. The best thing about this is, when specifying the date components, you can specify the weekday you want to repeat the notification on for example, if you want to repeat the notification on monday set the weekday property as 2 and so on.

let calendar = Calendar.current
            var components = calendar.dateComponents([.hour, .minute], from: date)
            components.weekday = 2

            //creating trigger
            let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)

            //setting content
            let content = UNMutableNotificationContent()
            content.title = "Title"
            content.body = "Body"

            //requesting notification
            let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

            //scheduling notification
            UNUserNotificationCenter.current().add(request)

Now, if you have more than 1 weekday on which you want to repeat the notification, simply run this code in a loop and pass different weekdays.

I did make a alarm-app using this approach and it works perfectly fine.

Hope this helps. :)

bhakti123
  • 833
  • 10
  • 22