iOS开发之旅--本地通知的发送与取消
2015-11-30 18:20
519 查看
现在有时间,就把之前学习的使用过的本地通知的基本过程给大家分享一下。
解释一下本地通知是程序员在应用中给用户手动推送的消息。(与远程推送的具体区别,后期会更新。)
本地通知的具体实现如下:
首先, 我创建了一个本地通知的Demo:AmerLocationNotification:
在Main.storyboard中拖一个switch并脱线出Change事件:
接下来,我们要将具体实现写在该事件中。
注意:这种状态的通知,是只有你的应用进入后台才会有提示。
else中得代码是要取消通知,一定要实现,否则你会被不停的通知烦死。因为如果你不取消通知,即使你将应用卸载重装,通知依旧存在,仍然会给你弹。
下一步,我们要实现让通知在应用在前台显示时也可以提醒,代码位于appDelegate中.
最后最重要的一步,千万不要漏掉,否则你什么通知也收不到。
文档原话:
解释一下本地通知是程序员在应用中给用户手动推送的消息。(与远程推送的具体区别,后期会更新。)
本地通知的具体实现如下:
首先, 我创建了一个本地通知的Demo:AmerLocationNotification:
在Main.storyboard中拖一个switch并脱线出Change事件:
接下来,我们要将具体实现写在该事件中。
- (IBAction)changed:(UISwitch *)sender { UIApplication *app = [UIApplication sharedApplication]; if (sender.on) { // 初始化本地通知对象 UILocalNotification *notification = [[UILocalNotification alloc] init]; if (notification) { // 使用本地时区 notification.timeZone = [NSTimeZone defaultTimeZone]; //设置出发时间 notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0]; // 设置重复间隔 notification.repeatInterval = kCFCalendarUnitMinute; // 设置锁屏时,提醒文字title notification.alertAction = @"查水表"; // 设置提醒的文字内容 notification.alertBody = @"你妈妈叫你回家吃饭"; // 通知提示音 使用默认的 notification.soundName= UILocalNotificationDefaultSoundName; // 设置应用程序右上角的提醒个数 notification.applicationIconBadgeNumber = 1; // 设定通知的userInfo,用来标识该通知 NSDictionary *infor = @{@"amer.org" : @"key"}; notification.userInfo = infor; /*这里也可以设置alertLaunchImage*/ // 将通知添加到系统中 [app scheduleLocalNotification:notification]; } } else { //获取所有除与调度中的数组 NSArray *locationArr = app.scheduledLocalNotifications; if (locationArr) { for (UILocalNotification *ln in locationArr) { NSDictionary *dict = ln.userInfo; if (dict) { NSString *obj = [dict objectForKey:@"amer.org"]; if ([obj isEqualToString:@"key"]) { //取消调度通知 [app cancelLocalNotification:ln]; } } } } } }
注意:这种状态的通知,是只有你的应用进入后台才会有提示。
else中得代码是要取消通知,一定要实现,否则你会被不停的通知烦死。因为如果你不取消通知,即使你将应用卸载重装,通知依旧存在,仍然会给你弹。
下一步,我们要实现让通知在应用在前台显示时也可以提醒,代码位于appDelegate中.
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { NSLog(@"接收到通知"); [[[UIAlertView alloc] initWithTitle:notification.alertAction message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show]; application.applicationIconBadgeNumber = 0; } - (void)applicationWillEnterForeground:(UIApplication *)application { //如果app在前台运行 application.applicationIconBadgeNumber = 0; }
最后最重要的一步,千万不要漏掉,否则你什么通知也收不到。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIApplication *app = [UIApplication sharedApplication]; //是否响应registerUserNotificationSettings(ios8以后) if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]; [app registerUserNotificationSettings:settings]; } else //如果不适配iOS8之前的设备,这个else可以省略 { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [app registerForRemoteNotificationTypes:myTypes]; } return YES; } -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ [application registerForRemoteNotifications]; // <================== 新添加 NSLog(@"接收到通知"); [[[UIAlertView alloc] initWithTitle:notification.alertAction message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show]; application.applicationIconBadgeNumber = 0; }
文档原话:
// In iOS 8.0 and later, your application must register for user notifications using -[UIApplication registerUserNotificationSettings:] before being able to schedule and present UILocalNotifications
相关文章推荐
- iOS不同版本适配问题
- iOS Assets Catalogs
- iOS滤镜效果的实现:CoreImage
- IOS工程自动打包并发布脚本实现
- iOS开发之NSXMLParserDelegate解析失败
- iOS: 如何判断代码运行在DEBUG还是RELEASE模式下?
- iOS h264 硬解码
- iOS:XMPP即时聊天知识
- iOS实现Cell自适应高度
- iOS应用间调用
- 如何设计一个 iOS 控件?(iOS 控件完全解析)
- 画虚线
- IOS SDK详解之NSDictionary
- iOS开发入门到精通四阶段
- 关于苹果开发者账号的大部分事
- 【iOS9】真机测试添加证书
- ios has ambiguous scrollable content height
- iOS开发-进阶:延时执行的几种方式的比较和汇总
- iOS多线程GCD
- iOS 开发,工程中混合使用 ARC 和非ARC