本文介绍了iOS 通知触发器:每两周和/或每季度一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到任何适用于这种情况的 Apple 文档,我尝试了各种方法来做到这一点,但总是空空如也.

I can't seem to find any Apple Documentation for this exact scenario, and I've tried various ways to do this and I keep coming up empty.

我想安排重复通知(iOS 10+ 所以UNCalendarNotificationTrigger 或等效的).
这些是本地通知不是推送通知.

I would like to schedule a repeating notification (iOS 10+ so UNCalendarNotificationTrigger or equivalent).
These are Local Notifications not Push Notifications.

安排重复的通知:

  • 每两周一次(例如,每第二次周二)
  • 每季度一次(例如,每 3 个月的第一天)
  • once a fortnight (e.g., every second Tuesday)
  • once a quarter (e.g., 1st of every 3 months)

这些触发器运行良好,并且易于实现(在 Swift Playground 中运行代码).

These triggers work well, and are simple to implement (Running the code in a Swift Playground).

// Every day at 12pm
var daily = DateComponents()
daily.hour = 12
let dailyTrigger = UNCalendarNotificationTrigger(dateMatching: daily, repeats: true)
dailyTrigger.nextTriggerDate() // "Jan 4, 2017, 12:00 PM"

// Every Tuesday at 12pm
var weekly = DateComponents()
weekly.hour = 12
weekly.weekday = 3
let weeklyTrigger = UNCalendarNotificationTrigger(dateMatching: weekly, repeats: true)
weeklyTrigger.nextTriggerDate() // "Jan 10, 2017, 12:00 PM"

// The 1st of every month at 12pm
var monthly = DateComponents()
monthly.hour = 12
monthly.day = 1
let monthlyTrigger = UNCalendarNotificationTrigger(dateMatching: monthly, repeats: true)
monthlyTrigger.nextTriggerDate() // "Feb 1, 2017, 12:00 PM"

// Every 1st of February at 12pm
var yearly = DateComponents()
yearly.hour = 12
yearly.day = 1
yearly.month = 2
let yearlyTrigger = UNCalendarNotificationTrigger(dateMatching: yearly, repeats: true)
yearlyTrigger.nextTriggerDate() // "Feb 1, 2017, 12:00 PM"

但是...

我似乎无法让 每两周每季度 触发器正常运行.

But...

I can't seem to get a fortnightly or quarterly trigger to function correctly.

// Every second Tuesday at 12pm
// ... There is no "date.fortnight", is this possible?

// The 1st of every quarter at 12pm
var quarterly = DateComponents()
quarterly.hour = 12
quarterly.day = 4
// Values: 1, 2, 3 or 4 all produce the same "nextTriggerDate" - why?
quarterly.quarter = 4
let quarterlyTrigger = UNCalendarNotificationTrigger(dateMatching: quarterly, repeats: true)
quarterlyTrigger.nextTriggerDate()

所以,明确地说,我的问题是:

So, to be clear, my questions are:

  1. 是否可以获得每两周重复一次的通知?
  2. 我们如何获得一个季度一次的触发器?

由于 DateComponents() 有一个 quarter 单位,我假设 quarterly 触发器是可能的.然而,对于每两周一次的提醒,我什至不确定这是否可能......

Since DateComponents() has a quarter unit, I assume that a quarterly trigger is possible. For the fortnightly reminder however, I'm not even sure if this is possible...

任何见解将不胜感激!

推荐答案

我没有看到任何触发两周通知的直接选项.来自我的建议.

I didn't see any direct option to trigger fortnight notification.Suggestion from my end.

1) 是否可以收到每两周重复一次的通知?

我提出两个选项:

  1. 我们可以使用 UNTimeIntervalNotificationTrigger 以两周的时间间隔重复通知吗?下面的例子

  1. Can we use UNTimeIntervalNotificationTrigger to repeat the notification with a time interval of two weeks time? Example below

let timeInterValFor2Weeks = 1190507.790425003

let intervalTrigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterValFor2Weeks, repeats: true)//"Jan 3, 2017, 5:24 PM"

intervalTrigger.nextTriggerDate() //"Jan 17, 2017, 12:05 PM"

  • 安排两个 UNCalendarNotificationTrigger 触发器,应该触发一个月的第一天和第三天.例如,它应该在一个月的第一个星期日和第三个星期日发出通知.

  • Schedule two UNCalendarNotificationTrigger trigger, which should trigger first and third day of a month. For example it should fire notification in first Sunday and third Sunday of a month.

    var fortnightPart1 = DateComponents()
    fortnightPart1.weekday = 1 //(Day..here Sunday)
    fortnightPart1.weekdayOrdinal = 2 //= n == (nth Day in the month...here 2nd Sunday in every month month)
    fortnightPart1.hour = 12
    
    let fortnightTrigger = UNCalendarNotificationTrigger(dateMatching: fortnightPart1, repeats: true)
    fortnightPart1.nextTriggerDate()
    

  • 2) 我们如何获得一个季度一次的触发器?

    如果没有直接选项可用,那么我建议与上述相同的解决方案.

    If there is no direct option available, then I suggest the same solution as above.

    这篇关于iOS 通知触发器:每两周和/或每季度一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-24 01:09