在前台的特定视图控制器中禁用推送通知?

时间:2018-07-19 11:40:54

标签: ios swift apple-push-notifications

我正在应用程序中实现聊天功能。现在,每当我使用应用程序时,我都会使用它来显示通知:

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

我想知道是否有可能在保持特定视图控制器的蜂鸣器的同时禁止在顶部显示推送通知?

如果是,我应该怎么做?

1 个答案:

答案 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