@fiy-fish
2017-03-23T14:12:47.000000Z
字数 4218
阅读 1730
工作日志
今天主要是两个知识点
1.swift 闭包
2.远程推送
{(传入参数名:参数类型->(方法返回参数类型) in 方法内容 return 返回值)}{(传入参数名 in 方法内容 return 返回值)}{(传入参数名 in 方法内容和返回值为一体)}{$0>$1}@noescape 推送原理
远程推送在项目中的应用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// 在iOS8之前注册远程通知的方法,如果项目要支持iOS8以前的版本,必须要写此方法UIRemoteNotificationType types = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert;[[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];// iOS8之后注册远程通知的方法UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];}
- 注册远程通知成功和失败的回调方法
// 注册成功回调方法,其中deviceToken即为APNs返回的token- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {[self sendProviderDeviceToken:deviceToken]; // 将此deviceToken发送给Provider}// 注册失败回调方法,处理失败情况- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {}
- 接收到远程推送
方法1.didReceiveRemoteNotification, 只有程序在前台才会调用,程序在后台不会调用,当用户点击收到的推送信息时会调用。
当收到远程推送时,程序不是处于启动状态,推送信息会传递给方法application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions:,当调用这两个方法时,可以取得推送消息:NSDictionary *pushInfor = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {}
方法2:如下,如果下面的方法和上面的方法同时存在,只会执行下面的方法
//这里采用可以后台操作的推送方法- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{//APP 前台 收到远程推送都会调用//APP 后台 收到远程推送调用,(前提是 你开启了APP后台模式)//当用户点击推送信息进入APP 还会调用 这个方法//一定要实现 闭包UIBackgroundFetchResult 否则程序不会启动//在程序启动前,你有30秒的时间去在后台处理数据,刷新UI/*UIBackgroundFetchResultNewData, 接收到新数据UIBackgroundFetchResultNoData, 没有接受到数据UIBackgroundFetchResultFailed 接受数据失败*///必须执行这个blockcompletionHandler(UIBackgroundFetchResultNewData);//接下来做 收到推送的处理}
{"aps" : {"alert" : { // string or dictionary"title" : "string""body" : "string","title-loc-key" : "string or null""title-loc-args" : "array of strings or null""action-loc-key" : "string or null""loc-key" : "string""loc-args" : "array of strings""launch-image" : "string"},"badge" : number,"sound" : "string""content-available" : number;"category" : "string"},}aps:推送消息必须有的keyalert:推送消息包含此key值,系统就会根据用户的设置展示标准的推送信息badge:在app图标上显示消息数量,缺少此key值,消息数量就不会改变,消除标记时把此key对应的value设置为0sound:设置推送声音的key值,系统默认提示声音对应的value值为defaultcontent-available:此key值设置为1,系统接收到推送消息时就会调用不同的回调方法,iOS7之后配置后台模式category:UIMutableUserNotificationCategory's identifier 可操作通知类型的key值title:简短描述此调推送消息的目的,适用系统iOS8.2之后版本body:推送的内容title-loc-key:功能类似title,附加功能是国际化,适用系统iOS8.2之后版本title-loc-args:配合title-loc-key字段使用,适用系统iOS8.2之后版本action-loc-key:可操作通知类型key值,不详细叙述loc-key:参考title-loc-keyloc-args:参考title-loc-argslaunch-image:点击推送消息或者移动事件滑块时,显示的图片。如果缺少此key值,会加载app默认的启动图片。
{"aps" : {"alert" : "Provider push messag.","badge" : 9,"sound" : "toAlice.aiff"},"Id" : 1314, // 自定义key值"type" : "customType" // 自定义key值}