Can a SwiftUI view be used in a Notification Content Extension? The Xcode template only offers a view controller, could this be done?
Asked
Active
Viewed 1,252 times
2 Answers
6
Yes, you should be able to embed a SwiftUI view in the UIViewController
using UIHostingController
. There are more extensive answers here (Include SwiftUI views in existing UIKit application), but here's a short version using the Xcode template for UNNotificationContentExtension
as a base:
class NotificationViewController: UIViewController, UNNotificationContentExtension {
@IBOutlet var container: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let childView = UIHostingController(rootView: SwiftUIView())
addChild(childView)
childView.view.frame = container.bounds
container.addSubview(childView.view)
childView.didMove(toParent: self)
}
func didReceive(_ notification: UNNotification) {
//
}
}

jnpdx
- 45,847
- 6
- 64
- 94
-
Has this changed at WWDC 2021? – TruMan1 Jun 13 '21 at 22:54
-
Not that I'm aware of – jnpdx Jun 13 '21 at 22:56
-
There's `UNNotificationContentProviding` but there's no documentation about it, I wonder if this could be it: https://developer.apple.com/documentation/usernotifications/unnotificationcontentproviding – TruMan1 Jun 13 '21 at 22:59
-
That doesn't look related to SwiftUI at all. – jnpdx Jun 13 '21 at 23:01
-
Hi @jnpdx , if i create an horizontal progress bar in my swiftui view and update it every seconds, that updates in my local notification? – Amin Rezaew Jan 21 '23 at 19:34
-
Sorry — I don’t understand. If you have an additional question, I suggest you make a new post. – jnpdx Jan 21 '23 at 21:19
-
@jnpdx, i want to use this notification content extension in swiftui project, with your answer we can add swiftui view to story board, but how can i call this story board view in my swiftui app? – Amin Rezaew Jan 22 '23 at 09:10
-
Know i add my swiftui view in UIHostingController but that gives error can not find my view in scope, what should i do? – Amin Rezaew Jan 22 '23 at 09:19
-
Start a new question and include the relevant code. This isn’t a good conversation to have in the comment section. – jnpdx Jan 22 '23 at 15:38
3
Here's how I do it using AutoLayout rules.
...
var hostingView: UIHostingController<NotificationView>!
...
func didReceive(_ notification: UNNotification) {
let notificationView = NotificationView()
hostingView = UIHostingController(rootView: notificationView)
self.view.addSubview(hostingView.view)
hostingView.view.translatesAutoresizingMaskIntoConstraints = false
hostingView.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
hostingView.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
hostingView.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
hostingView.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
}
http://brunowernimont.me/howtos/2021-06-21-embed-swiftui-view-in-notification-content-extension

Bruno Wernimont
- 101
- 1
- 4