Schedules alarm notification when added
This commit is contained in:
@@ -82,7 +82,10 @@ class AddAlarmViewController: UIViewController
|
|||||||
|
|
||||||
// Add the alarm to the list and save the list
|
// Add the alarm to the list and save the list
|
||||||
Alarms.fromLocal().apply { $0.list.append(alarm) }.localSave();
|
Alarms.fromLocal().apply { $0.list.append(alarm) }.localSave();
|
||||||
|
|
||||||
|
//Schedules notification for the alarm
|
||||||
|
Notification(alarm: alarm).scheduleNotification()
|
||||||
|
|
||||||
// Dismiss this view
|
// Dismiss this view
|
||||||
self.dismiss(animated: true, completion: nil)
|
self.dismiss(animated: true, completion: nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ class DebugViewController: UIViewController
|
|||||||
//Sends a test notification
|
//Sends a test notification
|
||||||
@IBAction func sendNotification(_ sender: Any)
|
@IBAction func sendNotification(_ sender: Any)
|
||||||
{
|
{
|
||||||
Notification(alarm: Alarms.fromLocal().listEnabled[0]).sendNotification()
|
Notification(alarm: Alarms.fromLocal().listEnabled[0]).scheduleNotification()
|
||||||
}
|
}
|
||||||
|
|
||||||
@IBAction func addAlarm(_ sender: Any)
|
@IBAction func addAlarm(_ sender: Any)
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,7 +27,7 @@ class DebugViewController: UIViewController
|
|||||||
//Sends a test notification
|
//Sends a test notification
|
||||||
@IBAction func sendNotification(_ sender: Any)
|
@IBAction func sendNotification(_ sender: Any)
|
||||||
{
|
{
|
||||||
Notification(alarm: Alarms.fromLocal().listEnabled[0]).sendNotification()
|
Notification(alarm: Alarms.fromLocal().listEnabled[0]).scheduleNotification()
|
||||||
}
|
}
|
||||||
|
|
||||||
@IBAction func addAlarm(_ sender: Any)
|
@IBAction func addAlarm(_ sender: Any)
|
||||||
|
|||||||
Reference in New Issue
Block a user