Reputation: 49
When I click on the Notification when the App is closed, no prints are outputted, but I'm waiting for "app opened from PushNotification tap 2".
When the App is in background mode, everything is Ok, I see "app opened from PushNotification tap 1". Any idea what I'm missing?
Here is full example code:
import SwiftUI
import UserNotifications
struct ContentView: View {
var body: some View {
VStack {
Button("Request Permission") {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success {
print("Success")
} else if let error = error {
print(error.localizedDescription)
}
}
}
Button("Schedule Notification") {
let content = UNMutableNotificationContent()
content.title = "Title"
content.subtitle = "Subtitle"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
}
}
}
Or perhaps the code doesn't work just for me?
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("app opened from PushNotification tap 1")
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("app opened from PushNotification tap 2")
completionHandler([.banner, .sound, .badge])
}
}
Upvotes: 0
Views: 106