@@ -9,71 +9,65 @@ import Foundation
9
9
import UserNotifications
10
10
11
11
class NotificationManager {
12
-
13
- // MARK: - Properties
14
-
15
- static let shared = NotificationManager ( )
12
+ // MARK: - Properties
16
13
17
- // MARK: - Methods
14
+ static let shared = NotificationManager ( )
18
15
19
- /// νμΌ 11μμ μ±μ μ μ
μ μ λνλ νΈμ μλ¦Όμ λ°μ‘νλ λ©μλ
20
- ///
21
- /// - Title : π€ μ€λ λ°₯ λ λ¨Ήμ§β¦
22
- /// - Body : μ€λμ νμμ νμΈν΄λ³΄μΈμ!
23
- func scheduleWeekday11AMNotification( ) {
24
- let center = UNUserNotificationCenter . current ( )
16
+ // MARK: - Methods
25
17
26
- // μλ¦Ό μ½ν
μΈ μ€μ
27
- let content = UNMutableNotificationContent ( )
18
+ /// νμΌ 11μμ μ±μ μ μ
μ μ λνλ νΈμ μλ¦Όμ λ°μ‘νλ λ©μλ
19
+ ///
20
+ /// - Title : π€ μ€λ λ°₯ λ λ¨Ήμ§β¦
21
+ /// - Body : μ€λμ νμμ νμΈν΄λ³΄μΈμ!
22
+ func scheduleWeekday11AMNotification( ) {
23
+ let center = UNUserNotificationCenter . current ( )
24
+
25
+ // μλ¦Ό μ½ν
μΈ μ€μ
26
+ let content = UNMutableNotificationContent ( )
28
27
29
- content. title = TextLiteral . Notification. dailyWeekdayNotificationTitle
30
- content. body = TextLiteral . Notification. dailyWeekdayNotificationBody
31
- content. sound = . default
28
+ content. title = TextLiteral . Notification. dailyWeekdayNotificationTitle
29
+ content. body = TextLiteral . Notification. dailyWeekdayNotificationBody
30
+ content. sound = . default
32
31
33
- // λ°λ³΅ν μμΌ λ° μκ° μ€μ (νμΌ μ€μ 11μ)
34
- let weekdays = [ 2 , 3 , 4 , 5 , 6 ] // μ, ν, μ, λͺ©, κΈ (Calendarμμ 1μ΄ μΌμμΌ)
32
+ // λ°λ³΅ν μμΌ λ° μκ° μ€μ (νμΌ μ€μ 11μ)
33
+ let weekdays = [ 2 , 3 , 4 , 5 , 6 ] // μ, ν, μ, λͺ©, κΈ (Calendarμμ 1μ΄ μΌμμΌ)
35
34
36
- for weekday in weekdays {
37
- var dateComponents = DateComponents ( )
38
- dateComponents. hour = 11
39
- dateComponents. minute = 0
40
- dateComponents. weekday = weekday
35
+ for weekday in weekdays {
36
+ var dateComponents = DateComponents ( )
37
+ dateComponents. hour = 11
38
+ dateComponents. minute = 0
39
+ dateComponents. weekday = weekday
41
40
42
- let trigger = UNCalendarNotificationTrigger ( dateMatching: dateComponents, repeats: true )
41
+ let trigger = UNCalendarNotificationTrigger ( dateMatching: dateComponents, repeats: true )
43
42
44
- // κ³ μ ν μλ³μλ₯Ό μν΄ weekdayλ₯Ό μ¬μ©
45
- let identifier = " weekdayNotification- \( weekday) "
46
- let request = UNNotificationRequest (
47
- identifier: identifier, content: content, trigger: trigger)
43
+ // κ³ μ ν μλ³μλ₯Ό μν΄ weekdayλ₯Ό μ¬μ©
44
+ let identifier = " weekdayNotification- \( weekday) "
45
+ let request = UNNotificationRequest (
46
+ identifier: identifier, content: content, trigger: trigger)
47
+
48
+ // μλ¦Ό λ±λ‘
49
+ center. add ( request) { error in
50
+ if let error = error {
51
+ print ( " μλ¦Ό λ±λ‘ κ° μλ¬ λ©μμ§: \( error. localizedDescription) " )
52
+ }
53
+ }
54
+ }
55
+ }
48
56
49
- // μλ¦Ό λ±λ‘
50
- center. add ( request) { error in
51
- if let error = error {
52
- print ( " μλ¦Ό λ±λ‘ κ° μλ¬ λ©μμ§: \( error. localizedDescription) " )
53
- }
54
- }
55
- }
56
- }
57
-
58
57
/// νμΌ 11μμ μ±μ μ μ
μ μ λνλ νΈμ μλ¦Όμ μ·¨μνλ λ©μλ
59
58
func cancelWeekday11AMNotification( ) {
60
59
let weekday = [ 2 , 3 , 4 , 5 , 6 ]
61
60
let identifier = " weekdayNotification- \( weekday) "
62
-
61
+
63
62
let center = UNUserNotificationCenter . current ( )
64
63
center. removePendingNotificationRequests ( withIdentifiers: [ identifier] )
65
64
}
66
65
67
- /// μ± μ€ν μ μλ¦Ό λ°μ‘ κΆνμ μμ²νλ νμ
νΈμΆ λ©μλ
68
- func requestNotificationPermission( ) {
69
- UNUserNotificationCenter . current ( ) . requestAuthorization ( options: [ . alert, . sound, . badge] ) {
70
- granted, error in
71
- if granted {
72
- print ( " μλ¦Ό κΆν μΉμΈλ¨ " )
73
- } else {
74
- print ( " μλ¦Ό κΆν κ±°λΆλ¨ " )
75
- }
76
- }
77
- }
78
-
66
+ /// μ± μ€ν μ μλ¦Ό λ°μ‘ κΆνμ μμ²νλ νμ
νΈμΆ λ©μλ
67
+ func requestNotificationPermission( completion: @escaping ( _ granted : Bool ) -> Void ) {
68
+ UNUserNotificationCenter . current ( ) . requestAuthorization ( options: [ . alert, . sound, . badge] ) {
69
+ granted, _ in
70
+ completion ( granted)
71
+ }
72
+ }
79
73
}
0 commit comments