NotificationController.swift


  1. import WatchKit
  2. import SwiftUI
  3. import UserNotifications
  4. class NotificationController: WKUserNotificationHostingController<NotificationView> {
  5. var landmark: Landmark?
  6. var title: String?
  7. var message: String?
  8. let landmarkIndexKey = "landmarkIndex"
  9. override var body: NotificationView {
  10. NotificationView(
  11. title: title,
  12. message: message,
  13. landmark: landmark
  14. )
  15. }
  16. override func willActivate() {
  17. // This method is called when watch view controller is about to be visible to user
  18. super.willActivate()
  19. }
  20. override func didDeactivate() {
  21. // This method is called when watch view controller is no longer visible
  22. super.didDeactivate()
  23. }
  24. override func didReceive(_ notification: UNNotification) {
  25. let modelData = ModelData()
  26. let notificationData =
  27. notification.request.content.userInfo as? [String: Any]
  28. let aps = notificationData?["aps"] as? [String: Any]
  29. let alert = aps?["alert"] as? [String: Any]
  30. title = alert?["title"] as? String
  31. message = alert?["body"] as? String
  32. if let index = notificationData?[landmarkIndexKey] as? Int {
  33. landmark = modelData.landmarks[index]
  34. }
  35. }
  36. }