@@ -176,7 +176,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
176
176
177
177
// MARK: - API Request Calls
178
178
179
- func register( token: Data ,
179
+ func register( token: String ,
180
180
onSuccess: OnSuccessHandler ? = nil ,
181
181
onFailure: OnFailureHandler ? = nil ) {
182
182
guard let appName = pushIntegrationName else {
@@ -187,8 +187,8 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
187
187
return
188
188
}
189
189
190
- hexToken = token. hexString ( )
191
- let registerTokenInfo = RegisterTokenInfo ( hexToken: token. hexString ( ) ,
190
+ hexToken = token
191
+ let registerTokenInfo = RegisterTokenInfo ( hexToken: token,
192
192
appName: appName,
193
193
pushServicePlatform: config. pushPlatform,
194
194
apnsType: dependencyContainer. apnsTypeChecker. apnsType,
@@ -208,6 +208,12 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
208
208
)
209
209
}
210
210
211
+ func register( token: Data ,
212
+ onSuccess: OnSuccessHandler ? = nil ,
213
+ onFailure: OnFailureHandler ? = nil ) {
214
+ register ( token: token. hexString ( ) , onSuccess: onSuccess, onFailure: onFailure)
215
+ }
216
+
211
217
@discardableResult
212
218
func disableDeviceForCurrentUser( withOnSuccess onSuccess: OnSuccessHandler ? = nil ,
213
219
onFailure: OnFailureHandler ? = nil ) -> Pending < SendRequestValue , SendRequestError > {
@@ -216,12 +222,18 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
216
222
onFailure ? ( errorMessage, nil )
217
223
return SendRequestError . createErroredFuture ( reason: errorMessage)
218
224
}
225
+
219
226
guard userId != nil || email != nil else {
220
227
let errorMessage = " either userId or email must be present "
221
228
onFailure ? ( errorMessage, nil )
222
229
return SendRequestError . createErroredFuture ( reason: errorMessage)
223
230
}
224
231
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
+
225
237
return requestHandler. disableDeviceForCurrentUser ( hexToken: hexToken, withOnSuccess: onSuccess, onFailure: onFailure)
226
238
}
227
239
@@ -500,6 +512,8 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
500
512
private var _userId : String ?
501
513
private var _successCallback : OnSuccessHandler ? = nil
502
514
private var _failureCallback : OnFailureHandler ? = nil
515
+
516
+ private let notificationCenter : NotificationCenterProtocol
503
517
504
518
505
519
/// the hex representation of this device token
@@ -666,6 +680,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
666
680
localStorage = dependencyContainer. localStorage
667
681
inAppDisplayer = dependencyContainer. inAppDisplayer
668
682
urlOpener = dependencyContainer. urlOpener
683
+ notificationCenter = dependencyContainer. notificationCenter
669
684
deepLinkManager = DeepLinkManager ( redirectNetworkSessionProvider: dependencyContainer)
670
685
}
671
686
@@ -698,10 +713,44 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
698
713
requestHandler. start ( )
699
714
700
715
checkRemoteConfiguration ( )
716
+
717
+ addForegroundObservers ( )
701
718
702
719
return inAppManager. start ( )
703
720
}
704
721
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
+
705
754
private func handle( launchOptions: [ UIApplication . LaunchOptionsKey : Any ] ? ) {
706
755
guard let launchOptions = launchOptions else {
707
756
return
@@ -772,6 +821,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
772
821
773
822
deinit {
774
823
ITBInfo ( )
824
+ notificationCenter. removeObserver ( self )
775
825
requestHandler. stop ( )
776
826
}
777
827
}
0 commit comments