- (void)viewDidLoad
{
[super viewDidLoad];
UILocalNotification *notification = [[UILocalNotification alloc]init];
if (notification != nil) {NSLog(@">>支持本地通知");
NSDate *now = [NSDate date];
notification.fireDate = [now dateByAddingTimeInterval:5]; //系统产生本地消息的时间
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = @"给吃饭了!";
notification.hasAction = YES;
notification.alertAction = @"Open App";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
//自定义数据,本地通知可以包含一个dictionary类型的本地数据
NSDictionary *data = [NSDictionary dictionaryWithObject:now forKey:@"time"];
notification.userInfo = data;
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
}
}
在AppDelegate.m文件里当APP接受到本地消息的时间
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
application.applicationIconBadgeNumber--;
NSLog(@"receive local notification at %@!",[notification.userInfo objectForKey:@"time"]);
}