我正在应用程序中实现聊天功能。现在,每当我使用应用程序时,我都会使用它来显示通知:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
我想知道是否有可能在保持特定视图控制器的蜂鸣器的同时禁止在顶部显示推送通知?
如果是,我应该怎么做?
答案 0 :(得分:3)
像这样实现它:
UNUserNotificationCenter.current().delegate = self
...
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if wantToShow {
completionHandler([.alert, .sound])
} else {
completionHandler([])
}
}
然后根据当前状态更改wantToShow
。