-
Notifications
You must be signed in to change notification settings - Fork 47
/
LocalNotificationManager.swift
89 lines (76 loc) · 3.09 KB
/
LocalNotificationManager.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// LocalNotificationManager.swift
// Local_Notification
//
// Created by JungpyoHong on 6/21/21.
//
import Foundation
import UserNotifications
import UIKit
struct LocalNotification {
var id: String
var title: String
var body: String
}
enum LocalNotificationDurationType {
case days
case hours
case minutes
case seconds
}
struct LocalNotificationManager {
static private var notifications = [LocalNotification]()
static private func requestPermission() -> Void {
UNUserNotificationCenter
.current()
.requestAuthorization(options: [.alert, .badge, .alert]) { granted, error in
if granted == true && error == nil {
// Permission accept
}
}
}
static private func addNotification(title: String, body: String) -> Void {
notifications.append(LocalNotification(id: UUID().uuidString, title: title, body: body))
}
static private func scheduleNotifications(_ durationInSeconds: Int, repeats: Bool, userInfo: [AnyHashable : Any]) {
UIApplication.shared.applicationIconBadgeNumber = 0
for notification in notifications {
let content = UNMutableNotificationContent()
content.title = notification.title
content.body = notification.body
content.sound = UNNotificationSound.default
content.badge = NSNumber(value: UIApplication.shared.applicationIconBadgeNumber + 1)
content.userInfo = userInfo
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(durationInSeconds), repeats: repeats)
let request = UNNotificationRequest(identifier: notification.id, content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) { error in
guard error == nil else { return }
print("Scheduling notification with id: \(notification.id)")
}
}
notifications.removeAll()
}
static private func scheduleNotifications(_ duration: Int, of type: LocalNotificationDurationType, repeats: Bool, userInfo: [AnyHashable : Any]) {
var seconds = 0
switch type {
case .seconds:
seconds = duration
case .minutes:
seconds = duration * 60
case .hours:
seconds = duration * 60 * 60
case .days:
seconds = duration * 60 * 60 * 24
}
scheduleNotifications(seconds, repeats: repeats, userInfo: userInfo)
}
static func cancel() {
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
}
static func setNotification(_ duration: Int, of type: LocalNotificationDurationType, repeats: Bool, title: String, body: String, userInfo: [AnyHashable : Any]) {
requestPermission()
addNotification(title: title, body: body)
scheduleNotifications(duration, of: type, repeats: repeats, userInfo: userInfo)
}
}