Skip to content

Commit bca6c86

Browse files
committed
Add type-safe ForegroundNotification for PushNotificationsManager
1 parent 2ee87e1 commit bca6c86

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

WooCommerce/Classes/Notifications/PushNotificationsManager.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ private extension PushNotificationsManager {
251251
return false
252252
}
253253

254-
if let message = userInfo.dictionary(forKey: APNSKey.aps)?.string(forKey: APNSKey.alert) {
255-
configuration.application.presentInAppNotification(message: message)
254+
if let foregroundNotification = ForegroundNotification.from(userInfo: userInfo) {
255+
configuration.application.presentInAppNotification(message: foregroundNotification.message)
256256
}
257257

258258
synchronizeNotifications(completionHandler: completionHandler)
@@ -401,6 +401,20 @@ private extension PushNotificationsManager {
401401
}
402402
}
403403

404+
// MARK: - ForegroundNotification Extension
405+
406+
private extension ForegroundNotification {
407+
static func from(userInfo: [AnyHashable: Any]) -> ForegroundNotification? {
408+
guard let noteID = userInfo.integer(forKey: APNSKey.identifier),
409+
let message = userInfo.dictionary(forKey: APNSKey.aps)?.string(forKey: APNSKey.alert),
410+
let type = userInfo.string(forKey: APNSKey.type),
411+
let noteKind = Note.Kind(rawValue: type) else {
412+
return nil
413+
}
414+
415+
return ForegroundNotification(noteID: noteID, kind: noteKind, message: message)
416+
}
417+
}
404418

405419
// MARK: - Private Types
406420
//
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
import Foundation
3+
import struct Yosemite.Note
4+
5+
/// Emitted by `PushNotificationsManager` when a remote notification is received while
6+
/// the app is active.
7+
///
8+
struct ForegroundNotification {
9+
let noteID: Int
10+
let kind: Note.Kind
11+
let message: String
12+
}

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@
311311
5754727B2451F14600A94C3C /* Observable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5754727A2451F14600A94C3C /* Observable.swift */; };
312312
5754727D2451F1D800A94C3C /* PublishSubject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5754727C2451F1D800A94C3C /* PublishSubject.swift */; };
313313
5754727F24520B2A00A94C3C /* Observer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5754727E24520B2A00A94C3C /* Observer.swift */; };
314+
575472812452185300A94C3C /* ForegroundNotification.swift in Sources */ = {isa = PBXBuildFile; fileRef = 575472802452185300A94C3C /* ForegroundNotification.swift */; };
314315
576F92222423C3C0003E5FEF /* OrdersViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 576F92212423C3C0003E5FEF /* OrdersViewModel.swift */; };
315316
5795F22C23E26A8D00F6707C /* OrderSearchStarterViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5795F22B23E26A8D00F6707C /* OrderSearchStarterViewController.xib */; };
316317
5795F22E23E26A9E00F6707C /* OrderSearchStarterViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5795F22D23E26A9E00F6707C /* OrderSearchStarterViewController.swift */; };
@@ -1131,6 +1132,7 @@
11311132
5754727A2451F14600A94C3C /* Observable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Observable.swift; sourceTree = "<group>"; };
11321133
5754727C2451F1D800A94C3C /* PublishSubject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PublishSubject.swift; sourceTree = "<group>"; };
11331134
5754727E24520B2A00A94C3C /* Observer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Observer.swift; sourceTree = "<group>"; };
1135+
575472802452185300A94C3C /* ForegroundNotification.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForegroundNotification.swift; sourceTree = "<group>"; };
11341136
576F92212423C3C0003E5FEF /* OrdersViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrdersViewModel.swift; sourceTree = "<group>"; };
11351137
5795F22B23E26A8D00F6707C /* OrderSearchStarterViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = OrderSearchStarterViewController.xib; sourceTree = "<group>"; };
11361138
5795F22D23E26A9E00F6707C /* OrderSearchStarterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderSearchStarterViewController.swift; sourceTree = "<group>"; };
@@ -3669,6 +3671,7 @@
36693671
D831E2DB230E0558000037D0 /* Authentication.swift */,
36703672
D831E2DF230E0BA7000037D0 /* Logs.swift */,
36713673
02FE89C8231FB31400E85EF8 /* FeatureFlagService.swift */,
3674+
575472802452185300A94C3C /* ForegroundNotification.swift */,
36723675
);
36733676
path = ServiceLocator;
36743677
sourceTree = "<group>";
@@ -4664,6 +4667,7 @@
46644667
021AEF9E2407F55C00029D28 /* PHAssetImageLoader.swift in Sources */,
46654668
020BE74D23B1F5EB007FE54C /* TitleAndTextFieldTableViewCell.swift in Sources */,
46664669
D81F2D37225F0D160084BF9C /* EmptyListMessageWithActionView.swift in Sources */,
4670+
575472812452185300A94C3C /* ForegroundNotification.swift in Sources */,
46674671
B55BC1F121A878A30011A0C0 /* String+HTML.swift in Sources */,
46684672
B56C721221B5B44000E5E85B /* PushNotificationsConfiguration.swift in Sources */,
46694673
02279587237A50C900787C63 /* AztecHeaderFormatBarCommand.swift in Sources */,

0 commit comments

Comments
 (0)