停止呈现PushNotification

时间:2018-12-13 10:05:18

标签: ios swift apple-push-notifications

当通知userId中没有收到userInfo时,我不想显示通知。

那么我如何停止显示通知呢?

我尝试的代码:

  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
        }
    }

但仍然显示通知。

1 个答案:

答案 0 :(得分:6)

尝试这样

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