swift 每天在设定的时间重复本地通知
Repeating local notification daily at a set time with swift
我是 iOS 开发的新手,但已经创建了该应用程序,并且我正在尝试创建一个设定时间的每日通知。当前,通知针对给定的 date/time 执行一次。我不确定如何使用 repeatInterval 方法每天安排它。每天重复通知的最佳方法是什么?任何帮助将不胜感激 (Y)。
var dateComp:NSDateComponents = NSDateComponents()
dateComp.year = 2015;
dateComp.month = 06;
dateComp.day = 03;
dateComp.hour = 12;
dateComp.minute = 55;
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var date:NSDate = calender.dateFromComponents(dateComp)!
var notification:UILocalNotification = UILocalNotification()
notification.category = "Daily Quote"
notification.alertBody = quoteBook.randomQuote()
notification.fireDate = date
notification.repeatInterval =
UIApplication.sharedApplication().scheduleLocalNotification(notification)
您必须提供一个 NSCalendarUnit 值,例如“HourCalendarUnit”或“DayCalendarUnit”才能重复通知。
只需添加此代码即可每天重复本地通知:
notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
var repeatInterval: NSCalendarUnit
=> 文档说 "The calendar interval at which to reschedule the notification."
所以:使用NSCalendarUnit.CalendarUnitDay
因此,不得不稍微修改@vizllx 的上述代码。这是新行:
notification.repeatInterval = NSCalendarUnit.Day
这是我使用的完整示例:
let notification = UILocalNotification()
/* Time and timezone settings */
notification.fireDate = NSDate(timeIntervalSinceNow: 8.0)
notification.repeatInterval = NSCalendarUnit.Day
notification.timeZone = NSCalendar.currentCalendar().timeZone
notification.alertBody = "A new item is downloaded."
/* Action settings */
notification.hasAction = true
notification.alertAction = "View"
/* Badge settings */
notification.applicationIconBadgeNumber =
UIApplication.sharedApplication().applicationIconBadgeNumber + 1
/* Additional information, user info */
notification.userInfo = [
"Key 1" : "Value 1",
"Key 2" : "Value 2"
]
/* Schedule the notification */
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
其他答案显示了如何使用 iOS 10 之前的旧本地通知。对于 iOS 10 及更高版本,您必须按如下方式使用 UNNotificationCenter:
let content = UNMutableNotificationContent()
content.title = "This will appear in bold, on it's own line."
content.subtitle = "This will appear in bold, on it's own line, below the title."
content.body = "This will appear as normal text, below the title and subtitle."
let triggerInputForHourlyRepeat = Calendar.current.dateComponents([.minute], from: intendedFireDateVariable)
let trigger = UNCalendarNotificationTrigger.init(dateMatching: triggerInput, repeats: true)
let request = UNNotificationRequest(identifier: "someUniqueID", content: content, trigger: trigger)
let unc = UNUserNotificationCenter.current()
unc.add(request, withCompletionHandler: { (error) in
/// Handle error
})
有用的教程:
我是 iOS 开发的新手,但已经创建了该应用程序,并且我正在尝试创建一个设定时间的每日通知。当前,通知针对给定的 date/time 执行一次。我不确定如何使用 repeatInterval 方法每天安排它。每天重复通知的最佳方法是什么?任何帮助将不胜感激 (Y)。
var dateComp:NSDateComponents = NSDateComponents()
dateComp.year = 2015;
dateComp.month = 06;
dateComp.day = 03;
dateComp.hour = 12;
dateComp.minute = 55;
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var date:NSDate = calender.dateFromComponents(dateComp)!
var notification:UILocalNotification = UILocalNotification()
notification.category = "Daily Quote"
notification.alertBody = quoteBook.randomQuote()
notification.fireDate = date
notification.repeatInterval =
UIApplication.sharedApplication().scheduleLocalNotification(notification)
您必须提供一个 NSCalendarUnit 值,例如“HourCalendarUnit”或“DayCalendarUnit”才能重复通知。
只需添加此代码即可每天重复本地通知:
notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
var repeatInterval: NSCalendarUnit
=> 文档说 "The calendar interval at which to reschedule the notification."
所以:使用NSCalendarUnit.CalendarUnitDay
因此,不得不稍微修改@vizllx 的上述代码。这是新行:
notification.repeatInterval = NSCalendarUnit.Day
这是我使用的完整示例:
let notification = UILocalNotification()
/* Time and timezone settings */
notification.fireDate = NSDate(timeIntervalSinceNow: 8.0)
notification.repeatInterval = NSCalendarUnit.Day
notification.timeZone = NSCalendar.currentCalendar().timeZone
notification.alertBody = "A new item is downloaded."
/* Action settings */
notification.hasAction = true
notification.alertAction = "View"
/* Badge settings */
notification.applicationIconBadgeNumber =
UIApplication.sharedApplication().applicationIconBadgeNumber + 1
/* Additional information, user info */
notification.userInfo = [
"Key 1" : "Value 1",
"Key 2" : "Value 2"
]
/* Schedule the notification */
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
其他答案显示了如何使用 iOS 10 之前的旧本地通知。对于 iOS 10 及更高版本,您必须按如下方式使用 UNNotificationCenter:
let content = UNMutableNotificationContent()
content.title = "This will appear in bold, on it's own line."
content.subtitle = "This will appear in bold, on it's own line, below the title."
content.body = "This will appear as normal text, below the title and subtitle."
let triggerInputForHourlyRepeat = Calendar.current.dateComponents([.minute], from: intendedFireDateVariable)
let trigger = UNCalendarNotificationTrigger.init(dateMatching: triggerInput, repeats: true)
let request = UNNotificationRequest(identifier: "someUniqueID", content: content, trigger: trigger)
let unc = UNUserNotificationCenter.current()
unc.add(request, withCompletionHandler: { (error) in
/// Handle error
})
有用的教程: