Skip to content

Commit f8ae623

Browse files
committed
Add observable background notifications to observe and reset the last sync timestamp in store stats.
1 parent 0929304 commit f8ae623

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

WooCommerce/Classes/Notifications/PushNotificationsManager.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ final class PushNotificationsManager: PushNotesManager {
4545
/// Mutable reference to `inactiveNotifications`
4646
private let inactiveNotificationsSubject = PassthroughSubject<PushNotification, Never>()
4747

48+
/// An observable that emits values when a Remote Notification is received while the app is
49+
/// in the background.
50+
///
51+
var backgroundNotifications: AnyPublisher<PushNotification, Never> {
52+
backgroundNotificationsSubject.eraseToAnyPublisher()
53+
}
54+
55+
/// Mutable reference to `backgroundNotifications`
56+
private let backgroundNotificationsSubject = PassthroughSubject<PushNotification, Never>()
57+
4858
/// An observable that emits values when a local notification is received.
4959
///
5060
var localNotificationUserResponses: AnyPublisher<UNNotificationResponse, Never> {
@@ -292,6 +302,10 @@ extension PushNotificationsManager {
292302

293303
handleRemoteNotificationInAllAppStates(userInfo)
294304

305+
if let notification = PushNotification.from(userInfo: userInfo) {
306+
backgroundNotificationsSubject.send(notification)
307+
}
308+
295309
return await synchronizeNotifications()
296310
}
297311

WooCommerce/Classes/ServiceLocator/PushNotesManager.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ protocol PushNotesManager {
2020
///
2121
var inactiveNotifications: AnyPublisher<PushNotification, Never> { get }
2222

23+
/// An observable that emits values when a Remote Notification is received while the app is
24+
/// in the background.
25+
///
26+
var backgroundNotifications: AnyPublisher<PushNotification, Never> { get }
27+
2328
/// An observable that emits values when a local notification response is received.
2429
///
2530
var localNotificationUserResponses: AnyPublisher<UNNotificationResponse, Never> { get }

WooCommerce/Classes/ViewRelated/Dashboard/Stats v4/StoreStatsAndTopPerformersViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ private extension StoreStatsAndTopPerformersViewController {
268268
func observeRemotelyCreatedOrdersToResetLastSyncTimestamp() {
269269
let siteID = self.siteID
270270
remoteOrdersSubscription = Publishers
271-
.Merge(pushNotificationsManager.inactiveNotifications, pushNotificationsManager.foregroundNotificationsToView)
271+
.Merge(pushNotificationsManager.backgroundNotifications, pushNotificationsManager.foregroundNotifications)
272272
.filter { $0.kind == .storeOrder && $0.siteID == siteID }
273273
.sink { [weak self] _ in
274274
self?.resetLastSyncTimestamp()

WooCommerce/WooCommerceTests/Mocks/MockPushNotificationsManager.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ final class MockPushNotificationsManager: PushNotesManager {
2424

2525
private let inactiveNotificationsSubject = PassthroughSubject<PushNotification, Never>()
2626

27+
var backgroundNotifications: AnyPublisher<PushNotification, Never> {
28+
backgroundNotificationsSubject.eraseToAnyPublisher()
29+
}
30+
31+
private let backgroundNotificationsSubject = PassthroughSubject<PushNotification, Never>()
32+
2733
var localNotificationUserResponses: AnyPublisher<UNNotificationResponse, Never> {
2834
localNotificationResponsesSubject.eraseToAnyPublisher()
2935
}

WooCommerce/WooCommerceTests/Notifications/PushNotificationsManagerTests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,48 @@ final class PushNotificationsManagerTests: XCTestCase {
470470
XCTAssertNil(emittedNotification.message)
471471
}
472472

473+
// MARK: - Background Notification Observable
474+
475+
func test_it_emits_background_notifications_when_it_receives_a_notification_while_app_is_active() async throws {
476+
// Given
477+
application.applicationState = .background
478+
479+
manager = {
480+
let configuration = PushNotificationsConfiguration(application: self.application,
481+
defaults: self.defaults,
482+
storesManager: self.storesManager,
483+
supportManager: self.supportManager,
484+
userNotificationsCenter: self.userNotificationCenter)
485+
return PushNotificationsManager(configuration: configuration)
486+
}()
487+
488+
var emittedNotifications = [PushNotification]()
489+
manager.backgroundNotifications.sink { notification in
490+
emittedNotifications.append(notification)
491+
}.store(in: &subscriptions)
492+
493+
let userinfo = notificationPayload(noteID: 9_981,
494+
type: .storeOrder,
495+
siteID: 606,
496+
title: Sample.defaultTitle,
497+
subtitle: Sample.defaultSubtitle,
498+
message: Sample.defaultMessage)
499+
500+
// When
501+
_ = await manager.handleRemoteNotificationInTheBackground(userInfo: userinfo)
502+
503+
// Then
504+
XCTAssertEqual(emittedNotifications.count, 1)
505+
506+
let emittedNotification = try XCTUnwrap(emittedNotifications.first)
507+
XCTAssertEqual(emittedNotification.kind, .storeOrder)
508+
XCTAssertEqual(emittedNotification.noteID, 9_981)
509+
XCTAssertEqual(emittedNotification.siteID, 606)
510+
XCTAssertEqual(emittedNotification.title, Sample.defaultTitle)
511+
XCTAssertEqual(emittedNotification.subtitle, Sample.defaultSubtitle)
512+
XCTAssertEqual(emittedNotification.message, Sample.defaultMessage)
513+
}
514+
473515
// MARK: - App Badge Number
474516

475517
/// Verifies that `handleNotification` updates app badge number to 1 when the notification is from the same site.

0 commit comments

Comments
 (0)