MopPush是Mob平台推出的推送SDK。MobPush支持iOS和Android两大平台,并拥有Cocoa2d、Unity3D、JavaScript插件支持,支持RestApi轻松接入,集成更方便、简单、快速,提供完整的可视化数据和强大的管理后台。推送是App不可或缺的功能,可以有效提高用户留存率和活跃度,快速高效的为App集成MobPush推送服务,可以应对各种推送场景。本文主要介绍MobPushForiOS。主要功能推送流程MobPush提供了两个通道:一个APNs通道和一个长连接通道。MOBPush的推送流程从上图可以看出:应用在前台时,使用的是MobPush的长连接通道。MobPush接收长连接调用本地通知,达到和APNs一样的效果,大大提高了推送速度和成功率。当应用程序在后台时,它使用APNs通道,MobPush服务器直接向APNs服务器发送消息。上面的APNs推送流程是苹果的APNs流程,请看下图:MobPushAPI下面主要介绍MobPush的API和基本使用。MobPushDidReceiveMessageNotification/**收到消息通知(数据为MPushMessage对象,可能是推送数据、自定义消息数据、APNs的回调、本地通知等)*/externNSString*constMobPushDidReceiveMessageNotification;收到消息通知(数据为MPushMessage对象,可能是推送数据、自定义消息数据、APNs、本地通知等),通过NSNotificationCenter监听这个通知,根据MPushMessage对象中的messageType判断当前收到的通知类型:自定义消息数据、APN、本地通知。将所有的通知回调封装起来,与通知中心一起监听,大大减少了代码量,方便了开发者的接入,降低了开发者的接入门槛。例如:第一步监听通知[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(didReceiveMessage:)name:MobPushDidReceiveMessageNotificationobject:nil];第二步接收通知//接收通知回调(void)didReceiveMessage:(NSNotification*)notification{MPushMessage*message=notification.object;switch(message.messageType){caseMPushMessageTypeCustom:{//自定义消息}break;caseMPushMessageTypeAPNs:{//APNs回调}break;caseMPushMessageTypeLocal:{//本地通知回调}break;default:break;}}第三步dealloc中去掉通知(void)dealloc{[[NSNotificationCenterdefaultCenter]removeObserver:self];}setAPNsForProduction/**设置推送环境@paramisProduction是否为生产环境。如果处于开发状态,则设置为NO;如果它处于生产状态,则应将其更改为YES。默认为YES生产状态*/(void)setAPNsForProduction:(BOOL)isProduction;设置推送环境,设置当前推送环境是否为生产环境,如果是开发状态则设置为NO;如果是生产状态,应该改成YES。默认为YES生产状态。这行代码写在didFinishLaunchingWithOptions中,用来告诉MobPush服务器在哪个环境下推送。例如://设置推送环境ifdefDEBUG[MobPushsetAPNsForProduction:NO];else[MobPushsetAPNsForProduction:YES];endif通常我们在DEBUG模式下设置开发状态,在Release模式下设置生产状态。setupNotification/**设置推送配置@paramconfiguration配置信息*/(void)setupNotification:(MPushNotificationConfiguration*)configuration;设置推送配置,这里主要设置一些推送配置参数,你可以在任何地方设置。例如://MobPush推送设置(获取角标、声音、弹窗提醒权限)MPushNotificationConfiguration*configuration=[[MPushNotificationConfigurationalloc]init];配置类型=MPushAuthorizationOptionsBadge|MPushAuthorizationOptions声音|MPushAuthorizationOptionsAlert;[MobPushconfigurationNotification];ification:以上代码是最基本的配置,设置了推送角标、声音、弹窗提醒的权限。addLocalNotification/**添加本地推送通知@parammessage消息数据*/(void)addLocalNotification:(MPushMessage*)message;添加本地推送通知,可以自定义本地通知的内容、标题、副标题、声音、角标、什么时间触发等。例如:MPushMessage*message=[[MPushMessagealloc]init];message.messageType=MPushMessageTypeLocal;MPushNotification*noti=[[MPushNotificationalloc]init];noti.body=@"Pushcontent";noti.title=@"title";noti.subTitle=@"subtitle";noti.sound=@"unbelievable.caf";noti.badge=999;message.notification=noti;NSDate*currentDate=[NSDatedateWithTimeIntervalSinceNow:0];NSTimeIntervalnowtime=[currentDatetimeIntervalSince1970]*1000;//设置立即触发//message.isInstantMessage=YES;//设置几分钟后发起本地推送NSTimeIntervaltaskDate=nowtime+self.timeValue601000;message.taskDate=taskDate;[MobPushaddLocalNotification:message];addTags/**添加标签@paramtags标签组@paramhandlerresult*/(void)addTags:(NSArray
