Abd
Abd

Reputation: 568

Disable push notification in specific view controller in foreground?

I am implementing a chat feature in an app. Right now every push I am using this to show notification while using app:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert, .badge, .sound])
}

I am wondering if it is possible to disable push notification from showing at top, while maintaining the buzzer for a specific view controller?

If yes, what should I do to create it?

Upvotes: 3

Views: 2109

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100541

Implement it like this:

UNUserNotificationCenter.current().delegate = self

...

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    if wantToShow {
         completionHandler([.alert, .sound])
    } else {
         completionHandler([])
    }
}

Then change wantToShow according to the current state.

Upvotes: 3

Related Questions