-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotificationsInterfaceController.swift
75 lines (66 loc) · 2.42 KB
/
NotificationsInterfaceController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// NotificationsInterfaceController.swift
// FocusLoop WatchKit Extension
//
// Created by Avijeet Sachdev on 10/2/18.
// Copyright © 2018 Avijeet Sachdev. All rights reserved.
//
import WatchKit
import Foundation
import UserNotifications
class NotificationsInterfaceController: WKInterfaceController {
var hours = 1
@IBOutlet weak var timePicker: WKInterfacePicker!
override func awake(withContext context: Any?) {
super.awake(withContext: context)
var items : [WKPickerItem] = []
for hour in 1...24 {
let pickerItem = WKPickerItem()
if hour == 1 {
pickerItem.title = "\(hour) hours"
}
else {
pickerItem.title = "\(hour) hours"
}
items.append(pickerItem)
}
timePicker.setItems(items)
if let delegate = WKExtension.shared().delegate as? ExtensionDelegate {
delegate.notificationPermissions()
}
}
@IBAction func startNotif() {
for hour in 1...hours {
let content = UNMutableNotificationContent()
content.body = "Are you still productive?"
content.categoryIdentifier = "focusedCategory"
//amount of hours based off of seconds multiplied
let secondRound = 60 * 60 * hour
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(secondRound), repeats: false)
let request = UNNotificationRequest(identifier: NSUUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if error != nil {
print("Error caught")
}
else {
print("No error found")
}
}
}
UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) in
print(requests.count)
}
pop()
}
@IBAction func deleteTapped() {
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
pop() // remove current interface, revert back to previous
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
@IBAction func pickerChanged(_ value: Int) {
hours = value + 1
}
}