我想在我的应用程序处于前台时收到本地通知,当我尝试使用以下代码时,它永远不会触发通知,但是当我在后台输入应用程序时,它确实触发了。
这是我尝试过的:
//Schedule a Local Notification
func ScheduleNotification(timeInterval: Double, repeats: Bool, notificationBody:String, title:String){
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: timeInterval, repeats: repeats)
let center = UNUserNotificationCenter.current()
let identifier = "UYLLocalNotification"
let content = UNMutableNotificationContent()
content.title = title
content.body = notificationBody
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request, withCompletionHandler: { (error) in
if let error = error {
//Something went wrong
print(error.localizedDescription)
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
if Currentcount < data.count {
self.ScheduleNotification(timeInterval: 5.0, …Run Code Online (Sandbox Code Playgroud)