@@ -176,7 +176,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
176176
177177 // MARK: - API Request Calls
178178
179- func register( token: Data ,
179+ func register( token: String ,
180180 onSuccess: OnSuccessHandler ? = nil ,
181181 onFailure: OnFailureHandler ? = nil ) {
182182 guard let appName = pushIntegrationName else {
@@ -187,8 +187,8 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
187187 return
188188 }
189189
190- hexToken = token. hexString ( )
191- let registerTokenInfo = RegisterTokenInfo ( hexToken: token. hexString ( ) ,
190+ hexToken = token
191+ let registerTokenInfo = RegisterTokenInfo ( hexToken: token,
192192 appName: appName,
193193 pushServicePlatform: config. pushPlatform,
194194 apnsType: dependencyContainer. apnsTypeChecker. apnsType,
@@ -208,6 +208,12 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
208208 )
209209 }
210210
211+ func register( token: Data ,
212+ onSuccess: OnSuccessHandler ? = nil ,
213+ onFailure: OnFailureHandler ? = nil ) {
214+ register ( token: token. hexString ( ) , onSuccess: onSuccess, onFailure: onFailure)
215+ }
216+
211217 @discardableResult
212218 func disableDeviceForCurrentUser( withOnSuccess onSuccess: OnSuccessHandler ? = nil ,
213219 onFailure: OnFailureHandler ? = nil ) -> Pending < SendRequestValue , SendRequestError > {
@@ -216,12 +222,18 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
216222 onFailure ? ( errorMessage, nil )
217223 return SendRequestError . createErroredFuture ( reason: errorMessage)
218224 }
225+
219226 guard userId != nil || email != nil else {
220227 let errorMessage = " either userId or email must be present "
221228 onFailure ? ( errorMessage, nil )
222229 return SendRequestError . createErroredFuture ( reason: errorMessage)
223230 }
224231
232+ // We need to call register token here so that we can trigger the device registration
233+ // with the updated notification settings
234+
235+ register ( token: hexToken)
236+
225237 return requestHandler. disableDeviceForCurrentUser ( hexToken: hexToken, withOnSuccess: onSuccess, onFailure: onFailure)
226238 }
227239
@@ -500,6 +512,8 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
500512 private var _userId : String ?
501513 private var _successCallback : OnSuccessHandler ? = nil
502514 private var _failureCallback : OnFailureHandler ? = nil
515+
516+ private let notificationCenter : NotificationCenterProtocol
503517
504518
505519 /// the hex representation of this device token
@@ -666,6 +680,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
666680 localStorage = dependencyContainer. localStorage
667681 inAppDisplayer = dependencyContainer. inAppDisplayer
668682 urlOpener = dependencyContainer. urlOpener
683+ notificationCenter = dependencyContainer. notificationCenter
669684 deepLinkManager = DeepLinkManager ( redirectNetworkSessionProvider: dependencyContainer)
670685 }
671686
@@ -698,10 +713,44 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
698713 requestHandler. start ( )
699714
700715 checkRemoteConfiguration ( )
716+
717+ addForegroundObservers ( )
701718
702719 return inAppManager. start ( )
703720 }
704721
722+ private func addForegroundObservers( ) {
723+ notificationCenter. addObserver ( self ,
724+ selector: #selector( onAppDidBecomeActiveNotification ( notification: ) ) ,
725+ name: UIApplication . didBecomeActiveNotification,
726+ object: nil )
727+ }
728+
729+ @objc private func onAppDidBecomeActiveNotification( notification: Notification ) {
730+ guard config. autoPushRegistration else { return }
731+
732+ notificationStateProvider. isNotificationsEnabled { [ weak self] systemEnabled in
733+ guard let self = self else { return }
734+
735+ let storedEnabled = self . localStorage. isNotificationsEnabled
736+ let hasStoredPermission = self . localStorage. hasStoredNotificationSetting
737+
738+ if self . isEitherUserIdOrEmailSet ( ) {
739+ if hasStoredPermission && ( storedEnabled != systemEnabled) {
740+ if !systemEnabled {
741+ self . disableDeviceForCurrentUser ( )
742+ } else {
743+ self . notificationStateProvider. registerForRemoteNotifications ( )
744+ }
745+ }
746+
747+ // Always store the current state
748+ self . localStorage. isNotificationsEnabled = systemEnabled
749+ self . localStorage. hasStoredNotificationSetting = true
750+ }
751+ }
752+ }
753+
705754 private func handle( launchOptions: [ UIApplication . LaunchOptionsKey : Any ] ? ) {
706755 guard let launchOptions = launchOptions else {
707756 return
@@ -772,6 +821,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
772821
773822 deinit {
774823 ITBInfo ( )
824+ notificationCenter. removeObserver ( self )
775825 requestHandler. stop ( )
776826 }
777827}
0 commit comments