Skip to content

Commit

Permalink
Merge pull request #566 from pennlabs/notifications/fix-token-upload
Browse files Browse the repository at this point in the history
Fix uploading of notification tokens
  • Loading branch information
JHawk0224 authored Nov 9, 2024
2 parents 15b095c + 25c956b commit 04f79e8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
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

0 comments on commit 04f79e8

Please sign in to comment.