Example of handling notifications using the clean architecture
NeverwinterMoon opened this issue · comments
Would be nice to see an example of how to handle notifications, especially the silent ones that would trigger some data fetch/update.
Seeing as notifications are handled in AppDelegate callback, how would one go around hooking the logic from, say, Interactors
.
So far, I see that it's possible to handle non-silent notifications in scene delegate like this:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func scene(
_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions
) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
let environment = AppEnvironment.bootstrap(window: window)
// This is UNNotificationResponse
if let notificationResponse = connectionOptions.notificationResponse {
window.makeKeyAndVisible()
// do something with environment.container.interactors
return
}
window.makeKeyAndVisible()
}
}
}
And for silent notifications:
func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> ()
) {
if let sceneDelegate = application.connectedScenes.first?.delegate as? SceneDelegate {
sceneDelegate.handleSilentNotification(userInfo)
completionHandler(.newData)
}
completionHandler(.noData)
}
Now that Xcode 11.4 allows to test notifications even on a simulator, it would be great to see the notification handling introduced to the example Clean SwiftUI Architecture app.
That's a good idea, I'll add these! Stay tuned!
@NeverwinterMoon , I've added handling of silent notifications and the notifications with a deep link. The project now contains a test .apns
notification payload you can drag & drop onto the Simulator. Thank you for suggesting this!
@nalexn This is amazing! Thanks a lot for a quick response time and great work. This is definitely cleaner than my own implementation. Will try to find time today to check out how I can improve my code based on your work.