1 关于推送(简介)服务器
推送的目的是为了让不在前台运行的app使用户可以知道咱们有信息给他们。好比一条信息,即将到来的约会等。app
推送给用户的类型有三种ide
Users can get notified in the following ways:fetch
An onscreen alert or bannerspa
A badge on the app’s icon代理
A sound that accompanies an alert, banner, or badgecode
推送分为两种,本地通知和远程推送,二者从用户角度来讲是相同的,但从技术实现角度来说,二者有很大的区别。对象
(1)本地通知:经过安装在设备上的app设定和负责推送。token
(2)远程推送:经过自身的服务器,发送通知到Apple Push Notification Center(APNs),而后在经过APNs发送到用户的设备上。开发
2 本地通知和远程推送的实现(基本)
2.1本地通知
(1)在iOS8以前的版本中,本地通知是不须要注册的,可是在iOS8以后的版本,本地通知和远程通知统一使用UIApplication类中的
registerUserNotificationSettings:方法注册
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { //配置通知类型 UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound| UIUserNotificationTypeAlert; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; //调用这个方法后,进入app时系统会弹出对话框来询问用户是否接收通知 //当调用这个方法后,系统会回调AppDelegate中的application:didRegisterUserNotificationSettings: [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; } return YES; }
本地通知使用步骤:
(1)使用上面的方法注册通知的类型
(2)初始化UILocalNotification对象
(3)设置系统发送该通知的日期和时间,也就是fireDate属性。若是设置了timeZone属性为当地时区,那么当设备所在的时区改变时(或者从新设置了),系统会自动调整fireData。通常本地通知是基于时间来发送的,iOS8新出了一个region属性,下面再说。
(4)设置提醒,图标(右上角小红点),声音来提醒用户。
alert
再说,iOS8有新特性
applicationIconBadgeNumber 属性用于显示图标右上角小红点中的数字
soundName 发送通知时的声音,与上面两个结合使用
(5)可选择的(到下面再说)
(6)iOS8以后,能够自定义通知的操做。
(7)使用scheduleLocalNotification:方法设定本地通知,该方法会根据fireDate中设置的时间来发送通知,也可使用
presentLocalNotificationNow:当即发送通知。
使用cancelLocalNotification:或者cancelAllLocalNotifications:来用代码隐藏当前显示的通知。
2.2远程推送(使用远程通知须要申请推送证书,证书稍后再说,能够选择第三方极光推送或者百度云推送,下降开发成本)
设备app向APNs请求device token,获取后会返回给AppDelegate的代理方法,app须要接收device token,并将其发送给
推送服务器(provider);
使用步骤:(iOS8以后)
(1)使用2.1 (1)中的方法注册通知类型
(2)调用registerForRemoteNotifications向APNs注册接收推送
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { //配置通知类型 UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound| UIUserNotificationTypeAlert; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; //调用这个方法后,进入app时系统会弹出对话框来询问用户是否接收通知 //当调用这个方法后,系统会回调AppDelegate中的application:didRegisterUserNotificationSettings: [application registerUserNotificationSettings:settings]; //注册远程通知 [application registerForRemoteNotifications]; } else { //在iOS8以前,使用registerForRemoteNotificationTypes:方法来代替前两个步骤 //iOS8以前的方法 UIRemoteNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound| UIUserNotificationTypeAlert; [application registerForRemoteNotificationTypes:types]; } return YES; }
(3)成功注册后,APNsf返回device token,系统回调AppDelegate中的代理方法
(4)将device token提交给推送的服务器(与APNs相连的);
//向APNs注册,成功后返回deviceToken -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ //处理接收到到了deviceToken,须要上传给服务器(provider); ... NSLog(@"%ld",[deviceToken length]); } //向APNs注册,失败返回的结果 -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ NSLog(@"%@",error); }
注意点:
(1)设备离线状况下,系统不会调用两个代理方法。
(2)若是设备经过wifi没法链接到APNs的5223端口,也会发生这种状况。(更换wifi或者使用蜂窝来解决)
服务器推送包括1.device token 2.payload
3通知的处理
用户收到通知后,会对其进行处理,处理通知分为如下几种状况。
1 发送通知的时候app没有在前台运行(此状况下,系统根据注册的通知类型来提醒用户)
(1)在iOS8以后的通知中,用户点击了自定义的Action button
系统调用application:handleActionWithIdentifier:forRemoteNotification:completionHandler:或者
application:handleActionWithIdentifier:forLocalNotification:completionHandler:。在两个方法中,能够
(2)用户点击了默认按钮或者点击了app图标
若是是Local Notification,系统会将Notification传递给application:didFinishLaunchingWithOptions:方法
若是是Remote Notification,调用会讲payload传递给application:didFinishLaunchingWithOptions:同时也会调用
application: didReceiveRemoteNotification:fetchCompletionHandler:方法
2通知发送的时候app运行在前台
对于以上全部的状况,实现
application:didReceiveRemoteNotification:fetchCompletionHandler: application:didReceiveLocalNotification:
To handle notification actions.
application:handleActionWithIdentifier:forLocalNotification:completionHandler: application:handleActionWithIdentifier:forRemoteNotification:completionHandler: methods.
四个方法便可
4通知(iOS8以后新增的内容)
(1)自定义Notification Action,iOS8以前,默认只有一个动做。如今咱们能够为通知增长自定义的内容。
为了在app中使用Notification actions,你必须定义这个actions,将他们按照categories分类,而后用UIApplication注册他们
为了定义一个notification Action,
一,你必须建立并实例化一个notification class实例,好比UIMutableUserNotfiicationAction。
二。你定义一个identifier,当它处理Action的时候会传给你的app,同时也定义一个本土化的Action button。
三。设置Action的激活模式,foreground(须要打扰用户)或者background
四。声明Action是否具备破坏性,他的按钮显示红色。是否须要用户输入密码
5证书申请
6APNs
5,6有空再写。