IOS基础之——添加本地通知(UILocalNotification)
来源:互联网 发布:三国志11公孙瓒数据 编辑:程序博客网 时间:2024/05/01 20:28
本地通知(local notifacation)用于向用户提示一条消息——即使应用没有运行,用户也可以收到本地通知。
应用还可以通过后台服务器实现推送消息(push notification)。本篇只讨论本地通知,不讨论推送消息。
实现本地通知非常简单,首先需要创建一个UILocalNotification 对象并设置其显示内容和提醒时间,然后调用UIApplication单例对象的scheduleLocalNotification:方法注册该通知就可以了。
-(IBAction)addReminder:(id)sender{ NSDate *date = self.dataPicker.date; UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.alertBody = @"Hypnotize me!"; notification.fireDate = date;[[UIApplication sharedApplication] scheduleLocalNotification:notification];}
dataPicker是一个时间选择器,选择时间后按下按钮,就会在设置的时间到来时收到本地通知。
为了看到本地通知,必须将关闭应用,按home键使应用到后台。当到达时间后会弹出本地提醒。
实际上,达到了相应的时间,我们并没有收到预期的提醒,因为通知需要得到系统的权限,在控制台上会看到下面一段话:
2014-06-07 11:14:16.663 CCA-TV[735:149070] Attempting to schedule a local notification {fire date = Saturday, June 7, 2014 at 11:14:21 Pacific Daylight Time, time zone = America/Los_Angeles (PDT) offset -25200 (Daylight), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, June 7, 2014 at 11:14:21 Pacific Daylight Time, user info = (null)} with an alert but haven't received permission from the user to display alerts
那么我们就需要在代码中添加以下的内容,才能顺利收到本地通知:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil ]]; }}
0 0
- IOS基础之——添加本地通知(UILocalNotification)
- iOS本地通知UILocalNotification
- IOS 本地通知 UILocalNotification
- ios本地通知UILocalNotification
- iOS UILocalNotification本地通知
- iOS本地通知:UILocalNotification
- iOS本地通知:UILocalNotification
- iOS UILocalNotification本地通知
- iOS本地通知:UILocalNotification
- iOS本地通知:UILocalNotification
- iOS UILocalNotification本地通知
- iOS本地通知 UILocalNotification
- iOS 本地通知:UILocalNotification
- iOS开发之UILocalNotification本地通知
- iOS开发之本地通知UILocalNotification
- iOS推送 之 本地通知UILocalNotification
- 本地通知——UILocalNotification
- 【iOS开发】UILocalNotification 本地通知的实现 —— 伊禾媛
- VS2010/2008常用快捷键
- codeforces 394E Lightbulb for Minister 简单几何
- 招聘初级程序员必须考虑的6个因素
- Spring 在XML中声明切面/AOP
- 【C# for OJ/ACM做题】适用于各种OJ的2种C#输入输出辅助类(IOHelper)
- IOS基础之——添加本地通知(UILocalNotification)
- 从web service得到的SoapObject解析
- 6
- 《算法心得》高效用法记录
- Unix Network 2
- 数学分析之曲面积分
- linux程序设计——同时执行的线程(第十二章)
- The Joel Test:软件开发成功12法则
- leetCode 9.Palindrome Number (回文数字) 解题思路和方法