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 not perceive the place there may be an occasion or maybe some sort of callback to catch the second when a notification is displayed on the display screen.
I apologize upfront, maybe the query will appear very deletant to iOS builders. Any criticism and recommendation on finest practices are welcome.
From what I may work out with none issues, that is the implementation of the notification itself and the implementation of the delegate through which there are strategies which can be referred to as by clicking on the notification within the background and within the foreground. However this isn’t precisely what I want, I want to repair precisely the second when the notification is displayed on the display screen.
non-public func scheduleNotification(delayTime: Double) {
let middle = UNUserNotificationCenter.present()
middle.requestAuthorization(choices: [.alert, .sound, .badge]) { outcome, error in
if let error = error {
print(error)
}
let content material = UNMutableNotificationContent()
content material.title = MR.strings().push_title.get()
content material.physique = MR.strings().new_sticker_push.get()
let triger = UNTimeIntervalNotificationTrigger(timeInterval: 15, repeats: false)
let request = UNNotificationRequest(identifier: "my_id", content material: content material, set off: triger)
middle.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(_ middle: UNUserNotificationCenter, didReceive response:
UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let notiName = Notification.Identify(response.notification.request.identifier)
NotificationCenter.default.put up(identify:notiName , object: response.notification.request.content material)
completionHandler()
}
/** Deal with notification when the app is in foreground */
func userNotificationCenter(_ middle: UNUserNotificationCenter, willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let notiName = Notification.Identify( notification.request.identifier )
NotificationCenter.default.put up(identify:notiName , object: notification.request.content material)
completionHandler([.banner, .sound, .badge])
}
}
@fundamental
struct iOSApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var physique: some Scene {
...
}
}