@@ -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