IOS本地推送 学习手记
2015-07-21 09:42
453 查看
iOS上有两种消息通知,一种是本地消息(Local Notification),一种是远程消息(Push Notification,也叫Remote Notification),设计这两种通知的目的都是为了提醒用户,现在有些什么新鲜的事情发生了,吸引用户重新打开应用。本地推送也可以通过服务器控制,比如说如果有新消息了,推送消息,但是,前提是程序必须是打开的,而远程推送,是通过苹果APNS服务器,推送给手机,手机在推送给具体的哪个程序,一般远程推送用到的比较多。
首先,先在appdelegate中注册:
然后,在具体的viewcontroller中实现推送:
如果需要接受消息的话 重写方法如下:
在appdelegate中会接收到推送信息
过程中可能会出现如下状况:
Attempting to schedule a local notification……with a sound but haven't received permission from the user to play sounds
Attempting to schedule a local notification……with an alert but haven't received permission from the user to display alerts
可能是因为你没有注册,或者设置中没有开启推送功能,
首先,先在appdelegate中注册:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];//注册本地推送 // Override point for customization after application launch. return YES; }
然后,在具体的viewcontroller中实现推送:
- (IBAction)localPushNow:(id)sender { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //本地推送 UILocalNotification*notification = [[UILocalNotification alloc]init]; NSDate * pushDate = [NSDate dateWithTimeIntervalSinceNow:10]; if (notification != nil) { notification.fireDate = pushDate; notification.timeZone = [NSTimeZone defaultTimeZone]; notification.repeatInterval = kCFCalendarUnitDay; notification.soundName = UILocalNotificationDefaultSoundName; notification.alertBody = @"hello,world"; notification.applicationIconBadgeNumber = 0; NSDictionary*info = [NSDictionary dictionaryWithObject:@"test" forKey:@"name"]; notification.userInfo = info; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; } }); }
如果需要接受消息的话 重写方法如下:
在appdelegate中会接收到推送信息
//接收本地推送 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ NSLog(@"%@",notification.alertBody); UILabel*label = [[UILabel alloc]init]; label.frame = CGRectMake(0, 0, 160, 20); label.layer.cornerRadius = 10; label.backgroundColor = [UIColor blackColor]; label.text = notification.alertBody; label.textColor = [UIColor whiteColor]; label.font = [UIFont systemFontOfSize:12]; label.textAlignment = NSTextAlignmentCenter; [self.window addSubview:label]; }
过程中可能会出现如下状况:
Attempting to schedule a local notification……with a sound but haven't received permission from the user to play sounds
Attempting to schedule a local notification……with an alert but haven't received permission from the user to display alerts
可能是因为你没有注册,或者设置中没有开启推送功能,
相关文章推荐
- iOS 与webService
- (9/18)重学Standford_iOS7开发_动画、自动布局_课程笔记
- Nagios监控--案例一
- 开发一个iOS应用没有那么容易
- iOS Runtime 和消息机制
- IOS开发之开篇--CocoaPods安装
- 提高iOS开发效率的方法和工具
- Nagios监控平台搭建--安装篇
- iOS-CoreLocation:无论你在哪里,我都要找到你!
- iOS中的几种数据持久化方案
- ios开发之自动售货机的设计与实现
- ios开发之电子优惠券的设计与实现
- IOS开发之电子菜单的设计与实现(继承、封装、多态)
- iOS 实现拍照功能
- IOS 开发,调用打电话,发短信,打开网址
- IOS-TextField控件详解 学习手记
- iOS-CoreImage简单使用
- 《IOS编程》深入学习:键值编码 学习手记
- 《IOS编程》控制逻辑课后练习 学习手记
- 如何设置bios让电脑从U盘启动