Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uploading of notification tokens #566

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions PennMobile/General/Networking + Analytics/UserDBManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
1 change: 0 additions & 1 deletion PennMobile/Notifications/Model/NotificationAPIModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ import Foundation
struct GetNotificationID: Codable, Identifiable {
let id: Int
let kind: String
let dev: Bool
let token: String
}
5 changes: 5 additions & 0 deletions PennMobile/Setup + Navigation/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion PennMobile/Setup + Navigation/PennMobile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ struct PennMobile: App {

// Register to receive delegate actions from rich notifications
UNUserNotificationCenter.current().delegate = delegate
UIApplication.shared.registerForRemoteNotifications()

FirebaseApp.configure()

Expand Down