iOS UILocationNotification 本地通知
来源:互联网 发布:3d扫描数据采集超声波 编辑:程序博客网 时间:2024/04/30 03:48
1.注册本地通知
/** 注册本地通知 */- (void)registerUserNotification { // 判读系统版本是否是“iOS 8.0”以上 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 || [UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { // 定义用户通知类型(Remote.远程 - Badge.标记 Alert.提示 Sound.声音) UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound; // 定义用户通知设置 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; // 注册用户通知 - 根据用户通知设置 [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; }}
2.创建一个本地通知
NSInteger time = _textField.text.integerValue; UILocalNotification *localNotification = [[UILocalNotification alloc] init]; if (localNotification) { // 使用本地时区 localNotification.timeZone = [NSTimeZone defaultTimeZone]; //设置触发通知时间 localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:time]; //通知参数 localNotification.userInfo = @{@"type":[NSString stringWithFormat:@"test:%ld",(long)time],@"time":@(time)};; //通知声音 localNotification.soundName = UILocalNotificationDefaultSoundName; //通知内容 localNotification.alertBody = @"test"; localNotification.repeatInterval = 0;//不重复 //注册通知 [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; }
3.收到本地通知
- 如果应用在前台,会在- (void)application:(UIApplication )application didReceiveLocalNotification:(UILocalNotification )notification 中收到通知。
- 如果应用是关闭或者在后台,会在状态栏收到通知。
通过点击状态栏通知打开应用,在- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions 中,通过UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
可以获取本地通知信息。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self registerUserNotification]; [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; //本地通知处理 UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if ([localNotification isKindOfClass:[UILocalNotification class]]) { //---1 NSLog(@"通知内容1%@",localNotification);// [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; } [self p_showBtnAction]; return YES;}- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) { //-----2 NSLog(@"收到本地通知"); }else if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive){ //----3 AudioServicesPlaySystemSound(1007); } NSString * msg = [[NSString alloc] initWithFormat:@"%@-%lu-%@",notification.userInfo,(unsigned long)notification.repeatInterval,notification.repeatCalendar ]; UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"通知" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alertView show]; NSLog(@"收到通知:%@",notification); NSLog(@"============="); [self p_showBtnAction];}
在1 的地方可以处理本地通知,进行页面跳转等操作;
如果不在1的地方处理,可以调用[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 这样didReceiveLocalNotification会被调用。
在2的地方表示点击了状态栏的通知消息,可在此进行操作。
在3的地方添加声音提示,应用在前台时收到本地通知会被调用。
需要注意的地方
for (UILocalNotification* noti in [UIApplication sharedApplication].scheduledLocalNotifications) {
NSLog(@"通知:%@",noti);
}
可遍历所有本地通知。如果 localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];立即通知的,通知不会加入到 [UIApplication sharedApplication].scheduledLocalNotifications数组中。
- 走到didReceiveLocalNotification的通知,会自动从[UIApplication sharedApplication].scheduledLocalNotifications数组中删除。 4.
0 0
- iOS 本地通知 UILocationNotification
- iOS UILocationNotification 本地通知
- UILocationNotification
- iOS 本地通知 远程通知
- iOS本地通知 远程通知
- iOS本地通知&定时通知
- iOS实现本地通知
- iOS本地通知
- iOS本地通知
- iOS本地通知UILocalNotification
- IOS实现本地通知
- IOS 本地通知 UILocalNotification
- ios本地通知UILocalNotification
- iOS UILocalNotification本地通知
- iOS实现本地通知
- iOS实现本地通知
- IOS实现本地通知
- iOS-本地通知
- 数据结构实验之排序一:一趟快排
- fzu1021
- HDU2063:过山车
- 51nod oj 1028 大数乘法 V2 1029 大数除法【java大数】
- 萌萌的websocket原理解析
- iOS UILocationNotification 本地通知
- C语言中变量声明和定义
- android lcd 背光控制流程 .
- ACdream 1015Double Kings
- 多线程VS多进程
- IO流——重定向流
- 2047. 对布满灰尘的西洋棋宣告将军
- 【NOIP提高】Binary
- poj1200 hash入门