-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mobile user permission for push notifications
- Loading branch information
Showing
10 changed files
with
144 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,51 @@ | ||
import UIKit | ||
import os | ||
import UserNotifications | ||
|
||
class AppDelegate: UIResponder, UIApplicationDelegate { | ||
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { | ||
private let logger = Logger( | ||
subsystem: Bundle.main.bundleIdentifier ?? "com.trackflow", category: "AppDelegate") | ||
|
||
func application( | ||
_ application: UIApplication, | ||
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? | ||
) -> Bool { | ||
|
||
logger.info( | ||
"Application did finish launching with options: \(String(describing: launchOptions))") | ||
return true | ||
} | ||
|
||
func application( | ||
_ application: UIApplication, | ||
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data | ||
) { | ||
let tokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined() | ||
NotificationManager.shared.updateDeviceToken(tokenString) | ||
} | ||
|
||
func application( | ||
_ application: UIApplication, | ||
didFailToRegisterForRemoteNotificationsWithError error: Error | ||
) { | ||
logger.error("Failed to register for remote notifications: \(error.localizedDescription)") | ||
} | ||
|
||
private func registerForPushNotifications() { | ||
UNUserNotificationCenter.current().delegate = self | ||
|
||
UNUserNotificationCenter.current().requestAuthorization( | ||
options: [.alert, .sound, .badge] | ||
) { [weak self] granted, error in | ||
guard let self = self else { return } | ||
|
||
if granted { | ||
self.logger.info("Notification permission granted") | ||
DispatchQueue.main.async { | ||
UIApplication.shared.registerForRemoteNotifications() | ||
} | ||
} else if let error = error { | ||
self.logger.error("Error requesting notification permission: \(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,29 @@ | ||
import SwiftUI | ||
import UserNotifications | ||
|
||
class AppState: ObservableObject { | ||
@Published var status: AppStateStatus = .loggedOut | ||
@Published var jwtToken: String? = nil | ||
@Published var notificationStatus: UNAuthorizationStatus = .notDetermined | ||
|
||
func checkNotificationStatus() { | ||
UNUserNotificationCenter.current().getNotificationSettings { settings in | ||
DispatchQueue.main.async { | ||
self.notificationStatus = settings.authorizationStatus | ||
} | ||
} | ||
} | ||
|
||
func requestNotificationPermission() { | ||
UNUserNotificationCenter.current().delegate = UIApplication.shared.delegate as? UNUserNotificationCenterDelegate | ||
|
||
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in | ||
DispatchQueue.main.async { | ||
self.checkNotificationStatus() | ||
if granted { | ||
UIApplication.shared.registerForRemoteNotifications() | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Foundation | ||
import os | ||
|
||
class NotificationManager { | ||
static let shared = NotificationManager() | ||
private var deviceToken: String? | ||
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "com.trackflow", category: "NotificationManager") | ||
|
||
private init() {} | ||
|
||
func updateDeviceToken(_ token: String) { | ||
deviceToken = token | ||
logger.info("Received new device token") | ||
sendTokenToServer() | ||
} | ||
|
||
private func sendTokenToServer() { | ||
guard let token = deviceToken, | ||
let jwtToken = UserDefaults.standard.string(forKey: "jwt_token") | ||
else { | ||
logger.error("Missing device token or JWT token") | ||
return | ||
} | ||
|
||
APIManager.shared.updateDeviceToken(token: jwtToken, deviceToken: token) { result in | ||
switch result { | ||
case .success: | ||
self.logger.info("Successfully registered device token with server") | ||
case .failure(let error): | ||
self.logger.error("Failed to register device token: \(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>aps-environment</key> | ||
<string>development</string> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters