Skip to content

Commit

Permalink
Improve push notification handle on cold start
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed Sep 26, 2024
1 parent 15ef8be commit b4b4128
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
11 changes: 2 additions & 9 deletions Gem/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ class AppDelegate: NSObject, UIApplicationDelegate, UIWindowSceneDelegate {
fatalError()
}

if let userInfo = launchOptions?[.remoteNotification] as? [String: AnyObject] {
NotificationService.main.handleUserInfoOnLaunch(userInfo)
}

return true
}

Expand All @@ -102,7 +98,7 @@ class AppDelegate: NSObject, UIApplicationDelegate, UIWindowSceneDelegate {
try await DeviceService(subscriptionsService: .main, walletStore: .main).update()
}
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
NSLog("didFailToRegisterForRemoteNotificationsWithError error: \(error)")
}
Expand Down Expand Up @@ -132,13 +128,10 @@ class AppDelegate: NSObject, UIApplicationDelegate, UIWindowSceneDelegate {
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.badge, .banner, .list, .sound])

#if DEBUG
NotificationService.main.handleUserInfo(notification.request.content.userInfo)
#endif
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
NotificationService.main.handleUserInfo(response.notification.request.content.userInfo)
completionHandler()
}
}
4 changes: 0 additions & 4 deletions Gem/Core/Services/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ class NotificationService {
notifications = []
}

func handleUserInfoOnLaunch(_ userInfo: [AnyHashable : Any]) {
handleUserInfo(userInfo)
}

func handleUserInfo(_ userInfo: [AnyHashable : Any]) {
do {
let notification = try PushNotification(from: userInfo)
Expand Down
18 changes: 10 additions & 8 deletions Gem/Core/Views/MainTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ struct MainTabView: View {
.onChange(of: notificationService.notifications) { _, newValue in
onReceiveNotifications(newValue)
}
.onAppear {
//Needed here because .onChange(of: notificationService.notifications) not trigger on cold start
onReceiveNotifications(notificationService.notifications)
}
}
}

Expand Down Expand Up @@ -123,18 +127,16 @@ extension MainTabView {
private func onReceiveNotification(notification: PushNotification) {
do {
switch notification {
case .transaction(let transaction):
let walletIndex = transaction.walletIndex

case .transaction(let walletIndex, let assetId):
//select wallet
if walletIndex != keystore.currentWallet?.index {
keystore.setCurrentWalletIndex(walletIndex.asInt)
if walletIndex != keystore.currentWallet?.index.asInt {
keystore.setCurrentWalletIndex(walletIndex)
}

let asset = try walletsService.assetsService.getAsset(for: try AssetId(id: transaction.assetId))
let asset = try walletsService.assetsService.getAsset(for: assetId)
navigationStateManager.wallet.append(Scenes.Asset(asset: asset))
case .priceAlert(let priceAlert):
let asset = try walletsService.assetsService.getAsset(for: try AssetId(id: priceAlert.assetId))
case .priceAlert(let assetId):
let asset = try walletsService.assetsService.getAsset(for: assetId)
navigationStateManager.wallet.append(Scenes.Price(asset: asset))
case .buyAsset(let assetId):
let asset = try walletsService.assetsService.getAsset(for: assetId)
Expand Down
12 changes: 7 additions & 5 deletions Packages/Primitives/Sources/PushNotification+Primitives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import Foundation

public enum PushNotification: Equatable {
case transaction(PushNotificationTransaction)
case priceAlert(PriceAlert)
case transaction(walletIndex: Int, AssetId)
case priceAlert(AssetId)
case buyAsset(AssetId)
case test
case unknown
Expand All @@ -23,10 +23,12 @@ public enum PushNotification: Equatable {
switch type {
case .transaction:
let transaction = try decoder.decode(PushNotificationTransaction.self, from: data)
self = .transaction(transaction)
let assetId = try AssetId(id: transaction.assetId)
self = .transaction(walletIndex: transaction.walletIndex.asInt, assetId)
case .priceAlert:
let priceAlert = try decoder.decode(PriceAlert.self, from: data)
self = .priceAlert(priceAlert)
let priceAlert = try decoder.decode(PushNotificationPriceAlert.self, from: data)
let assetId = try AssetId(id: priceAlert.assetId)
self = .priceAlert(assetId)
case .buyAsset:
let buyAsset = try decoder.decode(PushNotificationBuyAsset.self, from: data)
let assetId = try AssetId(id: buyAsset.assetId)
Expand Down
8 changes: 8 additions & 0 deletions Packages/Primitives/Sources/PushNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public struct PushNotificationPayloadType: Codable, Equatable {
}
}

public struct PushNotificationPriceAlert: Codable, Equatable {
public let assetId: String

public init(assetId: String) {
self.assetId = assetId
}
}

public struct PushNotificationTransaction: Codable, Equatable {
public let walletIndex: Int32
public let assetId: String
Expand Down

0 comments on commit b4b4128

Please sign in to comment.