I wish to add and ship an iOS native push notification to a selected date and wish to schedule it by weekday for a selected time (hour, minute). I needed to ship this reminder weekday-wise however wish to take away at present’s if use already took/achieved his at present’s objective. I’ve tried with the code under however didn’t ship it, please verify the code under.
import UIKit
import UserNotifications
public extension Date {
func midday(utilizing calendar: Calendar = .present) -> Date {
calendar.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
func day(utilizing calendar: Calendar = .present) -> Int {
calendar.element(.day, from: self)
}
func weekday(utilizing calendar: Calendar = .present) -> Int {
calendar.element(.weekday, from: self)
}
func hour(utilizing calendar: Calendar = .present) -> Int {
calendar.element(.hour, from: self)
}
func minute(utilizing calendar: Calendar = .present) -> Int {
calendar.element(.minute, from: self)
}
func second(utilizing calendar: Calendar = .present) -> Int {
calendar.element(.second, from: self)
}
func including(_ element: Calendar.Element, worth: Int, utilizing calendar: Calendar = .present) -> Date {
calendar.date(byAdding: element, worth: worth, to: self)!
}
func monthSymbol(utilizing calendar: Calendar = .present) -> String {
calendar.monthSymbols[calendar.component(.month, from: self)-1]
}
func addDay(n: Int) -> Date {
let calendar = Calendar.present
return calendar.date(byAdding: .day, worth: n, to: self)!
}
}
class ViewController: UIViewController, UNUserNotificationCenterDelegate {
non-public let categoryID = "Class"
override func viewDidLoad() {
tremendous.viewDidLoad()
UNUserNotificationCenter.present().delegate = self
let actionShowSomething = UNNotificationAction(identifier: "ShowSomething", title: "Present One thing", choices: [])
let class = UNNotificationCategory(identifier: categoryID, actions: [actionShowSomething], intentIdentifiers: [], choices: [])
UNUserNotificationCenter.present().setNotificationCategories()
}
@IBAction func addNotification(_ sender: Any) {
UNUserNotificationCenter.present().removeAllPendingNotificationRequests()
// Request Notification Settings
UNUserNotificationCenter.present().getNotificationSettings { (notificationSettings) in
change notificationSettings.authorizationStatus {
case .notDetermined:
self.requestAuthorization(completionHandler: { (success) in
guard success else { return }
self.scheduleLocalNotification()
})
case .approved:
self.scheduleLocalNotification()
case .denied:
print("Software Not Allowed to Show Notifications")
case .provisional:
break
case .ephemeral:
break
@unknown default:
break
}
}
}
non-public func requestAuthorization(completionHandler: @escaping (_ success: Bool) -> ()) {
// Request Authorization
UNUserNotificationCenter.present().requestAuthorization(choices: [.alert, .sound, .badge]) { (success, error) in
if let error = error {
print("Request Authorization Failed ((error), (error.localizedDescription))")
}
completionHandler(success)
}
}
// MARK: - ScheduleLocalNotification
non-public func scheduleLocalNotification() {
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Title"
notificationContent.subtitle = "Subtitle"
notificationContent.physique = "Physique"
notificationContent.categoryIdentifier = categoryID
var dateComponents = DateComponents()
let calendar = Calendar.present
dateComponents.calendar = calendar
let date = Date()
let second = calendar.element(.second, from: date)
dateComponents.weekday = 2 + 7 // Monday = 2
dateComponents.second = second + 30
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let notificationRequest = UNNotificationRequest(identifier: "cocoacasts_local_notification", content material: notificationContent, set off: notificationTrigger)
UNUserNotificationCenter.present().add(notificationRequest) { (error) in
if let error = error {
print("Unable to Add Notification Request ((error), (error.localizedDescription))")
}
}
}
}