大家好,我在iOS
应用程序中遇到了一个问题。我通过互联网搜索并没有找到解决方案。大多数解决方案都使用已弃用
func application(_ application: UIApplication, didReceive notification: UILocalNotification)
所以情况如下:
应用程序安排每天5本地通知,这是可以的,并且有效:
var dateComponents = DateComponents()
dateComponents.weekday = Date().dayNumberOfWeek()
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.second = 5
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: "identifier", content: notifcation, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
但我的问题是:是否有可能找出何时触发通知而不管应用程序状态并从该通知中获取数据?
因为从API检索每个通知的时间,我不知道何时(小时和分钟)通知将被安排。
例如在Android中我使用AlarmManager
API安排通知,而不是通过扩展BroadcastReciver
,我可以获取每个触发的通知并获取通过Intent
发送的数据。
我需要找出当天触发的最后一个(第五个)通知,以便我可以执行一些额外的工作。
简而言之,无论应用程序状态如何,都无法收到通知。
如果应用程序在前台运行,您可以使用UNUserNotificationCenterDelegate方法执行此操作,但如果您的应用程序不在前台,则应用程序只有在通知打开时才会知道该通知(滑动或点击它)。