安排本地通知
确保你看到注册本地通知以使其正常工作:
迅速
let notification = UILocalNotification()
notification.alertBody = "Hello, local notifications!"
notification.fireDate = NSDate().dateByAddingTimeInterval(10) // 10 seconds after now
UIApplication.sharedApplication().scheduleLocalNotification(notification)
Objective-C
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Hello, local notifications!";
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10]; // 10 seconds after now
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
要在 iOS 模拟器中查看通知,请键入^⌘H
(control-command-H)返回主页,然后键入⌘L
(command-L)以锁定设备。等待几秒钟,然后会出现通知(此外观将根据注册本地通知中讨论的通知类型而有所不同):
滑动通知以返回应用程序(请注意,如果你在第一个视图控制器的 viewDidLoad
,viewWillAppear
,viewDidAppear
等中调用此通知,则会再次安排通知)。