当前位置: 首页 > Web前端 > HTML

MobPush框架及API简介

时间:2023-03-28 01:29:54 HTML

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)tagsresult:(void(^)(NSErrorerror))handler;addtags,可以添加Multiple,notcovered,tags:对用户进行分组的作用,比如一个新闻app,用户喜欢体育和科技。为用户添加体育和科技标签,当有体育和科技文章时,可以推送给用户。例如:[MobPushaddTags:@[@"Sports",@"Technology"]result:^(NSError*error){}];getTagsWithResult/**获取所有标签@paramhandlerresult*/(void)getTagsWithResult:(void(^)(NSArraytags,NSErrorerror))handler;获取所有添加标签的列表例如:[MobPushgetTagsWithResult:^(NSArraytags,NSErrorerror){NSLog(@"%@",tags);}];deleteTags/**删除标签@paramtags要删除的标签@paramhandlerresult*/(void)deleteTags:(NSArray)tagsresult:(void(^)(NSErrorerror))handler;delete标签,删除指定标签例如:[MobPushdeleteTags:@"Sports"result:^(NSError*error){}];cleanAllTags/**清除所有标签@paramhandlerresult*/(void)cleanAllTags:(void(^)(NSError*error))处理程序;清除所有标签例如:[MobPushcleanAllTags:^(NSError*error){}];setAlias/**setalias@paramaliasalias@paramhandlerresult*/(void)setAlias:(NSString)aliasresult:(void(^)(NSError错误))处理程序;设置别名,别名是唯一的,会被覆盖。一般用作指定用户,userId可以设置为别名。例如:[MobPushsetAlias:@"userId"result:^(NSError*error){}];getAliasWithResult/**getalias@paramhandlerresult*/(void)getAliasWithResult:(void(^)(NSStringalias,NSError错误))处理程序;获取别名,获取设置的别名。例如:[MobPushgetAliasWithResult:^(NSStringalias,NSErrorerror){NSLog(@"%@",alias);}];deleteAlias/**删除别名@paramhandlerresult*/(void)deleteAlias:(void(^)(NSError*error))处理程序;删除别名,别名是唯一的,全部不用指定,直接删除即可。例如:[MobPushdeleteAlias:^(NSError*error){}];getRegistrationID/**获取注册id(可以绑定userid向指定用户推送消息)@paramhandlerresult*/(void)getRegistrationID:(void(^)(NSStringregistrationID,NSErrorerror))handler;获取注册id,可与用户id绑定,向指定用户推送消息。例如:[MobPushgetRegistrationID:^(NSStringregistrationID,NSErrorerror){NSLog(@"%@",registrationID);}];setBadge/**设置badge值到本地Mob服务器,首先调用setApplicationIconBadgeNumber函数显示徽章Badge,然后将徽章值同步到Mob服务器,@parambadgenewbadgevalue(会覆盖服务器上保存的值)*/(void)setBadge:(NSInteger)badge;给Mob服务器设置徽章值,同步徽章为例:[MobPushsetBadge:99];clearBadge/**清除徽章,但不清除通知栏消息*/(void)clearBadge;清除徽章,但不清除通知栏消息。例如:[MobPushclearBadge];