@@ -79,45 +79,33 @@ public class PushNotificationManager: NSObject {
7979
8080 var isSimulator : Bool = false
8181 private let notificationRegister : RemoteNotificationRegistering
82- private var apiVersion : String
8382 private var restClient : RestClient ?
84- private var currentUser : UserAccount ?
8583 private var preferences : SFPreferences ?
8684
8785 /// Convenience initializer that sets up the PushNotificationManager with default values:
8886 /// - notificationRegister: DefaultRemoteNotificationRegistrar() - Handles APNS registration
89- /// - apiVersion: RestClient.shared.apiVersion - Uses the current API version from RestClient
9087 /// - restClient: RestClient.shared - Uses the shared RestClient instance
91- /// - currentUser: UserAccountManager.shared.currentUserAccount - Uses the current logged-in user
9288 /// - preferences: SFPreferences.sharedPreferences(for: .user, user: currentUser) - Uses user-level preferences
9389 @objc
9490 public override convenience init ( ) {
9591 self . init ( notificationRegister: DefaultRemoteNotificationRegistrar ( ) ,
96- apiVersion: RestClient . shared. apiVersion,
9792 restClient: RestClient . shared,
98- currentUser: UserAccountManager . shared. currentUserAccount,
9993 preferences: SFPreferences . sharedPreferences ( for: . user, user: UserAccountManager . shared. currentUserAccount) )
10094 }
10195
10296 /// Internal initializer used for testing and dependency injection.
10397 /// This initializer allows for customizing the dependencies of PushNotificationManager:
10498 /// - Parameter notificationRegister: The registrar that handles APNS registration. Defaults to DefaultRemoteNotificationRegistrar.
105- /// - Parameter apiVersion: The Salesforce API version to use. Defaults to RestClient.shared.apiVersion.
10699 /// - Parameter restClient: The REST client for making API calls. Defaults to RestClient.shared.
107- /// - Parameter currentUser: The user account to use. Defaults to UserAccountManager.shared.currentUserAccount.
108100 /// - Parameter preferences: The preferences store to use. Defaults to user-level SFPreferences.
109101 internal init ( notificationRegister: RemoteNotificationRegistering = DefaultRemoteNotificationRegistrar ( ) ,
110- apiVersion: String = RestClient . shared. apiVersion,
111102 restClient : RestClient ? = RestClient . shared,
112- currentUser: UserAccount ? = UserAccountManager . shared. currentUserAccount,
113103 preferences: SFPreferences ? = nil ) {
114104 self . notificationRegister = notificationRegister
115- self . apiVersion = apiVersion
116105 self . restClient = restClient
117- self . currentUser = currentUser
118106
119107 if preferences == nil {
120- self . preferences = SFPreferences . sharedPreferences ( for : . user , user : currentUser )
108+ self . preferences = SFPreferences . currentUserLevel ( )
121109 } else {
122110 self . preferences = preferences
123111 }
@@ -130,9 +118,8 @@ public class PushNotificationManager: NSObject {
130118 self . isSimulator = false
131119#endif
132120
133- let prefs = SFPreferences . currentUserLevel ( )
134- self . deviceToken = prefs? . string ( forKey: PushNotificationConstants . deviceToken)
135- self . deviceSalesforceId = prefs? . string ( forKey: PushNotificationConstants . deviceSalesforceId)
121+ self . deviceToken = preferences? . string ( forKey: PushNotificationConstants . deviceToken)
122+ self . deviceSalesforceId = preferences? . string ( forKey: PushNotificationConstants . deviceSalesforceId)
136123
137124 NotificationCenter . default. addObserver ( self ,
138125 selector: #selector( onUserLoggedIn ( _: ) ) ,
@@ -224,6 +211,7 @@ public class PushNotificationManager: NSObject {
224211 return false
225212 }
226213
214+ let apiVersion = restClient? . apiVersion ?? SFRestDefaultAPIVersion
227215 let path = " / \( apiVersion) / \( PushNotificationConstants . endPoint) "
228216 let request = RestRequest ( method: . POST, path: path, queryParams: nil )
229217
@@ -286,7 +274,7 @@ public class PushNotificationManager: NSObject {
286274
287275 Task {
288276 do {
289- try await self . fetchAndStoreNotificationTypes ( restClient: restClient, account : user )
277+ try await self . fetchAndStoreNotificationTypes ( restClient: restClient)
290278 } catch {
291279 SFSDKCoreLogger . e ( Self . self, message: " Get Notification Types Error: \( error. localizedDescription) " )
292280 }
@@ -317,64 +305,14 @@ public class PushNotificationManager: NSObject {
317305 return unregisterSalesforceNotifications ( for: user, completionBlock: completionBlock)
318306 }
319307
320- /// Unregisters the device from Salesforce push notifications for a specific user.
321- ///
322- /// - Parameters:
323- /// - user: The user account to unregister.
324- /// - completionBlock: A block executed when unregistration is complete.
325- /// - Returns: `true` if unregistration started successfully, otherwise `false`.
326- @discardableResult
327- @objc ( unregisterSalesforceNotificationsWithCompletionBlock: completionBlock: )
328- public func unregisterSalesforceNotifications( for user: UserAccount ,
329- completionBlock: ( ( ) -> Void ) ? ) -> Bool {
330- guard deviceSalesforceId != nil else {
331- completionBlock ? ( )
332- return true
333- }
334-
335- if isSimulator {
336- completionBlock ? ( )
337- return true
338- }
339-
340- guard let prefs = preferences else {
341- SFSDKCoreLogger . e ( Self . self, message: " Cannot unregister from notifications with Salesforce: no user prefs " )
342- return false
343- }
344-
345- guard let sfId = prefs. string ( forKey: PushNotificationConstants . deviceSalesforceId) else {
346- SFSDKCoreLogger . e ( Self . self, message: " Cannot unregister from notifications with Salesforce: no deviceSalesforceId " )
347- return false
348- }
349-
350- let path = " / \( apiVersion) / \( PushNotificationConstants . endPoint) / \( sfId) "
351- let request = RestRequest ( method: . DELETE,
352- path: path,
353- queryParams: nil )
354- Task {
355- do {
356- _ = try await restClient? . send ( request: request)
357- completionBlock ? ( )
358- } catch {
359- SFSDKCoreLogger . e ( Self . self, message: " Push notification unregistration failed: \( error. localizedDescription) " )
360- completionBlock ? ( )
361- }
362- }
363-
364- SFSDKCoreLogger . i ( Self . self, message: " Unregister from notifications with Salesforce sent " )
365- return true
366- }
367-
368308 /// Fetches and stores actionable notification types from the server or cache.
369309 ///
370310 /// - Parameters:
371311 /// - restClient: The `RestClient` to use for the API call.
372- /// - account: The user account to associate notification types with.
373312 /// - Throws: An error if the types cannot be retrieved from server or cache.
374- @objc ( fetchAndStoreNotificationTypesWithRestClient: account: completionHandler: )
375- public func fetchAndStoreNotificationTypes( restClient: RestClient = RestClient . shared,
376- account: UserAccount ? = UserAccountManager . shared. currentUserAccount) async throws {
377- guard let account = account else {
313+ @objc ( fetchAndStoreNotificationTypesWithRestClient: completionHandler: )
314+ public func fetchAndStoreNotificationTypes( restClient: RestClient = RestClient . shared) async throws {
315+ guard let account = UserAccountManager . shared. currentUserAccount else {
378316 throw PushNotificationManagerError . currentUserNotDetected
379317 }
380318
@@ -439,25 +377,10 @@ public class PushNotificationManager: NSObject {
439377 }
440378 self . unregisterForSalesforceNotifications ( user: currentUser, completionBlock)
441379 }
442-
443- /// Unregister from Salesforce notfications for a specific user
444- /// - Parameters:
445- /// - user: The user that should be unregistered from notfications
446- /// - completionBlock: completion block to call with success or failure
447- public func unregisterForSalesforceNotifications( user: UserAccount , _ completionBlock: @escaping ( Bool ) -> ( ) ) {
448- let result = unregisterSalesforceNotifications ( for: user) {
449- completionBlock ( true )
450- }
451-
452- if ( !result) {
453- completionBlock ( false )
454- }
455- }
456380}
457381
458382private extension PushNotificationManager {
459383 @objc private func onUserLoggedIn( _ notification: Notification ) {
460- refreshDependencies ( )
461384
462385 if deviceToken != nil {
463386 SFSDKCoreLogger . i ( Self . self, message: " User logged in, registering push " )
@@ -476,13 +399,54 @@ private extension PushNotificationManager {
476399 _ = registerSalesforceNotifications ( completionBlock: nil , failBlock: nil )
477400 }
478401
479- func refreshDependencies( ) {
480- // Refresh dependencies now that user is logged in
481- let restClient = RestClient . shared
482- let user = UserAccountManager . shared. currentUserAccount
402+ private func unregisterSalesforceNotifications( for user: UserAccount ,
403+ completionBlock: ( ( ) -> Void ) ? ) -> Bool {
404+ guard deviceSalesforceId != nil else {
405+ completionBlock ? ( )
406+ return true
407+ }
483408
484- self . restClient = restClient
485- self . apiVersion = restClient. apiVersion
486- self . currentUser = user
409+ if isSimulator {
410+ completionBlock ? ( )
411+ return true
412+ }
413+
414+ guard let prefs = preferences else {
415+ SFSDKCoreLogger . e ( Self . self, message: " Cannot unregister from notifications with Salesforce: no user prefs " )
416+ return false
417+ }
418+
419+ guard let sfId = prefs. string ( forKey: PushNotificationConstants . deviceSalesforceId) else {
420+ SFSDKCoreLogger . e ( Self . self, message: " Cannot unregister from notifications with Salesforce: no deviceSalesforceId " )
421+ return false
422+ }
423+
424+ let apiVersion = restClient? . apiVersion ?? SFRestDefaultAPIVersion
425+ let path = " / \( apiVersion) / \( PushNotificationConstants . endPoint) / \( sfId) "
426+ let request = RestRequest ( method: . DELETE,
427+ path: path,
428+ queryParams: nil )
429+ Task {
430+ do {
431+ _ = try await restClient? . send ( request: request)
432+ completionBlock ? ( )
433+ } catch {
434+ SFSDKCoreLogger . e ( Self . self, message: " Push notification unregistration failed: \( error. localizedDescription) " )
435+ completionBlock ? ( )
436+ }
437+ }
438+
439+ SFSDKCoreLogger . i ( Self . self, message: " Unregister from notifications with Salesforce sent " )
440+ return true
441+ }
442+
443+ private func unregisterForSalesforceNotifications( user: UserAccount , _ completionBlock: @escaping ( Bool ) -> ( ) ) {
444+ let result = unregisterSalesforceNotifications ( for: user) {
445+ completionBlock ( true )
446+ }
447+
448+ if ( !result) {
449+ completionBlock ( false )
450+ }
487451 }
488452}
0 commit comments