diff --git a/DDooing/DDooing/DDooing.entitlements b/DDooing/DDooing/DDooing.entitlements index 0a83c30..544e831 100644 --- a/DDooing/DDooing/DDooing.entitlements +++ b/DDooing/DDooing/DDooing.entitlements @@ -2,6 +2,8 @@ + aps-environment + development com.apple.developer.applesignin Default diff --git a/DDooing/DDooing/Info.plist b/DDooing/DDooing/Info.plist index 9bdac0a..3655ad9 100644 --- a/DDooing/DDooing/Info.plist +++ b/DDooing/DDooing/Info.plist @@ -2,6 +2,8 @@ + FirebaseAppDelegateProxyEnabled + NSAppTransportSecurity NSExceptionDomains diff --git a/DDooing/DDooing/Views/Auth/RandomCodeView.swift b/DDooing/DDooing/Views/Auth/RandomCodeView.swift index 683abdd..7a96b88 100644 --- a/DDooing/DDooing/Views/Auth/RandomCodeView.swift +++ b/DDooing/DDooing/Views/Auth/RandomCodeView.swift @@ -13,6 +13,8 @@ struct RandomCodeView: View { @State private var isConnectionMode = true @State private var code = "" @State private var randomCode = "" + @AppStorage("userDeviceToken") private var userDeviceToken: String = "" + var body: some View { NavigationStack{ @@ -88,6 +90,7 @@ struct RandomCodeView: View { sendRandomCodeToFirebase(for: user, with: randomCode) viewModel.observeUserConnectionStatus(userId: user.uid) } + print("Device Token in HomeView: \(userDeviceToken)") } .navigationBarBackButtonHidden(true) } @@ -95,25 +98,26 @@ struct RandomCodeView: View { let lettersAndDigits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" return String((0.. private var notificationDataList: [NotificationDataModel] - @State var pushMessage = "" - @State var deviceToken = "" + @State var partnerDeviceToken = "" init(partnerUID: String?) { self.partnerUID = partnerUID if messages.randomElement() != nil { - _randomMessages = State(initialValue: randomMessages) + _pushMessage = State(initialValue: pushMessage) } else { - _randomMessages = State(initialValue: "") + _pushMessage = State(initialValue: "") } } @@ -66,7 +65,9 @@ struct HomeView: View { ForEach(messages) { mess in if mess.isStarred { Button (action: { - sendMessage(messageText: mess.message, isStarred: true) + pushMessage = mess.message + sendMessage(messageText: pushMessage, isStarred: true) + fetchAccessTokenAndSendPushNotification() }, label: { Text(mess.message) }) @@ -78,10 +79,11 @@ struct HomeView: View { if !isLongPressed { print("짧게누름") if let randomMessage = messages.randomElement() { - randomMessages = randomMessage.message + pushMessage = randomMessage.message } - saveRandomMessage() print("메시지 입력") + sendMessage(messageText: pushMessage, isStarred: false) + fetchAccessTokenAndSendPushNotification() } isLongPressed = false } @@ -97,13 +99,48 @@ struct HomeView: View { name = fetchedName } + + } + } + } + func fetchPartnerDeviceToken(completion: @escaping (String) -> Void) { + let db = Firestore.firestore() + db.collection("Users").document(partnerUID).getDocument { document, error in + if let document = document, document.exists { + partnerDeviceToken = document.data()?["deviceToken"] as? String ?? "Unknown" + completion(partnerDeviceToken) + } else { + completion("Unknown") } } } - func saveRandomMessage() { - guard let partnerUID = partnerUID else { return } - sendMessage(messageText: randomMessages, isStarred: false) + func fetchPartnerConnectedNickname(completion: @escaping (String) -> Void) { + let db = Firestore.firestore() + db.collection("Users").document(partnerUID).getDocument { document, error in + if let document = document, document.exists { + partnerName = document.data()?["ConnectedNickname"] as? String ?? "Unknown" + completion(partnerName) + } else { + completion("Unknown") + } + } + } + private func fetchMyConnectedNickname(completion: @escaping (String) -> Void) { + let db = Firestore.firestore() + guard let currentUid = Auth.auth().currentUser?.uid else { + completion("Unknown") + return + } + + db.collection("Users").document(currentUid).getDocument { document, error in + if let document = document, document.exists { + name = document.data()?["ConnectedNickname"] as? String ?? "Unknown" + completion(name) + } else { + completion("Unknown") + } + } } func sendMessage(messageText: String, isStarred: Bool) { @@ -111,52 +148,120 @@ struct HomeView: View { guard let currentUid = Auth.auth().currentUser?.uid else { return } - let currenrUserRef = db.collection("Received-Messages") - .document(currentUid).collection(partnerUID).document() - - let PartnerRef = db.collection("Received-Messages") - .document(partnerUID).collection(currentUid) + let currentUserRef = db.collection("Received-Messages") + .document(partnerUID).collection(currentUid).document() // let recentCurrentUserRef = db.collection("Received-Messages") -// .document(currentUid).collection("recent-messages") -// .document(partnerUID) +// .document(partnerUID).collection("recent-messages") +// .document(currentUid) - let recentPartnerRef = db.collection("Received-Messages") - .document(partnerUID).collection("recent-messages") - .document(currentUid) - - let messageId = currenrUserRef.documentID + let messageId = currentUserRef.documentID let messageData: [String: Any] = [ - "fromId": currentUid, - "toId": partnerUID!, + "fromId": partnerUID!, + "toId": currentUid, "messageText": messageText, "timeStamp": Timestamp(date: Date()), "isStarred": isStarred, "messageId": messageId ] -// currenrUserRef.setData(messageData) - PartnerRef.document(messageId).setData(messageData) + currentUserRef.setData(messageData) // recentCurrentUserRef.setData(messageData) - recentPartnerRef.setData(messageData) } - private func fetchMyConnectedNickname(completion: @escaping (String) -> Void) { - let db = Firestore.firestore() - guard let currentUid = Auth.auth().currentUser?.uid else { - completion("Unknown") + func fetchAccessTokenAndSendPushNotification() { + fetchPartnerDeviceToken { fetchedtoken in + partnerDeviceToken = fetchedtoken + } + fetchPartnerConnectedNickname { fetchedName in + partnerName = fetchedName + } + + // 서버로부터 OAuth 2.0 액세스 토큰 가져오기 + guard let url = URL(string: "") else { + print("Invalid URL for token") return } + + var request = URLRequest(url: url) + request.httpMethod = "GET" - db.collection("Users").document(currentUid).getDocument { document, error in - if let document = document, document.exists { - name = document.data()?["ConnectedNickname"] as? String ?? "Unknown" - completion(name) + let session = URLSession(configuration: .default) + session.dataTask(with: request) { data, response, err in + if let err = err { + print(err.localizedDescription) + return + } + guard let data = data else { + print("No data received") + return + } + + // 서버로부터 받은 응답을 문자열로 변환하여 출력 + if let accessToken = String(data: data, encoding: .utf8) { + print("Access Token String: \(accessToken)") + sendPushNotification(with: accessToken) } else { - completion("Unknown") + print("Invalid token response") } + }.resume() + } + + func sendPushNotification(with accessToken: String) { + guard !accessToken.isEmpty else { + print("Access token is empty") + return + } + + // HTTP v1 API의 엔드포인트 URL + guard let url = URL(string: "https://fcm.googleapis.com/v1/projects/ddooing-8881b/messages:send") else { + print("Invalid URL for FCM") + return } + print("partnerdevicetoken >>> \(partnerDeviceToken)") + print("pushMessage >>> \(pushMessage)") + print("parname >>> \(partnerName)") + let json: [String: Any] = [ + "message": [ + "token": partnerDeviceToken, + "notification": [ + "body": pushMessage, + "title": partnerName + ] + ] + ] + + var request = URLRequest(url: url) + request.httpMethod = "POST" + request.httpBody = try? JSONSerialization.data(withJSONObject: json, options: [.prettyPrinted]) + request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization") + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + + let session = URLSession(configuration: .default) + session.dataTask(with: request) { data, response, err in + if let err = err { + print("Error sending push notification: \(err.localizedDescription)") + return + } + + guard let httpResponse = response as? HTTPURLResponse else { + print("Invalid response") + return + } + + print("Push notification response status code: \(httpResponse.statusCode)") + + if let data = data, let responseBody = String(data: data, encoding: .utf8) { + print("Response Body: \(responseBody)") + } + + if httpResponse.statusCode == 200 { + print("Push notification sent successfully") + } else { + print("Failed to send push notification") + } + }.resume() } }