I want to fire local notification on a particular time based just liked water notification.
Like user can set wake up time & go to bed time n drink water notification for every 2 hours so how can i set that between goto bed to wake up time my local notification won;t fire, other than that the notification will fire for every 2 hours..
Please help me.
Thanks
What you can do is to obtain the hours in between the wake up time and sleep time with the frequency of what you wish your notification to fire off.
Firstly, you would need to function to create a local notification that repeats during a particular hour everyday.
func createNotification(title: String, body: String, hour: Int, repeats: Bool, identifier: String) {
//calendar
let calendar = Calendar.current
let content = UNMutableNotificationContent()
content.title = title
content.body = body
let today = Date()
let futureDate = calendar.date(byAdding: .hour, value: hour, to: today)
let hourComponent = calendar.dateComponents([.hour], from: futureDate!)
let trigger = UNCalendarNotificationTrigger(dateMatching: hourComponent, repeats: repeats)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request, withCompletionHandler: {(error) in })
}
Secondly, place this code in your viewDidLoad() or the place you wish to set the notifications. What I am doing here is obtaining the wakeup hour and sleep hour and then finding the number of notifications that has to be made using the frequency that I want by dividing the difference in hour with the frequency.
I end of by creating a for loop that creates the notification based on the time that I add to the wakeup hour. eg. First notification fires of at 6am + 1x2 = 8am, second notification fires of at 6am + 2x2 = 10am
//calendar
let calendar = Calendar.current
//set the wakeup and sleep hour
let wakeupHour = 6 //6am
let sleepHour = 18 // 6pm
//Obtain starting time using date components
let wakeup = DateComponents(calendar: calendar, hour: wakeupHour)
//Obtain ending time using date components
let sleep = DateComponents(calendar: calendar, hour: sleepHour)
//calculate the number of hours between the two time
//obtain number of hour difference
let hourDifference = calendar.dateComponents([.hour], from: wakeup, to: sleep)
//how many hours do you want
let frequency:Int = 2
//how many notifications will there be between the wakeup and sleeptime
let numberOfNotifications:Int = hourDifference.hour! / frequency
//create the notifications
for num in 1...numberOfNotifications {
//hour that each notification will fire off
let hour = wakeupHour + frequency * num
createNotification(title: "Drink Up", body: "Drink Up", hour: hour, repeats: true, identifier: "drink\(hour)")
}
I'm still learning swift so this may not be the most efficient way of solving this problem but I hope this helps!
Related
I'm trying to make an algorithm that receives a time for the first dose of the medicine, and from there it triggers an alarm for X time from now, also selected by the user.
Example: First dose of the medicine was at 12:00 and I want to take it every 8 hours. Therefore, the first alarm must be set at 20:00 and the following, every 8 hours.
The problem is that if I set the first alarm to the desired time + 8, the alarm will not go off at 20, but yes. And if I only use it every 8 hours, I can't set the start time
func notifications(name: String, timeToTime: String, firstTime: String) {
let center = UNUserNotificationCenter.current()
getTimeForNotification(string: firstTime)
let content = UNMutableNotificationContent()
content.title = "It's time to take \(name)"
content.body = "Remember that you must ingest this medicine from \(timeToTime)"
content.sound = UNNotificationSound.default
let timeInterval = handleToInt(time: timeToTime) // * 3600
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: timeInterval,
repeats: true)
let request = UNNotificationRequest(identifier: name, content: content, trigger: trigger)
center.add(request)
}
first time is the variable that stores the time of the first dose, as a double, by the method getTimeForNotification() and timeInterval is the time, ex: 8 in 8, 12 in 12
Below is what I am trying to achieve.
Show local notification to the user about say sunrise/sunset time.
Say sunrise time is at 05:30AM and sunset time at 06:30PM.
Current time is 07:00PM, when the user enables the notification feature then every one hour show
sunrise notification with info time left for sunrise ( that is x hours and y mins left)
Once the time left for sunrise is less than 60 mins, then change the interval from one hour to say 10 mins. Finally when the time left for sunrise is less than 10 mins, then show notification for every minute. When the time for sunrise is reached repeat the same for sunset. This goes on till the user disable this feature via app settings.
Showing a countdown timer is also fine with in a notification if that doesn't drain the battery.
What I have achieved is fire a local notification at a specific time. Appreciate any guidance. I am new to IOS development.
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.categoryIdentifier = "sdf"
if let info = userInfo {
content.userInfo = info
}
content.sound = UNNotificationSound.default()
var comp = Calendar.current.dateComponents([.hour, .minute], from: date)
comp.minute = comp.minute! + 2
let trigger = UNCalendarNotificationTrigger(dateMatching: comp, repeats: false)
//let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
let request = UNNotificationRequest(identifier: "test", content: content, trigger: trigger)
center.add(request)
in my app I send a daily notification to remind the user to visit the app.
This notification is locally delivered every day at 1pm.
func scheduleNotifications() -> Void {
for notification in notifications {
let content = UNMutableNotificationContent()
content.title = notification.title
let todaysDate = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd"
let currentDay = dateFormatter.string(from: todaysDate)
let currentDayInt = Int(currentDay) ?? 0
var datComp = DateComponents()
datComp.hour = 13
datComp.minute = 00
datComp.day = currentDayInt + 1
let trigger = UNCalendarNotificationTrigger(dateMatching: datComp, repeats: true)
let request = UNNotificationRequest(identifier: notification.id, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
guard error == nil else { return }
print("Scheduling notification with id: \(notification.id) on Day \(datComp.day ?? 00) at \(datComp.hour ?? 00) - \(datComp.minute ?? 00)")
}
}
}
As you can see, I added the "current day + 1" lines because if the user opens the app before 1pm, there is no need to deliver the notification on this day.
So every time the user opens the app, I use UNUserNotificationCenter.current().removeAllPendingNotificationRequests() to remove and reschedule the notification for the next day (by recalling the function above).
My issue:
The notification should repeat every day, which it does, as long as the user opens the app.
But if the user does not open the app on one day, there will be no notification on the following days.
Is there a way to mute notifications for the current day so that I don't have to use this "current day + 1"-thing? Or does anyone have a better idea?
Thank you guys.
I think you misunderstood how repeating notifications and your datComp works here.
For example (let's use today's date: May 27)
datComp.hour = 13
datComp.minute = 00
datComp.day = currentDayInt + 1 // in our example it's 28
let trigger = UNCalendarNotificationTrigger(dateMatching: datComp, repeats: true)
means that your notification will get triggered every month on 28th, all parameters your trigger knows is hour, minute, day and by repeating it, will be triggered on every 28th at 13:00.
So the way your app works now is that you set up monthly notification starting from tomorrow, when you open the app you remove that monthly notification and reschedule it for day later. By opening every day it gives you impression that its daily notification but it's not, it's monthly. That's why if you don't open the app nothing shows up next the day, it will show up next month.
You can check my similar explanation here: (top answer there, maybe it is better worded and easier to understand)
Removing scheduled local notification
and my solution for similar problem i had here:
How to set up daily local notification for tasks but don't show it when user completes the task before
I'm trying to create application that will send local notifications every n days.
I has DailyRepeat structure that contains notification info:
struct DailyRepeat: BaseRepeat {
var title: String
var body: String
var date: Date
var day: Int
}
And method that schedule notification:
func notifyDaily(at notification: DailyRepeat) {
let content = generateContent(title: notification.title, body: notification.body)
let dateComponents = DateComponents(day: notification.day, hour: notification.date.time.hours, minute: notification.date.time.minutes)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
notificationCenter.add(request)
}
My first thought was to create UNCalendarNotificationTrigger on first fire date, than handle notification and set UNTimeIntervalNotificationTrigger, but unfortunately I cannot find the way to handle notification receive without user interaction.
Any thoughts how it should work?
As per the documentation if you want the notification to repeat you need to set repeatable constraints for the date components.
So to set a notification to run every morning at 8:30AM you would just set the hour and the minute and set it to repeat.
var date = DateComponents()
date.hour = 8
date.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
I don't think that you can use repeats to repeat every x number of days, you can set specific days of the week, or dates of the month, but not every n days.
You could set a TimeInterval of x number of days and repeat that, but getting the exact time to start it might be tricky.
I am currently making an alarm clock app and I am currently stuck at possibly just a misunderstanding. I want to launch a local notification at a specific time and repeat at a time interval. I have been getting a couple ideas and this (https://makeapppie.com/2017/01/31/how-to-repeat-local-notifications/) program is close to what I want it to be but I want it to go off at a certain time and then repeat at a single interval.
After user inputs information, the program will spit out:
timeIntervalSeconds, the interval that user wants the notifications in (in seconds)
timeStart, when the notifications will start
timeEnd, when the notifications will end if the user doesn't stop them
Any suggestions would be greatly appreciated.
Thank you!
To launch notification on specific time use below code.
let gregorian = Calendar(identifier: .gregorian)
let now = Date()
var components = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)
// Change the time to 10:00:00 in your locale
components.hour = 10
components.minute = 0
components.second = 0
let date = gregorian.date(from: components)!
let triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let request = UNNotificationRequest(identifier: CommonViewController.Identifier, content: content, trigger: trigger)
And to repeat notifications on specific time interval use below code in trigger.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: true) // it repeats after every 2 minutes
Since Apple provide only limited list of intervals for auto repeat of notifications you can try to do next to use custom intervals:
When a notification got fired you should calculate date & time for next notification and just schedule it on desired time.