Skip to content

Commit

Permalink
Merge pull request #11 from Miiha/fix/app-launch
Browse files Browse the repository at this point in the history
Fixes handling for notification which launch the app in example app.
  • Loading branch information
Miiha authored Mar 8, 2023
2 parents da0ea36 + 8225cfd commit 8e68dde
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1420"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4AA682A9254D824900031ADC"
BuildableName = "Example.app"
BlueprintName = "Example"
ReferencedContainer = "container:Example.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4AA682BA254D824B00031ADC"
BuildableName = "ExampleTests.xctest"
BlueprintName = "ExampleTests"
ReferencedContainer = "container:Example.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4AA682A9254D824900031ADC"
BuildableName = "Example.app"
BlueprintName = "Example"
ReferencedContainer = "container:Example.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4AA682A9254D824900031ADC"
BuildableName = "Example.app"
BlueprintName = "Example"
ReferencedContainer = "container:Example.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
19 changes: 10 additions & 9 deletions Examples/Example/Example/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct App: ReducerProtocol {

enum Action: Equatable {
case addNotificationResponse(TaskResult<Unit>)
case didFinishLaunching(notification: UserNotification?)
case didFinishLaunching
case didReceiveBackgroundNotification(BackgroundNotification)
case remoteCountResponse(TaskResult<Int>)
case requestAuthorizationResponse(TaskResult<Bool>)
Expand All @@ -23,15 +23,14 @@ struct App: ReducerProtocol {

func reduce(into state: inout State, action: Action) -> EffectTask<Action> {
switch action {
case let .didFinishLaunching(notification):
if case let .count(value) = notification {
state.count = value
}

case .didFinishLaunching:
// Ensure that the delegate is created within `didFinishLaunchingWithOptions` to
// receive notifications that started the application.
let userNotifications = self.userNotifications.delegate()
return .run { send in
await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
for await event in self.userNotifications.delegate() {
for await event in userNotifications {
await send(.userNotifications(event))
}
}
Expand Down Expand Up @@ -80,7 +79,8 @@ struct App: ReducerProtocol {
}

case let .userNotifications(.didReceiveResponse(response, completion)):
let notification = UserNotification(userInfo: response.notification.request.content.userInfo())
let userInfo = response.notification.request.content.userInfo()
let notification = UserNotification(userInfo: userInfo)
if case let .count(value) = notification {
state.count = value
}
Expand All @@ -100,11 +100,12 @@ struct App: ReducerProtocol {
let content = UNMutableNotificationContent()
content.title = "Example title"
content.body = "Example body"
content.userInfo = ["count": 123]

let request = UNNotificationRequest(
identifier: "example_notification",
content: content,
trigger: UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
trigger: UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
)

return .task {
Expand Down
2 changes: 1 addition & 1 deletion Examples/Example/Example/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {

ViewStore(store).send(.didFinishLaunching(notification: launchOptions?.notification))
ViewStore(store).send(.didFinishLaunching)
return true
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/ComposableUserNotifications/LiveKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extension UserNotificationClient: DependencyKey {
let delegate = Delegate(continuation: continuation)
UNUserNotificationCenter.current().delegate = delegate
continuation.onTermination = { _ in
let _ = delegate
_ = delegate
}
}
}
Expand Down

0 comments on commit 8e68dde

Please sign in to comment.