I got here to SwiftUI from Android growth. At work, we determined to experiment with KMM and who, if not me, responded to cope with the IOS half with none topic data.
The issue is that I can’t perceive the place there may be an occasion or maybe some type of callback to catch the second when a notification is displayed on the display.
I apologize upfront, maybe the query will appear very deletant to iOS builders. Any criticism and recommendation on greatest practices are welcome.
From what I might work out with none issues, that is the implementation of the notification itself and the implementation of the delegate by which there are strategies which are known as by clicking on the notification within the background and within the foreground. However this isn’t precisely what I would like, I would like to repair precisely the second when the notification is displayed on the display.
personal func scheduleNotification(delayTime: Double) {
let heart = UNUserNotificationCenter.present()
heart.requestAuthorization(choices: [.alert, .sound, .badge]) { consequence, error in
if let error = error {
print(error)
}
let content material = UNMutableNotificationContent()
content material.title = "title"
content material.physique = "physique"
let triger = UNTimeIntervalNotificationTrigger(timeInterval: delayTime, repeats: false)
let request = UNNotificationRequest(identifier: "my_id", content material: content material, set off: triger)
heart.add(request) { error in
if let error = error {
print(error)
}
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
func software(_ software: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.present().delegate = self
return true
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
/** Deal with notification when app is in background */
func userNotificationCenter(_ heart: UNUserNotificationCenter, didReceive response:
UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let notiName = Notification.Identify(response.notification.request.identifier)
NotificationCenter.default.publish(identify:notiName , object: response.notification.request.content material)
completionHandler()
}
/** Deal with notification when the app is in foreground */
func userNotificationCenter(_ heart: UNUserNotificationCenter, willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let notiName = Notification.Identify( notification.request.identifier )
NotificationCenter.default.publish(identify:notiName , object: notification.request.content material)
completionHandler([.banner, .sound, .badge])
}
}
@predominant
struct iOSApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var physique: some Scene {
...
}
}