ios - 本地通知数据

标签 ios swift

大家好,我在 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 执行此操作方法,但如果您的应用程序不在前台,则该应用程序只有在从通知中打开(滑动或点击它)时才会知道通知。

关于ios - 本地通知数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55903409/

相关文章:

ios - 我应该在我的 iOS (Swift) 应用程序生命周期中的哪个位置从 firebase 获取我的所有数据?

ios - 如何快速释放 CGRect、CGPoint 类型?

ios - 对象在 block 内初始化后变为 nil

ios - 如何从 WKWebview 中删除或隐藏 PDF 的页码计数器?

ios - SpriteKit + Swift 中的多点触控手势识别

ios - Fabric.with Twitter 无故崩溃

swift - ARCore + RealityKit – 云 anchor 和 AnchorEntities

swift - 使用 NSNumberFormatter 时的 EXC_BAD_ACCESS(代码=2)

ios - 在 iOS 应用程序中使用 Braintree 支付 api

ios - 在 iOS 上使用 LLVM 编译时,Sqlite FTS 不起作用