使用Swift每周重复本地通知

时间:2022-10-19 01:06:25

I have an app using local notifications there is a total of 64 notifications, which is the limit of local notifications. I need to repeat each notification every week. I know that to set the repeat interval you use:

我有一个使用本地通知的应用程序,总共有64个通知,这是本地通知的限制。我需要每周重复一次通知。我知道要设置您使用的重复间隔:

alarm.repeatInterval = NSCalendarUnit

I have tried using the .WeekOfYear and .WeekOfMonth. Do they repeat the notification every year or month? And I do not know the calendar unit for weekly. Which one can I use to repeat weekly?

我尝试过使用.WeekOfYear和.WeekOfMonth。他们每年或每月重复通知吗?我不知道每周的日历单位。我可以用哪一个每周重复一次?

Edit:

编辑:

This is the code I am using to set the notifications.

这是我用来设置通知的代码。

let notifyAlarm = UILocalNotification()
let component = NSDateComponents()
component.hour = NSUserDefaults.standardUserDefaults().integerForKey("Hour1")
component.minute = NSUserDefaults.standardUserDefaults().integerForKey("Minute1")
component.weekday = 1

notifyAlarm.fireDate = calendar.dateFromComponents(component)
notifyAlarm.timeZone = NSTimeZone.defaultTimeZone()
notifyAlarm.alertBody = NSUserDefaults.standardUserDefaults().stringForKey("Message1")
notifyAlarm.repeatInterval = NSCalendarUnit.WeekdayOrdinal
notifyAlarm.soundName = NSUserDefaults.standardUserDefaults().stringForKey("Sound1")
app.scheduleLocalNotification(notifyAlarm)

I am setting 64 notifications like that at once. But with different dates.

我一次设置64个这样的通知。但是有不同的约会。

1 个解决方案

#1


4  

If you want the notification to fire for first time after a week you need to change the fire date. I use TimeIntervalSinceNow for this, which is in seconds, so 1 week would be around 604000 seconds. You can use _ to separate numbers for legibility.

如果您希望在一周后第一次触发通知,则需要更改开火日期。我使用TimeIntervalSinceNow,这是以秒为单位,因此1周将是604000秒。您可以使用_来分隔数字以便易读。

alarm.fireDate = NSDate(timeIntervalSinceNow: 604_000)

Maybe a bit clunky but I think its the easiest for those type of notifications. I do something like this to make it easier.

也许有点笨重,但我认为这对于那些类型的通知最简单。我这样做是为了让它变得更容易。

struct NotificationFireDate {
    static let nextDay: NSTimeInterval = 85_000
    static let nextWeek: NSTimeInterval = 604_000
}

and than use it like so

而不是像这样使用它

alarm.fireDate = NSDate(timeIntervalSinceNow: NotificationFireDate.nextWeek)

Repeat interval should be weekOfYear

重复间隔应为weekOfYear

alarm.repeatInterval = NSCalendarUnit.WeekOfYear

The first repeating notification should fire 1 week after the first (fireDate) notification fired.

第一个重复通知应在第一个(fireDate)通知触发后1周触发。

For a full list have a look at this (thanks madmik3)

如需完整列表,请查看此内容(感谢madmik3)

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/c/tdef/NSCalendarUnit

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/c/tdef/NSCalendarUnit

#1


4  

If you want the notification to fire for first time after a week you need to change the fire date. I use TimeIntervalSinceNow for this, which is in seconds, so 1 week would be around 604000 seconds. You can use _ to separate numbers for legibility.

如果您希望在一周后第一次触发通知,则需要更改开火日期。我使用TimeIntervalSinceNow,这是以秒为单位,因此1周将是604000秒。您可以使用_来分隔数字以便易读。

alarm.fireDate = NSDate(timeIntervalSinceNow: 604_000)

Maybe a bit clunky but I think its the easiest for those type of notifications. I do something like this to make it easier.

也许有点笨重,但我认为这对于那些类型的通知最简单。我这样做是为了让它变得更容易。

struct NotificationFireDate {
    static let nextDay: NSTimeInterval = 85_000
    static let nextWeek: NSTimeInterval = 604_000
}

and than use it like so

而不是像这样使用它

alarm.fireDate = NSDate(timeIntervalSinceNow: NotificationFireDate.nextWeek)

Repeat interval should be weekOfYear

重复间隔应为weekOfYear

alarm.repeatInterval = NSCalendarUnit.WeekOfYear

The first repeating notification should fire 1 week after the first (fireDate) notification fired.

第一个重复通知应在第一个(fireDate)通知触发后1周触发。

For a full list have a look at this (thanks madmik3)

如需完整列表,请查看此内容(感谢madmik3)

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/c/tdef/NSCalendarUnit

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/c/tdef/NSCalendarUnit