如何在 iOS 应用程序上设置每隔一 (1) 小时重复一次的通知?
How to put notifications on iOS application to repeat every one (1) hour?
我试着在我的应用程序中添加通知,它应该每隔一小时重复一次,但它重复不受监管,需要明确的是,它有时重复 30 分钟,有时重复一小时,有时重复很长时间等。
我在 "AppDelegate.swift":
中使用的代码
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//Notification Repeat
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil))
return true
}
以及我在 "ViewController.swift" 中使用的代码:
//Notification Repeat
var Time = 1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Notification Repeat
var Timer = NSTimer.scheduledTimerWithTimeInterval(3600.0, target: self, selector: Selector("activateNotifications"), userInfo: nil, repeats: true)
}
//Notification Repeat
func activateNotifications() {
Time -= 1
if (Time <= 0){
var activateNotifications = UILocalNotification()
activateNotifications.alertAction = “Hey"
activateNotifications.alertBody = “Hello World!"
activateNotifications.fireDate = NSDate(timeIntervalSinceNow: 0)
UIApplication.sharedApplication().scheduleLocalNotification(activateNotifications)
}
}
谁能帮帮我,我错在哪里?
您根本不需要计时器。 UILocalNotification
class 有一个名为 repeatInterval
的 属性,如您所料,它设置重复通知的时间间隔。
据此,您可以按以下方式安排每小时重复一次的本地通知:
func viewDidLoad() {
super.viewDidLoad()
var notification = UILocalNotification()
notification.alertBody = "..." // text that will be displayed in the notification
notification.fireDate = NSDate() // right now (when notification will be fired)
notification.soundName = UILocalNotificationDefaultSoundName // play default sound
notification.repeatInterval = NSCalendarUnit.CalendarUnitHour // this line defines the interval at which the notification will be repeated
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
注意:请确保仅在启动通知时执行一次代码,因为它每次执行时都会安排不同的通知。为了更好地理解本地通知,您可以阅读Local Notifications in iOS 8 with Swift (Part 1) and Local Notifications in iOS 8 with Swift (Part 2)。
我试着在我的应用程序中添加通知,它应该每隔一小时重复一次,但它重复不受监管,需要明确的是,它有时重复 30 分钟,有时重复一小时,有时重复很长时间等。 我在 "AppDelegate.swift":
中使用的代码class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//Notification Repeat
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil))
return true
}
以及我在 "ViewController.swift" 中使用的代码:
//Notification Repeat
var Time = 1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Notification Repeat
var Timer = NSTimer.scheduledTimerWithTimeInterval(3600.0, target: self, selector: Selector("activateNotifications"), userInfo: nil, repeats: true)
}
//Notification Repeat
func activateNotifications() {
Time -= 1
if (Time <= 0){
var activateNotifications = UILocalNotification()
activateNotifications.alertAction = “Hey"
activateNotifications.alertBody = “Hello World!"
activateNotifications.fireDate = NSDate(timeIntervalSinceNow: 0)
UIApplication.sharedApplication().scheduleLocalNotification(activateNotifications)
}
}
谁能帮帮我,我错在哪里?
您根本不需要计时器。 UILocalNotification
class 有一个名为 repeatInterval
的 属性,如您所料,它设置重复通知的时间间隔。
据此,您可以按以下方式安排每小时重复一次的本地通知:
func viewDidLoad() {
super.viewDidLoad()
var notification = UILocalNotification()
notification.alertBody = "..." // text that will be displayed in the notification
notification.fireDate = NSDate() // right now (when notification will be fired)
notification.soundName = UILocalNotificationDefaultSoundName // play default sound
notification.repeatInterval = NSCalendarUnit.CalendarUnitHour // this line defines the interval at which the notification will be repeated
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
注意:请确保仅在启动通知时执行一次代码,因为它每次执行时都会安排不同的通知。为了更好地理解本地通知,您可以阅读Local Notifications in iOS 8 with Swift (Part 1) and Local Notifications in iOS 8 with Swift (Part 2)。