标签:
本地通知是由本地应用触发的,它是基于时间行为的一种通知形式
步骤:
AppDelegate.h文件中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //兼容iOS7.0及以后的版本, 版本适配 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]]; } //被杀掉进程后,接收本地通知 UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (notification) { //应用程序是点击本地通知启动的 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:notification.userInfo[@"notification"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } return YES; } #pragma mark 调用过用户注册通知方法之后执行(也就是调用完registerUserNotificationSettings:方法之后执行) //接收本地通知, 应用程序在后台 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ NSLog(@"%@", notification.userInfo[@"notification"]); //取消本地通知 [application cancelLocalNotification:notification]; } #pragma mark 进入前台后设置消息信息 - (void)applicationWillEnterForeground:(UIApplication *)application { [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];// 默认0 进入前台取消应用消息图标 }
.m文件中
- (IBAction)addNotification:(id)sender { //1.注册通知 //实例化通知对象 UILocalNotification *notification = [[UILocalNotification alloc] init]; //通知发出的时间, 当前时间7秒钟之后 notification.fireDate = [[NSDate date] dateByAddingTimeInterval:7]; //设置循环提醒 //循环标准的日历 notification.repeatCalendar = [NSCalendar currentCalendar]; //循环的标准 通知重复次数 notification.repeatInterval = NSCalendarUnitMinute; //时区 notification.timeZone = [NSTimeZone defaultTimeZone]; //提醒的内容 notification.alertBody = @"今天没吃药,吃药时间到了。"; notification.alertAction = @"打开"; //待机界面的滑动动作提示 //提醒的声音 notification.soundName = @"msgcome.wav"; notification.alertLaunchImage=@"";//通过点击通知打开应用时的启动图片 //应用程序图标右上角显示的消息数 notification.applicationIconBadgeNumber = 1; //附加的备注信息 notification.userInfo = @{@"notification" : @"吃药"}; //调用通知 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }
效果:
7秒之后:
点击后进入程序:
标签:
原文地址:http://www.cnblogs.com/10-19-92/p/4882159.html