gpt4 book ai didi

ios - 为什么我的本地通知没有在 iOS 10 的前台触发?

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:36 26 4
gpt4 key购买 nike

我想了解为什么本地通知没有显示在前台。我相信我添加了所有正确的代码以允许此功能,但当我的应用程序在前台时没有显示横幅。

这是我在 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)
{

}

}

我的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)
}

当我的应用首次启动时,我确实看到了允许我的应用推送通知的权限警告,但我根本没有看到任何通知。

我确实看到了日志输出GO IN HERE,因此调用了willPresent

还有什么我想念的吗?

最佳答案

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


let center = UNUserNotificationCenter.current()
center.delegate = self

UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound]) {(accepted, error) in
if !accepted {
print("Notification access denied")
}
}
}

在 AppDelegate 中添加:

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


completionHandler( [.alert, .badge, .sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}

关于ios - 为什么我的本地通知没有在 iOS 10 的前台触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43221455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com