通知中心(NotificationCenter)和通知(UILocalNotification)是雷锋和雷峰塔的关系哦
通知分为:本地通知和远程通知
本地通知
就是你好久不用一个程序,它给你拉一条横幅写“部落里没有你臣妾好寂寞”;或者更典型的一个日历软件,到时间提醒你今天是乔帮主的忌日。(UILocalNotification)
即:由本地应用程序发起的通知,一般是在应用程序处于后台或退出后,让iOS系统在指定时间通知用户的一种方式。
远程通知
就是你收到一条微信,在锁屏上显示的或在顶部导航栏显示的。(APNS)
本地通知使用步骤:
创建本地通知对象(UILocalNotification)
let notification = UILocalNotification()
设置处理通知的时间(fireDate属性)
notification.fireDate = NSDate(timeIntervalSinceNow: 5)
配置通知的内容:通知主体,通知声音,图标数字
notification.repeatInterval = NSCalendarUnit.CalendarUnitMinute//CalendarUnit是指一分钟/小时/周之后再发
notification.alertBody = "通知主体内容!"
notification.applicationIconBadgeNumber = 1;//强迫症最受不了的小红点,图标数字1
notification.soundName = "success.caf"
调用通知:
按计划调度: scheduleLocalNotification(一般情况都是按计划调用)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
立即调用: presentLocalNotification(一般不用)
要注意从iOS8开始,应用想发通知,必须经过用户允许,否则通知无法发送。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//判断当前设备是否是IOS8
if(UIDevice.currentDevice().systemVersion > 7.0){
//向用户申请发通知的权限(参数可以允许:通知内容,通知声音,图标数字)
let settings = UIUserNotificationSettings(forTypes:UIUserNotificationType.Alert | UIUserNotificationType.Sound | UIUserNotificationType.Badge, categories:nil)
application.registerUserNotificationSettings(settings)
}
}
用户点击通知之后会进入我们的App,所以还需要
1.取消红点
func applicationWillEnterForeground(application: UIApplication) {
println("应用程序从后台进入到了前台!")
//取消应用程序消息图标
application.applicationIconBadgeNumber = 0
}
//取消已有的通知
UIApplication.sharedApplication().cancelAllLocalNotifications()
1.控制台NSNotificationCenter
[[NSNotificationCenterdefaultCenter]postNotificationName:@"12306" object:self userInfo:@{@"买票": @"票已卖完",@"我要投诉":@"请拨打12306投诉电话"}];//向控制台发送消息
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(woyaotousu:) name:@"12306" object:nil];//成为控制台观察者 一旦接收到12306的消息,就执行方法woyaotousu:
-(void)woyaotousu:(NSNotification*)notification{//参数为一个控制台
NSDictionary*message=notification.userInfo;//得到控制台携带的消息
NSLog(@"%@ %@",message[@"买票"],message[@"我要投诉"]);
}
程序进入后台时,就会发出UIApplicationWillResignActiveNotification通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aa:) name:UIApplicationWillResignActiveNotification object:nil];
// 发出通知
[MTNotificationCenterpostNotificationName:MTCategoryDidChangeNotification object:niluserInfo:@{MTSelectCategory : category, MTSelectSubcategoryName : category.subcategories[subrow]}];
2.键盘通知
UIKeyboard
NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
[center addObserver:selfselector:@selector(keyboardOpen:) name:UIKeyboardWillShowNotificationobject:nil];//注册成为观察者,接收来自键盘打开的通知
[center addObserver:selfselector:@selector(keyboardClose:) name:UIKeyboardWillHideNotificationobject:nil];//注册成为观察者,接收来自键盘关闭的通知
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];//移除一个通知
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];//移除一个通知
NSValue*value=notification.userInfo[UIKeyboardFrameEndUserInfoKey];//读取消息,得到字典,在获得字典里面的信息
CGRect rect=[value CGRectValue];//获得键盘的frame
```
3.图标通知
```
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 8.0) { // iOS8+ IconBadge需授权
//创建一个Setting,申请发送通知
UIUserNotificationSettings *setting=[UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeBadge categories:nil];
//注册一个Setting
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
}
//在桌面图标上,显示提示10个消息
[UIApplicationsharedApplication].applicationIconBadgeNumber=10;
```
4.给用户的通知
4.1 OC
```
//注册应用接收通知
if ([[UIDevicecurrentDevice].systemVersiondoubleValue] > 8.0){
UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
//创建本地通知
UILocalNotification *localNoti=[[UILocalNotification alloc]init];
// 设置内容
localNoti.alertBody = [NSStringstringWithFormat:@"%@\n%@",message.fromStr,message.body];
// 设置通知执行时间
localNoti.fireDate = [NSDate date];
//声音
localNoti.soundName = @"default";
//红色的溶质角标
localNoti.applicationIconBadgeNumber = 1;
//执行
[[UIApplication sharedApplication] scheduleLocalNotification:localNoti];
```
4.2 swift
```
//取消所有通知
UIApplication.sharedApplication().cancelAllLocalNotifications()
//创建通知设置
let setting = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Sound | UIUserNotificationType.Badge, categories: nil)
//注册通知设置
UIApplication.sharedApplication().registerUserNotificationSettings(setting)
//创建通知对象
let notification = UILocalNotification()
//设置fireDate(推送间隔时间)
notification.fireDate = NSDate(timeIntervalSinceNow: 10)
//按分钟重复
notification.repeatInterval = NSCalendarUnit.CalendarUnitMinute
//通知内容
notification.alertBody = "时间到了"
//红色的溶质角标
notification.applicationIconBadgeNumber = 1
//提醒声音
notification.soundName = “success.caf"
localNoti.soundName = @"default";
//调用
UIApplication.sharedApplication().scheduleLocalNotification(notification)
//当点了通知,回到应用程序的时候回调
funcapplicationWillEnterForeground(application: UIApplication) {
application.applicationIconBadgeNumber=0}
//当程序在后台运行时,如果收到了一个通知,用户点击了这个通知,从后台进入前台的时候回调
-(void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification *)notification{}
//当程序在后台运行时,如果收到了一个远程通知,用户点击了这个通知,从后台进入前台的时候回调
-(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo{}
```