Skip to content

Commit

Permalink
feat/Hold opened push notifications until delegate is set
Browse files Browse the repository at this point in the history
  • Loading branch information
wassil committed Jan 6, 2021
1 parent 21432a3 commit 8d6ca2c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
10 changes: 9 additions & 1 deletion ExponeaSDK/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,23 @@ extension AppDelegate: PushNotificationManagerDelegate {
) {
Exponea.logger.log(
.verbose,
message: "Alert push received, " +
message: "Alert push opened, " +
"action \(action), value: \(String(describing: value)), extraData \(String(describing: extraData))"
)
showAlert(
"Push notification opened",
"action \(action), value: \(String(describing: value)), extraData \(String(describing: extraData))"
)
}

func silentPushNotificationReceived(extraData: [AnyHashable: Any]?) {
Exponea.logger.log(
.verbose,
message: "Silent push received, extraData \(String(describing: extraData))"
)
showAlert(
"Silent push received",
"extraData \(String(describing: extraData))"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,32 @@ final class PushNotificationManager: NSObject, PushNotificationManagerType {
private var lastTokenTrackDate: Date
private var pushNotificationSwizzler: PushNotificationSwizzler?

internal weak var delegate: PushNotificationManagerDelegate?
// some push notification can be received before the delegate is set, we'll store them and call delegate once set
internal var pendingOpenedPushes: [PushOpenedData] = []
private weak var delegateValue: PushNotificationManagerDelegate?
internal var delegate: PushNotificationManagerDelegate? {
get {
return delegateValue
}
set {
delegateValue = newValue
guard let delegateValue = delegateValue else {
return
}
pendingOpenedPushes.forEach {
if $0.silent {
delegateValue.silentPushNotificationReceived(extraData: $0.extraData)
} else {
delegateValue.pushNotificationOpened(
with: $0.actionType,
value: $0.actionValue,
extraData: $0.extraData
)
}
}
pendingOpenedPushes = []
}
}

var didReceiveSelfPushCheck: Bool = false

Expand Down Expand Up @@ -120,17 +145,25 @@ final class PushNotificationManager: NSObject, PushNotificationManagerType {
}

if pushOpenedData.silent {
delegate?.silentPushNotificationReceived(extraData: pushOpenedData.extraData)
if let delegate = delegate {
delegate.silentPushNotificationReceived(extraData: pushOpenedData.extraData)
} else {
pendingOpenedPushes.append(pushOpenedData)
}
} else {
// save campaign to be added to session start
Exponea.shared.trackCampaignData(data: pushOpenedData.campaignData, timestamp: nil)

// Notify the delegate
delegate?.pushNotificationOpened(
with: pushOpenedData.actionType,
value: pushOpenedData.actionValue,
extraData: pushOpenedData.extraData
)
if let delegate = delegate {
delegate.pushNotificationOpened(
with: pushOpenedData.actionType,
value: pushOpenedData.actionValue,
extraData: pushOpenedData.extraData
)
} else {
pendingOpenedPushes.append(pushOpenedData)
}

switch pushOpenedData.actionType {
case .none, .openApp, .selfCheck:
Expand Down Expand Up @@ -186,7 +219,7 @@ final class PushNotificationManager: NSObject, PushNotificationManagerType {
func processStoredPushOpens() {
let userDefaults = UserDefaults(suiteName: Constants.General.userDefaultsSuite)
guard let array = userDefaults?.array(forKey: Constants.General.openedPushUserDefaultsKey) else {
Exponea.logger.log(.verbose, message: "No opened push to track present in shared app group.")
Exponea.logger.log(.verbose, message: "No opened push to track present in UserDefaults.")
return
}

Expand Down

0 comments on commit 8d6ca2c

Please sign in to comment.