Reputation: 3435
When push notification arrives, and app is in foreground, I need to show custom popup. It must come from the upper edge of the screen, stay visible to a few seconds and then disappear back behind the top edge.
My question is how do I show it? As I know, in order to make view visible, I must add it as a subview to some existing view. But push notification may come at random time, so I do not know beforehand which view controller will be active at that moment.
So: where do I attach my custom view to (to make it visible on top of everything)?
Upvotes: 0
Views: 54
Reputation: 5039
You need to implement the following delegate in your appDelegate and your push notification will appear in foreground as well.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
And on tapping on the notification you can get the same behavior as you do when app is in background.
Upvotes: 1