Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of handling notifications using the clean architecture #18

Closed
NeverwinterMoon opened this issue Apr 21, 2020 · 3 comments
Closed

Comments

@NeverwinterMoon
Copy link

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.

@nalexn
Copy link
Owner

nalexn commented Apr 25, 2020

That's a good idea, I'll add these! Stay tuned!

@nalexn
Copy link
Owner

nalexn commented Apr 26, 2020

@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!

@NeverwinterMoon
Copy link
Author

@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.

@nalexn nalexn closed this as completed Sep 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants