远程通知(Remote|远程通知(Remote Notification リモート?ノーティフィケーション使用上の注意)

注册:要区分iOS8分别使用不同的api:

//推送处理 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; }else { //这里还是原来的代码 [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; }

可以看出iOS8的方法把之前的方法拆成了两步:先设置,后注册。
至于具体何时调用要看公司自己服务器的逻辑。
在这之后系统会自动调用两个方法:didRegisterForRemoteNotificationsWithDeviceToken:(注册成功),didFailToRegisterForRemoteNotificationsWithError:(注册失败),官方文档:
Discussion:Call this method to initiate the registration process with Apple Push Notification service. If registration succeeds, the app calls your app delegate object’s application:didRegisterForRemoteNotificationsWithDeviceToken: method and passes it a device token. You should pass this token along to the server you use to generate remote notifications for the device. If registration fails, the app calls its app delegate’s application:didFailToRegisterForRemoteNotificationsWithError: method instead.
大意是说逐层成功之后要发送自己的deviceToken给自家服务器,也就是注册的绑定。这是为什么腻?这里就得提一下苹果的推送服务的原理了:

远程通知(Remote|远程通知(Remote Notification リモート?ノーティフィケーション使用上の注意)
文章图片
屏幕快照 2015-12-19 下午2.48.44
上面就是著名的那张APNs说明图(不是清明上河图!-_-|||)。
那个圆圈Provider就是我们这边的服务器,那个小云彩就是苹果的APNs,细心的读者已经发现了它的全称:Apple Push Notification service(苹果推送通知服务)。流程翻译过来就是你家的服务器把你苹果设备当初注册给它的deviceToken和要推送的信息发送给APNs,然后苹果验证了之后才发给用户的设备(也就是单例的UIApplication),用户的设备再发送给app。为啥发给设备呢?因为苹果的用户比较调皮,没事儿喜欢杀杀后台程序,万一一个手滑把你app杀了还咋收信息?即使用户不调皮,苹果受限于自身设备的运行内存,没事也会杀杀后台程序给正在运行的程序让道,所以呢,就由整个系统帮手机里所有的app监控推送信息。
对了,还要提一下推送的开启条件:首先得有一个99美元的开发者账号,然后如下图制作CSR文件,好像是cert sign request,一看就是制作cert(证书)必备的文件。`
有了CSR就可以进入 苹果开发者中心申请证书。
//TODO:cert,provision profiles 【远程通知(Remote|远程通知(Remote Notification リモート?ノーティフィケーション使用上の注意)】

    推荐阅读