我试图理解为什么本地通知没有显示在前台。我相信我添加了所有正确的代码来允许此功能,但当我的应用程序位于前台时没有显示横幅。
这是我添加的内容AppDelegate.swift
:
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
{
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert, UNAuthorizationOptions.sound, UNAuthorizationOptions.badge]) { (willAllow: Bool, error: Error?) in
if willAllow == true
{
// Do something
}
}
UNUserNotificationCenter.current().delegate = self
// Override point for customization after application launch.
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
print("GO IN HERE")
completionHandler(.alert)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
}
}
My ViewController.swift
:
override func viewDidLoad()
{
super.viewDidLoad()
let content: UNMutableNotificationContent = UNMutableNotificationContent()
content.sound = UNNotificationSound.default()
content.subtitle = "This is a test"
let trigger: UNTimeIntervalNotificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request: UNNotificationRequest = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
当我的应用程序第一次启动时,我确实看到了一个权限警报,允许我的应用程序推送通知,但我根本没有看到任何通知。
我确实看到了日志输出进入这里, so willPresent
正在被呼叫。
我还缺少其他东西吗?