2

I want to setup a daily notification system for my app. It should notify the user twice a day, once at 8:00 AM (Hey there, your set of morning doses is ready. Wanna check it out?) and once at 7:00 PM (Ssup! Your evening Dose is waiting inside). I know that by doing this i'll run out of notifications in a month since there's a 64 notifications cap (for local notifications) but by then, the app will be live and i'll be done setting up remote notifications update.

I've looked at this question: How to schedule a same local notification in swift and the .Day is all good. I just need to know how to do it twice a day at those specified times;. Thanks in advance!

EDIT: how to set the specific time on the notification is part of the problem. The NSDate API is giving me funny time intervals (sinceNow/Since1973). I just want to set it to fire at 7 PM. :-/ Can't quite seem to do it with these NSDate apis unless I'm missing something.

2 Answers 2

6

Update for Swift 4.2

import UserNotifications 

In AppDelegate confirm to UNUserNotificationCenterDelegate and in didFinishLaunchingWithOptions

let center = UNUserNotificationCenter.current() center.delegate = self; center.requestAuthorization(options: [UNAuthorizationOptions.alert, .badge, .sound]) { (granted, error) in if !granted { print("Permission Declined"); } } let content = UNMutableNotificationContent(); content.title = "HI"; content.body = "Your notification is here"; content.sound = UNNotificationSound.default; let gregorian = Calendar(identifier: Calendar.Identifier.gregorian); let now = Date(); var components = gregorian.dateComponents(in: .autoupdatingCurrent, from: now) let hours = [8,19]; for hour in hours { components.timeZone = TimeZone.current components.hour = hour; components.minute = 00; components.second = 00; let date = gregorian.date(from: components); let formatter = DateFormatter(); formatter.dateFormat = "MM-dd-yyyy HH:mm"; guard let dates = date else { return; } var fireDate: String? fireDate = formatter.string(from: dates); print("\(fireDate ?? "")"); // Just to Check let dailyTrigger = Calendar.current.dateComponents([.hour, .minute, .second], from: dates); let trigger = UNCalendarNotificationTrigger.init(dateMatching: dailyTrigger, repeats: true); let identifier = "Local Notification" let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger) center.add(request) { (error) in if let error = error { print("Error \(error.localizedDescription)") } } } 
Sign up to request clarification or add additional context in comments.

Comments

2
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. let settings = UIUserNotificationSettings(forTypes: .Badge, categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(settings) let localNotification1 = UILocalNotification() localNotification1.alertBody = "Your alert message 111" localNotification1.timeZone = NSTimeZone.defaultTimeZone() localNotification1.fireDate = self.getEightAMDate() UIApplication.sharedApplication().scheduleLocalNotification(localNotification1) let localNotification2 = UILocalNotification() localNotification2.alertBody = "Your alert message22" localNotification2.timeZone = NSTimeZone.defaultTimeZone() localNotification2.fireDate = self.getSevenPMDate() UIApplication.sharedApplication().scheduleLocalNotification(localNotification2) return true } func getEightAMDate() -> NSDate? { let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) let now: NSDate! = NSDate() let date10h = calendar.dateBySettingHour(8, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)! return date10h } func getSevenPMDate() -> NSDate? { let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) let now: NSDate! = NSDate() let date19h = calendar.dateBySettingHour(19, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)! return date19h } 

4 Comments

hey! Thanks for the reply.... quick one... was hoping you'd tell me how to set the time to be 8: 00 AM / 7 PM because the NSDate API doesn't give me an option to set the time directly. only 'timeIntevalSinceNow/1970/reference' etc. Or am i supposed to use one of those? If so how would i go about it? Thanks
Thanks bunch mate. I guess the calendar is what i didn't know about. Looked around and a few people had the same problem. Apparently the calendar API isn't updated in the documentation. But I'll have to confirm that. Anyway, You're a hero mate!
right quick before we call it done, I need to set the repeat interval to .Day for the notifications to recur on a daily basis, yeah?
It supposed be. You can set a fire time, and see if it will show the notification for the second time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.