From 25c956b675184133ee7d8bab9d5f63cfbece7f1a Mon Sep 17 00:00:00 2001 From: Anthony Li Date: Fri, 8 Nov 2024 18:33:47 -0500 Subject: [PATCH] Fix token uploading --- .../UserDBManager.swift | 43 +++++++++++++------ .../Model/NotificationAPIModel.swift | 1 - .../Setup + Navigation/AppDelegate.swift | 5 +++ .../Setup + Navigation/PennMobile.swift | 1 - 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/PennMobile/General/Networking + Analytics/UserDBManager.swift b/PennMobile/General/Networking + Analytics/UserDBManager.swift index 263d9277a..e411326e6 100755 --- a/PennMobile/General/Networking + Analytics/UserDBManager.swift +++ b/PennMobile/General/Networking + Analytics/UserDBManager.swift @@ -504,18 +504,37 @@ extension UserDBManager { // Updates device token. func savePushNotificationDeviceToken(deviceToken: String, notifId: Int, _ completion: (() -> Void)? = nil) { - let url = "https://pennmobile.org/api/user/notifications/tokens/\(notifId)" - var params: [String: Any] = [ - "kind": "IOS", - "token": deviceToken, - "dev": false - ] - - #if DEBUG - params["dev"] = true - #endif - makePostRequestWithAccessToken(url: url, params: params) { (_, _, _) in - completion?() + Task { + defer { completion?() } + + struct DeviceTokenRequestBody: Encodable { + var kind: String + var token: String + var dev: Bool + } + + guard let url = URL(string: "https://pennmobile.org/api/user/notifications/tokens/\(notifId)/") else { + return + } + + var body = DeviceTokenRequestBody(kind: "IOS", token: deviceToken, dev: false) + +#if DEBUG + body.dev = true +#endif + + guard let token = await OAuth2NetworkManager.instance.getAccessTokenAsync() else { + return + } + + var request = URLRequest(url: url, accessToken: token) + request.httpMethod = "PUT" + + // If serializing a simple JSON object fails something is really wrong + request.httpBody = try! JSONEncoder().encode(body) + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + + _ = try? await URLSession.shared.data(for: request) } } diff --git a/PennMobile/Notifications/Model/NotificationAPIModel.swift b/PennMobile/Notifications/Model/NotificationAPIModel.swift index 6cc535623..2e84c53c2 100644 --- a/PennMobile/Notifications/Model/NotificationAPIModel.swift +++ b/PennMobile/Notifications/Model/NotificationAPIModel.swift @@ -11,6 +11,5 @@ import Foundation struct GetNotificationID: Codable, Identifiable { let id: Int let kind: String - let dev: Bool let token: String } diff --git a/PennMobile/Setup + Navigation/AppDelegate.swift b/PennMobile/Setup + Navigation/AppDelegate.swift index 318739ff9..c2d60b8b1 100755 --- a/PennMobile/Setup + Navigation/AppDelegate.swift +++ b/PennMobile/Setup + Navigation/AppDelegate.swift @@ -17,6 +17,11 @@ import PennMobileShared class AppDelegate: UIResponder, UIApplicationDelegate { var tabBarController: TabBarController! + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { + application.registerForRemoteNotifications() + return true + } func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { diff --git a/PennMobile/Setup + Navigation/PennMobile.swift b/PennMobile/Setup + Navigation/PennMobile.swift index debbd29c5..f021dc994 100644 --- a/PennMobile/Setup + Navigation/PennMobile.swift +++ b/PennMobile/Setup + Navigation/PennMobile.swift @@ -29,7 +29,6 @@ struct PennMobile: App { // Register to receive delegate actions from rich notifications UNUserNotificationCenter.current().delegate = delegate - UIApplication.shared.registerForRemoteNotifications() FirebaseApp.configure()