1+ import Experiments
12import Foundation
23import UserNotifications
34import AutomatticTracks
@@ -70,12 +71,16 @@ final class PushNotificationsManager: PushNotesManager {
7071 configuration. storesManager
7172 }
7273
74+ private let featureFlagService : FeatureFlagService
75+
7376 /// Initializes the PushNotificationsManager.
7477 ///
7578 /// - Parameter configuration: PushNotificationsConfiguration Instance that should be used.
79+ /// - Parameter featureFlagService: called for multi-store push notifications feature.
7680 ///
77- init ( configuration: PushNotificationsConfiguration = . default) {
81+ init ( configuration: PushNotificationsConfiguration = . default, featureFlagService : FeatureFlagService = ServiceLocator . featureFlagService ) {
7882 self . configuration = configuration
83+ self . featureFlagService = featureFlagService
7984 }
8085}
8186
@@ -345,8 +350,13 @@ private extension PushNotificationsManager {
345350 return false
346351 }
347352
348- if let foregroundNotification = PushNotification . from ( userInfo: userInfo) {
349- configuration. application. presentInAppNotification ( message: foregroundNotification. message)
353+ let pushNotificationsForAllStoresEnabled = featureFlagService. isFeatureFlagEnabled ( . pushNotificationsForAllStores)
354+ if let foregroundNotification = PushNotification . from ( userInfo: userInfo,
355+ pushNotificationsForAllStoresEnabled: pushNotificationsForAllStoresEnabled) {
356+ configuration. application
357+ . presentInAppNotification ( title: foregroundNotification. title,
358+ subtitle: foregroundNotification. subtitle,
359+ message: foregroundNotification. message)
350360
351361 foregroundNotificationsSubject. send ( foregroundNotification)
352362 }
@@ -372,7 +382,9 @@ private extension PushNotificationsManager {
372382
373383 DDLogVerbose ( " 📱 Handling Notification in Inactive State " )
374384
375- if let notification = PushNotification . from ( userInfo: userInfo) {
385+ let pushNotificationsForAllStoresEnabled = featureFlagService. isFeatureFlagEnabled ( . pushNotificationsForAllStores)
386+ if let notification = PushNotification . from ( userInfo: userInfo,
387+ pushNotificationsForAllStoresEnabled: pushNotificationsForAllStoresEnabled) {
376388
377389 // Handling the product review notifications (`.comment`) has been moved to
378390 // `ReviewsCoordinator`. All other push notification handling should be in a coordinator
@@ -418,7 +430,7 @@ private extension PushNotificationsManager {
418430 ///
419431 func registerDotcomDevice( with deviceToken: String , defaultStoreID: Int64 , onCompletion: @escaping ( DotcomDevice ? , Error ? ) -> Void ) {
420432 let device = APNSDevice ( deviceToken: deviceToken)
421- let pushNotificationsForAllStoresEnabled = ServiceLocator . featureFlagService. isFeatureFlagEnabled ( . pushNotificationsForAllStores)
433+ let pushNotificationsForAllStoresEnabled = featureFlagService. isFeatureFlagEnabled ( . pushNotificationsForAllStores)
422434 let action = NotificationAction . registerDevice ( device: device,
423435 applicationId: WooConstants . pushApplicationID,
424436 applicationVersion: Bundle . main. version,
@@ -519,15 +531,27 @@ private extension PushNotificationsManager {
519531// MARK: - PushNotification Extension
520532
521533private extension PushNotification {
522- static func from( userInfo: [ AnyHashable : Any ] ) -> PushNotification ? {
523- guard let noteID = userInfo. integer ( forKey: APNSKey . identifier) ,
524- let message = userInfo. dictionary ( forKey: APNSKey . aps) ? . string ( forKey: APNSKey . alert) ,
525- let type = userInfo. string ( forKey: APNSKey . type) ,
526- let noteKind = Note . Kind ( rawValue: type) else {
527- return nil
534+ static func from( userInfo: [ AnyHashable : Any ] , pushNotificationsForAllStoresEnabled: Bool ) -> PushNotification ? {
535+ if pushNotificationsForAllStoresEnabled {
536+ guard let noteID = userInfo. integer ( forKey: APNSKey . identifier) ,
537+ let alert = userInfo. dictionary ( forKey: APNSKey . aps) ? . dictionary ( forKey: APNSKey . alert) ,
538+ let title = alert. string ( forKey: APNSKey . alertTitle) ,
539+ let type = userInfo. string ( forKey: APNSKey . type) ,
540+ let noteKind = Note . Kind ( rawValue: type) else {
541+ return nil
542+ }
543+ let subtitle = alert. string ( forKey: APNSKey . alertSubtitle)
544+ let message = alert. string ( forKey: APNSKey . alertMessage)
545+ return PushNotification ( noteID: noteID, kind: noteKind, title: title, subtitle: subtitle, message: message)
546+ } else {
547+ guard let noteID = userInfo. integer ( forKey: APNSKey . identifier) ,
548+ let title = userInfo. dictionary ( forKey: APNSKey . aps) ? . string ( forKey: APNSKey . alert) ,
549+ let type = userInfo. string ( forKey: APNSKey . type) ,
550+ let noteKind = Note . Kind ( rawValue: type) else {
551+ return nil
552+ }
553+ return PushNotification ( noteID: noteID, kind: noteKind, title: title, subtitle: nil , message: nil )
528554 }
529-
530- return PushNotification ( noteID: noteID, kind: noteKind, message: message)
531555 }
532556}
533557
@@ -547,6 +571,9 @@ enum AppIconBadgeNumber {
547571private enum APNSKey {
548572 static let aps = " aps "
549573 static let alert = " alert "
574+ static let alertTitle = " title "
575+ static let alertSubtitle = " subtitle "
576+ static let alertMessage = " body "
550577 static let identifier = " note_id "
551578 static let type = " type "
552579 static let siteID = " blog "
0 commit comments