iOS 本地推送
2016-05-16 22:06
375 查看
众所周知,iOS中的通知推送分为:本地和远程两种。本地推送主要用于日历闹钟等提醒类应用中,APNS远程推送以后专门写一篇介绍,本文暂时按下不表。今天的主角是:UILocalNotification
想要通知有效,需要在 application: didFinishLaunchingWithOptions:里注册通知:
效果图如下:
收到通知后的处理可以写在方法 application:(UIApplication *)application didReceiveLocalNotification: 里面:
启动通知
创建一个本地通知://本地通知 UILocalNotification *localNoti = [[UILocalNotification alloc] init]; //时区 localNoti.timeZone = [NSTimeZone defaultTimeZone]; //触发时间 localNoti.fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0]; //重复间隔 localNoti.repeatInterval = kCFCalendarUnitSecond; //通知内容 localNoti.alertBody = @"欢迎来到英雄联盟"; //通知声音 localNoti.soundName = UILocalNotificationDefaultSoundName; //通知徽标 [UIApplication sharedApplication].applicationIconBadgeNumber += 1; //通知参数 localNoti.userInfo = [NSDictionary dictionaryWithObject:@"传递信息内容" forKey:@"通知的Key"]; //发送通知: [[UIApplication sharedApplication] scheduleLocalNotification:localNoti];
想要通知有效,需要在 application: didFinishLaunchingWithOptions:里注册通知:
//注册通知: if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; }
效果图如下:
收到通知后的处理可以写在方法 application:(UIApplication *)application didReceiveLocalNotification: 里面:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"本地通知" message:[notification.userInfo allValues].firstObject preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cacel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"取消"); }]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"确认"); }]; [alert addAction:cacel]; [alert addAction:action]; [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
取消通知
取消一个本地通知:// 获取所有本地通知数组 NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications; for (UILocalNotification *notification in localNotifications) { NSDictionary *userInfo = notification.userInfo; if (userInfo) { // 根据设置通知参数时指定的key来获取通知参数 NSString *info = userInfo[key]; // 如果找到需要取消的通知,则取消 if (info != nil) { [[UIApplication sharedApplication] cancelLocalNotification:notification]; //图标的数字清零 ([UIApplication sharedApplication].applicationIconBadgeNumber >= 1) ?([UIApplication sharedApplication].applicationIconBadgeNumber -= 1):0 ; break; } } }
最后
Demo_Git地址相关文章推荐
- xcode6和xcode5区别
- xcode6和xcode5区别
- iOS 隐藏键盘的通用方法
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- iOS - 利用pch文件在程序发布时统一去除打印代码
- 一些很好的ios文章
- IOS 应用悬浮窗
- iOS 各类宏定义的作用
- iOS中直接使用Gif图片
- IOS本地数据存取
- IOS开发之内存管理(2)
- IOS开发学习之内存管理
- ios 调用系统照片库/拍照/录像
- iOS百度地图比例,及自定义popview图片偏移设置
- IOS学习之NSNotificationCenter消息机制
- IOS开发资料汇总
- iOS的NSString常用方法总结
- iOS每个Cell倒计时
- Learning methods - 2