Im trying to run a code (here I print in console) when the notification appears on screen even if user is not interacting with it, but nothing happens, what am I missing?
import SwiftUI
import UserNotifications
class MyUserNotificationCenterDelegate : NSObject, UNUserNotificationCenterDelegate
{
func userNotificationCenter( center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
}
func userNotificationCenter( center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
print("notification arrived")
completionHandler()
}
}
struct ContentView: View
{
var body: some View
{
VStack
{
Button("Permission")
{
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success {
print("Done!")
} else if let error = error {
print(error.localizedDescription)
}
}
}
Button("Schedule")
{
let content = UNMutableNotificationContent()
content.title = "notification"
content.subtitle = "subtitle"
content.sound = UNNotificationSound.defaultCritical
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
Button("Stop")
{ UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I tried conforming the notification delegate UNUserNotificationCenterDelegate as you can see, but maybe I did forget something here, or this is not what is called when notification just shows up