【问题标题】:ios - waiting 24hrs to send a notificationios - 等待 24 小时发送通知
【发布时间】:2021-04-20 18:52:27
【问题描述】:

我们的应用程序每天早上 8 点发送通知,向用户打招呼并提醒他完成任务。这是我们正在使用的脚本:

    let center = UNUserNotificationCenter.current()
    center.removePendingNotificationRequests(withIdentifiers: ["dailyReminder"])
    
    let content = UNMutableNotificationContent()
    content.title = "Good morning"
    content.body = notificationMessages[randomIndex]
    content.sound = .default
    
    var dateComponents = DateComponents()
    dateComponents.hour = 8

    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
    let request = UNNotificationRequest(identifier: "dailyReminder", content: content, trigger: trigger)
    center.add(request)

这是在用户完成当天的任务后调用的,以便他们在第二天得到提示。但是,有些人会在早上 8 点之前完成任务。在这种情况下,我们希望在第二天早上 8 点发送通知。

我如何计算脚本是在 8 点之前还是之后运行并相应地添加 24 小时?

【问题讨论】:

    标签: swift nsdatecomponents


    【解决方案1】:

    由于这是在本地运行的,因此您可以通过将函数保存在 UserDefault 中来保存上次发送函数的时间。例如,将以下代码添加到用户上次完成任务时保存的代码:

    UserDefaults.standard.set("/(youWillWriteAFunctionForDate)", forKey: "checkTaskOfDate")
    

    然后您可以简单地检查它是否是今天发送的,并做出相应的计划:

    defaults.string(forKey: "checkTaskOfDate") ?? "Never Done"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多