-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix registerRemoteNotifications action would never return (#115)
# Fix registerRemoteNotifications action would never return ## ♻️ Current situation & Problem #98 introduced the `registerRemoteNotifications` action. However, the knowledge source that stores the continuation was never persisted in the store. Therefore, the continuation was never resumed and allocated resources forever. This was fixed with this PR. We originally discussed supporting cancellation of the registration processed, however I decided against as it wasn't clear to properly handle cancellation (e.g., automatically call the unregister action? How to handle the delegate call that might arrive later. Especially when another call to the action might incur in the mean time). This PR changes the behavior when concurrently calling the registerRemoteNotifications action. Now, the second caller will wait for the first call to complete instead of throwing a concurrence access error. This helps to share this resources with, e.g., multiple modules trying to retrieve an device token. **To emphasize: `registerRemoteNotifications` works on simulator devices!** ## ⚙️ Release Notes * Fixed an issue where the `registerRemoteNotifications` actions would never return. ## 📚 Documentation Slightly updated docs to provide more guidance around requesting authorization for notifications. ## ✅ Testing Added a unit test that verifies that the action returns. ## 📝 Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md). --------- Co-authored-by: Paul Schmiedmayer <[email protected]>
- Loading branch information
1 parent
7755013
commit f1f6fb4
Showing
12 changed files
with
301 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
Tests/UITests/TestApp/RemoteNotifications/NotificationModule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import Spezi | ||
|
||
|
||
@MainActor | ||
class NotificationModule: Module, EnvironmentAccessible { | ||
@Application(\.registerRemoteNotifications) | ||
var registerRemoteNotifications | ||
|
||
@Application(\.unregisterRemoteNotifications) | ||
var unregisterRemoteNotifications | ||
} |
75 changes: 75 additions & 0 deletions
75
Tests/UITests/TestApp/RemoteNotifications/RemoteNotificationsTestView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SwiftUI | ||
|
||
|
||
struct RemoteNotificationsTestView: View { | ||
@Environment(NotificationModule.self) | ||
private var notificationModule | ||
|
||
@State private var token: Data? | ||
@State private var error: Error? | ||
|
||
@State private var task: Task<Void, Never>? | ||
|
||
var body: some View { | ||
List { // swiftlint:disable:this closure_body_length | ||
Section("Token") { | ||
HStack { | ||
Text(verbatim: "Token") | ||
Spacer() | ||
if let token { | ||
Text(token.description) | ||
.foregroundStyle(.green) | ||
} else if let error = error as? LocalizedError, | ||
let description = error.errorDescription ?? error.failureReason { | ||
Text(verbatim: description) | ||
.foregroundStyle(.red) | ||
} else if error != nil { | ||
Text(verbatim: "failed") | ||
.foregroundStyle(.red) | ||
} else { | ||
Text(verbatim: "none") | ||
.foregroundStyle(.secondary) | ||
} | ||
} | ||
.accessibilityElement(children: .combine) | ||
.accessibilityIdentifier("token-field") | ||
} | ||
|
||
Section("Actions") { | ||
Button("Register") { | ||
task = Task { @MainActor in | ||
do { | ||
token = try await notificationModule.registerRemoteNotifications() | ||
} catch { | ||
self.error = error | ||
} | ||
} | ||
} | ||
Button("Unregister") { | ||
notificationModule.unregisterRemoteNotifications() | ||
token = nil | ||
error = nil | ||
} | ||
} | ||
} | ||
.onDisappear { | ||
task?.cancel() | ||
} | ||
} | ||
} | ||
|
||
|
||
#if DEBUG | ||
#Preview { | ||
RemoteNotificationsTestView() | ||
.environment(NotificationModule()) | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ class TestAppDelegate: SpeziAppDelegate { | |
} | ||
ModuleWithModifier() | ||
ModuleWithModel() | ||
NotificationModule() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>aps-environment</key> | ||
<string>development</string> | ||
<key>com.apple.developer.aps-environment</key> | ||
<string>development</string> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
This source file is part of the Stanford Spezi open-source project | ||
|
||
SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
|
||
SPDX-License-Identifier: MIT |
Oops, something went wrong.