当通知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
}
}
但仍然显示通知。
答案 0 :(得分:6)
尝试这样
if let userId = data["userId"] as? String {
completionHandler([.alert, .badge, .sound])
} else {
completionHandler([])
}