首页 > 解决方案 > 无法在 Swift (UIKIT) 中生成本地通知

问题描述

在此处输入图像描述

我正在尝试通过单击一个按钮来测试本地用户通知,但是当应用程序打开时它不起作用它只在它处于后台时被触发,为什么会这样?

标签: swiftuikit

解决方案


UNUserNotificationCenter.current().delegate = self

使用 UNUserNotificationCenter 分配委托并添加以下方法。

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

此 UNNotificationPresentationOptions “横幅”在应用程序处于前台时显示通知。


推荐阅读