Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
deleted 1 character in body
Source Link
Ajo
  • 416
  • 6
  • 12
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 = NSTimeZoneTimeZone.systemcurrent 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)") } } } 
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 = NSTimeZone.system 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)") } } } 
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)") } } } 
Source Link
Ajo
  • 416
  • 6
  • 12

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 = NSTimeZone.system 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)") } } }