From 4f31b80707b67704af6d4f5bb09043ed33755dd0 Mon Sep 17 00:00:00 2001 From: Aaron Date: Sun, 24 Jan 2021 17:40:05 -0500 Subject: [PATCH] Schedules alarm notification when added --- ProjectClock/AddAlarmViewController.swift | 5 ++- ProjectClock/DebugViewController.swift | 2 +- ProjectClock/Notification.swift | 45 +++++++++++++++++++++++ ProjectClock/TestingViewController.swift | 2 +- 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 ProjectClock/Notification.swift diff --git a/ProjectClock/AddAlarmViewController.swift b/ProjectClock/AddAlarmViewController.swift index d6ad026..bcab035 100644 --- a/ProjectClock/AddAlarmViewController.swift +++ b/ProjectClock/AddAlarmViewController.swift @@ -82,7 +82,10 @@ class AddAlarmViewController: UIViewController // Add the alarm to the list and save the list Alarms.fromLocal().apply { $0.list.append(alarm) }.localSave(); - + + //Schedules notification for the alarm + Notification(alarm: alarm).scheduleNotification() + // Dismiss this view self.dismiss(animated: true, completion: nil) } diff --git a/ProjectClock/DebugViewController.swift b/ProjectClock/DebugViewController.swift index a7c145a..bd081e5 100644 --- a/ProjectClock/DebugViewController.swift +++ b/ProjectClock/DebugViewController.swift @@ -27,7 +27,7 @@ class DebugViewController: UIViewController //Sends a test notification @IBAction func sendNotification(_ sender: Any) { - Notification(alarm: Alarms.fromLocal().listEnabled[0]).sendNotification() + Notification(alarm: Alarms.fromLocal().listEnabled[0]).scheduleNotification() } @IBAction func addAlarm(_ sender: Any) diff --git a/ProjectClock/Notification.swift b/ProjectClock/Notification.swift new file mode 100644 index 0000000..4d9e4d1 --- /dev/null +++ b/ProjectClock/Notification.swift @@ -0,0 +1,45 @@ +// +// Notification.swift +// ProjectClock +// +// Created by Aaron Saporito on 1/24/21. +// + +import Foundation +import UserNotifications + +class Notification { + + init(alarm: Alarm) { + self.alarm = alarm + } + + var alarm: Alarm + + func scheduleNotification() { + let content = UNMutableNotificationContent() + + //Date formatting to string + let today = Date() + let formatter1 = DateFormatter() + formatter1.dateFormat = "MMM d, h:mm" + + //Notification content + content.title = alarm.text + content.subtitle = formatter1.string(from: today) + content.body = "Wake method: \(alarm.wakeMethod.name)" + + // Notification image content + let imageName = "clock" + guard let imageURL = Bundle.main.url(forResource: imageName, withExtension: "png") else { return } + let attachment = try! UNNotificationAttachment(identifier: imageName, url: imageURL, options: .none) + content.attachments = [attachment] + + // Scheduels alarm notification for proper time + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: alarm.nextActivate!.timeIntervalSince(Date()), repeats: false) + let request = UNNotificationRequest(identifier: "notification.id.01", content: content, trigger: trigger) + + // Sends notification + UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) + } +} diff --git a/ProjectClock/TestingViewController.swift b/ProjectClock/TestingViewController.swift index a7c145a..bd081e5 100644 --- a/ProjectClock/TestingViewController.swift +++ b/ProjectClock/TestingViewController.swift @@ -27,7 +27,7 @@ class DebugViewController: UIViewController //Sends a test notification @IBAction func sendNotification(_ sender: Any) { - Notification(alarm: Alarms.fromLocal().listEnabled[0]).sendNotification() + Notification(alarm: Alarms.fromLocal().listEnabled[0]).scheduleNotification() } @IBAction func addAlarm(_ sender: Any)