Uday Babariya
Uday Babariya

Reputation: 1021

Stop presenting PushNotification

I don't want to present a notification when I don't get userId in notification's userInfo.

So how can I stop notification to present?

Code I tried:

  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("willPresent notification")

        let aps = notification.request.content.userInfo[AnyHashable("aps")]!
        guard let data = aps as? [String: Any] else{ return}

        if let userId = data["userId"] as? String {
            completionHandler([.alert, .badge, .sound])
        }else{
            return
        }
    }

but still notification is presented.

Upvotes: 1

Views: 258

Answers (1)

Kathiresan Murugan
Kathiresan Murugan

Reputation: 2962

Try like this

if let userId = data["userId"] as? String {
        completionHandler([.alert, .badge, .sound])
} else {
       completionHandler([])
}

Upvotes: 7

Related Questions