-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushNotificationService.swift
More file actions
89 lines (66 loc) · 2.9 KB
/
PushNotificationService.swift
File metadata and controls
89 lines (66 loc) · 2.9 KB
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
//
// PushNotificationService.swift
// DevLog
//
// Created by opfic on 7/10/25.
//
import FirebaseAuth
import FirebaseFirestore
final class PushNotificationService {
private let store = Firestore.firestore()
/// 푸시 알림 On/Off 설정
func fetchPushNotificationEnabled() async throws -> Bool {
guard let uid = Auth.auth().currentUser?.uid else {
throw AuthError.notAuthenticated
}
let settingsRef = store.document("users/\(uid)/userData/settings")
let doc = try await settingsRef.getDocument()
if let allowPush = doc.data()?["allowPushNotification"] as? Bool { return allowPush }
throw FirestoreError.dataNotFound("allowPushNotification")
}
/// 푸시 알림 시간 설정
func fetchPushNotificationTime() async throws -> DateComponents {
guard let uid = Auth.auth().currentUser?.uid else {
throw AuthError.notAuthenticated
}
let settingsRef = store.document("users/\(uid)/userData/settings")
let doc = try await settingsRef.getDocument()
guard let hour = doc.data()?["pushNotificationHour"] as? Int else {
throw FirestoreError.dataNotFound("pushNotificationHour")
}
guard let minute = doc.data()?["pushNotificationMinute"] as? Int else {
throw FirestoreError.dataNotFound("pushNotificationMinute")
}
return DateComponents(hour: hour, minute: minute)
}
/// 푸시 알림 설정 업데이트
func updatePushNotificationSettings(isEnabled: Bool, components: DateComponents) async throws {
guard let uid = Auth.auth().currentUser?.uid else {
throw AuthError.notAuthenticated
}
let settingsRef = store.document("users/\(uid)/userData/settings")
var dict: [String: Any] = ["allowPushNotification": isEnabled]
if let hour = components.hour {
dict["pushNotificationHour"] = hour
}
if let minute = components.minute {
dict["pushNotificationMinute"] = minute
}
try await settingsRef.setData(dict, merge: true)
}
/// 푸시 알림 기록 요청
func requestNotifications() async throws -> [PushNotificationResponse] {
guard let uid = Auth.auth().currentUser?.uid else { throw AuthError.notAuthenticated }
let collection = store.collection("users/\(uid)/notifications")
let snapshot = try await collection.getDocuments()
return try snapshot.documents.compactMap { document in
try document.data(as: PushNotificationResponse.self)
}
}
/// 푸시 알림 기록 삭제
func deleteNotification(_ notificationID: String) async throws {
guard let uid = Auth.auth().currentUser?.uid else { throw AuthError.notAuthenticated }
let docRef = store.collection("users/\(uid)/notifications").document(notificationID)
try await docRef.delete()
}
}