本地通知
UILocalNotification *notification = [[UILocalNotification alloc] init];
if(notification != nil ) {
NSDate *now = [NSDate new];
notification.fireDate=[now dateByAddingTimeInterval:6]; //觸發(fā)通知的時間
notification.repeatInterval=0; //循環(huán)次數(shù)菠劝,kCFCalendarUnitWeekday一周一次
notification.soundName = UILocalNotificationDefaultSoundName;
notification.alertBody=@"該去吃晚飯了疼进!";
notification.alertAction = @"打開"; //提示框按鈕
notification.hasAction = YES; //是否顯示額外的按鈕晓殊,為no時alertAction消失
notification.applicationIconBadgeNumber = 1; //設(shè)置app圖標(biāo)右上角的數(shù)字
//下面設(shè)置本地通知發(fā)送的消息,這個消息可以接受
NSDictionary* infoDic = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
notification.userInfo = infoDic;
////發(fā)送通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
接收本地消息
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"LocalNotification" message:notification.alertBody delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
[alert show];
NSDictionary* dic = [[NSDictionary alloc]init];
//這里可以接受到本地通知中心發(fā)送的消息
dic = notification.userInfo;
application.applicationIconBadgeNumber -= 1;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
application.applicationIconBadgeNumber -= 1;
}
其他
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"test" object:nil];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"test" object:nil];
}
發(fā)送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil];