@@ -79,7 +79,6 @@ public class PushNotificationManager: NSObject {
7979
8080 var isSimulator : Bool = false
8181 private let notificationRegister : RemoteNotificationRegistering
82- private var restClient : RestClient ?
8382 private var preferences : SFPreferences ?
8483
8584 private var loginObserver : NSObjectProtocol ?
@@ -93,8 +92,7 @@ public class PushNotificationManager: NSObject {
9392 @objc
9493 public override convenience init ( ) {
9594 self . init ( notificationRegister: DefaultRemoteNotificationRegistrar ( ) ,
96- restClient: RestClient . shared,
97- preferences: SFPreferences . sharedPreferences ( for: . user, user: UserAccountManager . shared. currentUserAccount) )
95+ preferences: SFPreferences . sharedPreferences ( for: . user, user: UserAccountManager . shared. currentUserAccount) )
9896 }
9997
10098 /// Internal initializer used for testing and dependency injection.
@@ -103,10 +101,8 @@ public class PushNotificationManager: NSObject {
103101 /// - Parameter restClient: The REST client for making API calls. Defaults to RestClient.shared.
104102 /// - Parameter preferences: The preferences store to use. Defaults to user-level SFPreferences.
105103 internal init ( notificationRegister: RemoteNotificationRegistering = DefaultRemoteNotificationRegistrar ( ) ,
106- restClient : RestClient ? = RestClient . shared,
107104 preferences: SFPreferences ? = nil ) {
108105 self . notificationRegister = notificationRegister
109- self . restClient = restClient
110106
111107 if preferences == nil {
112108 self . preferences = SFPreferences . currentUserLevel ( )
@@ -130,9 +126,15 @@ public class PushNotificationManager: NSObject {
130126
131127 deinit {
132128 let center = NotificationCenter . default
133- center. removeObserver ( loginObserver ?? self )
134- center. removeObserver ( logoutObserver ?? self )
135- center. removeObserver ( enterForegroundObserver ?? self )
129+ if let loginObserver = loginObserver {
130+ center. removeObserver ( loginObserver)
131+ }
132+ if let logoutObserver = logoutObserver {
133+ center. removeObserver ( logoutObserver)
134+ }
135+ if let enterForegroundObserver = enterForegroundObserver {
136+ center. removeObserver ( enterForegroundObserver)
137+ }
136138 }
137139
138140 /// Registers the app with Apple Push Notification Service (APNS).
@@ -214,6 +216,7 @@ public class PushNotificationManager: NSObject {
214216 return false
215217 }
216218
219+ let restClient = notificationRegister. client ( for: user)
217220 let apiVersion = restClient? . apiVersion ?? SFRestDefaultAPIVersion
218221 let path = " / \( apiVersion) / \( PushNotificationConstants . endPoint) "
219222 let request = RestRequest ( method: . POST, path: path, queryParams: nil )
@@ -337,7 +340,7 @@ public class PushNotificationManager: NSObject {
337340 SFSDKCoreLogger . e ( Self . self, message: " Cannot unregister from notifications with Salesforce: no deviceSalesforceId " )
338341 return false
339342 }
340-
343+ let restClient = notificationRegister . client ( for : user )
341344 let apiVersion = restClient? . apiVersion ?? SFRestDefaultAPIVersion
342345 let path = " / \( apiVersion) / \( PushNotificationConstants . endPoint) / \( sfId) "
343346 let request = RestRequest ( method: . DELETE,
@@ -455,12 +458,6 @@ private extension PushNotificationManager {
455458 queue: . main
456459 ) { [ weak self] in self ? . onUserLoggedIn ( $0) }
457460
458- logoutObserver = NotificationCenter . default. addObserver (
459- forName: UserAccountManager . didLogoutUser,
460- object: nil ,
461- queue: . main
462- ) { [ weak self] in self ? . onUserLoggedOut ( $0) }
463-
464461 enterForegroundObserver = NotificationCenter . default. addObserver (
465462 forName: UIApplication . willEnterForegroundNotification,
466463 object: nil ,
@@ -469,7 +466,6 @@ private extension PushNotificationManager {
469466 }
470467
471468 @objc private func onUserLoggedIn( _ notification: Notification ) {
472- restClient = RestClient . shared
473469 if deviceToken != nil {
474470 SFSDKCoreLogger . i ( Self . self, message: " User logged in, registering push " )
475471 _ = registerSalesforceNotifications ( completionBlock: nil , failBlock: nil )
@@ -484,10 +480,6 @@ private extension PushNotificationManager {
484480 }
485481
486482 SFSDKCoreLogger . i ( Self . self, message: " App entering foreground, re-registering push " )
487- _ = registerSalesforceNotifications ( completionBlock: nil , failBlock: nil )
488- }
489-
490- @objc private func onUserLoggedOut( _ notification: Notification ) {
491- restClient = RestClient . shared
483+ registerSalesforceNotifications ( completionBlock: nil , failBlock: nil )
492484 }
493485}
0 commit comments