gpt4 book ai didi

swift - 应用程序被终止时 UNUserNotificationCenter 不起作用

转载 作者:行者123 更新时间:2023-11-30 12:46:12 26 4
gpt4 key购买 nike

我刚刚对 iOs 10 和其他版本的通知进行了更改:

if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in

let content = UNMutableNotificationContent()

content.body = notifMessage!
content.sound = UNNotificationSound.default()
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "Upload", content: content, trigger: trigger)

// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request)
}
} else {
let notification = UILocalNotification()
notification.alertBody = notifMessage
notification.fireDate = NSDate() as Date
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(notification)
}

当我通过将设备与 USB 连接来在设备上运行应用程序时,它可以工作,但只有当应用程序处于后台时,它才不会在以下情况下工作:

  • 我杀死了应用程序

  • 当应用程序显示时

最佳答案

如果您终止该应用程序(通过双击主页按钮然后向上滑动),这不仅会终止该应用程序,还会禁止该应用程序的进一步后台操作(直到用户再次启动它)。您只需按主页按钮,然后通过正常的内存恢复过程就可以放弃该应用程序。或者,出于测试目的,您可以以编程方式使应用程序崩溃。但是您不能使用跳板(双击主页按钮技巧),因为会影响应用程序允许的背景模式。

关于显示应用程序时的通知、用户点击通知以及用户手动启动应用程序而忽略通知,这些都以不同的方式传达给应用程序。请参阅 UIApplicationDelegate documentation响应通知和事件部分。或者参见App Programming Guide for iOS: Background Execution有关后台操作的一般信息。

关于swift - 应用程序被终止时 UNUserNotificationCenter 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41614386/

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