iOS开发之旅--本地通知的发送与取消
来源:互联网 发布:网络市场调研案例 编辑:程序博客网 时间:2024/05/29 08:29
现在有时间,就把之前学习的使用过的本地通知的基本过程给大家分享一下。
解释一下本地通知是程序员在应用中给用户手动推送的消息。(与远程推送的具体区别,后期会更新。)
本地通知的具体实现如下:
首先, 我创建了一个本地通知的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
0 0
- iOS开发之旅--本地通知的发送与取消
- ios开发之本地通知
- iOS开发--本地通知与远程通知
- iOS开发-本地通知与远程通知
- iOS开发--本地通知与远程通知
- iOS本地推送与取消本地通知—UILocalNotification的使用
- iOS发送本地通知
- iOS开发之UILocalNotification本地通知
- iOS开发之本地通知UILocalNotification
- 取消指定的本地通知
- ios-王云鹤 发送本地通知
- iOS开发本地通知(每天定点发送消息推送)
- iOS开发--本地通知
- iOS注册本地通知、取消本地通知 - 家柱
- iOS 开发 本地通知的使用
- 程序发送和取消状态栏的通知
- [IOS开发] 本地通知:UILocalNotification
- iOS开发推送本地通知
- android输入判定
- 如何在程序中预留一段固定的虚拟地址空间
- 【XVI Japan】Marching Course(图论,最优解)
- android测试之四:Android设备上的Benckmark测试概述
- PostgreSQL EXPLAIN ANALYSE 分析SQL
- iOS开发之旅--本地通知的发送与取消
- spring配置各层所在的位置
- IOS_Swift_enum枚举方法
- 并行启动应用程序从而加速 Linux 的引导
- 3.8 练习
- 大型Servlet编程模型
- 镜头光晕是如何形成的?
- 指针函数与函数指针
- angularJS中把html格式的字符串转成html格式显示,ng-bind-html与trustAsHtml的过滤器