写点什么

MobPush iOS 端 SDK API

  • 2024-07-22
    上海
  • 本文字数:5743 字

    阅读完需:约 19 分钟

概述 MobPush 注册推送,获取推送 id 等方法均可在 SDK 的"MobPush.h"中进行查看,也可以下载 MobPush 的 Demo 进行参考。


推送环境设置(setAPNsForProduction)/**@param isProduction 是否生产环境。 如果为开发状态,设置为 NO; 如果为生产状态,应改为 YES。 Default 为 YES 生产状态*/


  • (void)setAPNsForProduction:(BOOL)isProduction;示例代码


// 设置推送环境 #ifdef DEBUG


[MobPush setAPNsForProduction:NO];
复制代码


#else


[MobPush setAPNsForProduction:YES];
复制代码


#endif 注册推送配置(setupNotification)/**@param configuration 配置信息*/


  • (void)setupNotification:(MPushNotificationConfiguration *)configuration;示例代码


//MobPush 推送设置(获得角标、声音、弹框提醒权限),应用要收到推送(角标、声音、弹框提醒)需要先申请权限,这个方法就是设置推送配置、申请权限的方法。用法可参考以下的例子。


MPushNotificationConfiguration *configuration = [[MPushNotificationConfiguration alloc] init];


configuration.types = MPushAuthorizationOptionsBadge | MPushAuthorizationOptionsSound | MPushAuthorizationOptionsAlert;


[MobPush setupNotification:configuration];通知回调接口(MobPushDidReceiveMessageNotification)/**收到消息通知(数据是 MPushMessage 对象,可能是推送数据、自定义消息数据,APNs、本地通知等的回调)*/extern NSString *const MobPushDidReceiveMessageNotification;


说明:应用收到消息,MobPush 会发起一个通知,开发者只需要建立一个通知收听 MobPushDidReceiveMessageNotification 并作相应处理即可。收到的数据是一个 MPushMessage 对象,可能是推送数据,也可能是自定义消息数据。如果是推送数据,开发者可以通过 MobPush.h 中的 addLocalNotification:方法,让消息以本地通知形式显示(iOS 10 之前的系统应用内是不会显示通知的)。


示例代码


// 注册通知回调[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMessage:) name:MobPushDidReceiveMessageNotification object:nil];


//查看通知参数可以打印 notification


  • (void)didReceiveMessage:(NSNotification *)notification{}获取推送 RegistrationID (getRegistrationID)获取推送 RegistrationID 接口,RegistrationID 可与用户 id 绑定,实现向指定用户推送消息,此接口必须在推送设置接口之后调用。


/**获取注册 id(可与用户 id 绑定,实现向指定用户推送消息)


@param handler 结果*/


  • (void)getRegistrationID:(void(^)(NSString *registrationID, NSError *error))handler;示例代码


[MobPush getRegistrationID:^(NSString *registrationID, NSError *error) {


NSLog(@"registrationID = %@--error = %@", registrationID, error);


}];推送标签 API(addTags)MobPush 支持根据标签进行推送,所以也提供了对标签的相应操作。


/**获取所有标签 @param handler 结果*/


  • (void)getTagsWithResult:(void (^) (NSArray tags, NSError error))handler;/


/**添加标签 @param tags 标签组 @param handler 结果*/


  • (void)addTags:(NSArray<NSString *> *)tags result:(void (^) (NSError *error))handler;


/**删除标签 @param tags 需要删除的标签 @param handler 结果*/


  • (void)deleteTags:(NSArray<NSString *> *)tags result:(void (^) (NSError *error))handler;


/**清空所有标签 @param handler 结果*/


  • (void)cleanAllTags:(void (^) (NSError *error))handler;示例代码


[MobPush getTagsWithResult:^(NSArray *tags, NSError *error) {};


[MobPush addTags:[self tags] result:^(NSError *error) {};


[MobPush deleteTags:[self tags] result:^(NSError *error) {};


[MobPush cleanAllTags:^(NSError *error) {};推送别名 API(setAlias)MobPush 同样支持根据别名推送,所以也提供了对别名的相应操作。


/**获取别名 @param handler 结果*/


  • (void)getAliasWithResult:(void (^) (NSString *alias, NSError *error))handler;


/**设置别名 @param alias 别名 @param handler 结果*/


  • (void)setAlias:(NSString *)alias result:(void (^) (NSError *error))handler;


/**删除别名 @param handler 结果*/


  • (void)deleteAlias:(void (^) (NSError *error))handler;示例代码


[MobPush getAliasWithResult:^(NSString *alias, NSError *error) {


};


[MobPush deleteAlias:^(NSError *error) {


};


[MobPush setAlias:@"alias" result:^(NSError error) {}];添加本地推送接口(addLocalNotification)/*添加本地推送通知 @param request 消息请求(消息标识、消息具体信息、触发方式)@param handler 结果,iOS10 以上成功 result 为 UNNotificationRequest 对象、iOS10 以下成功 result 为 UILocalNotification 对象,失败 result 为 nil*/


  • (void)addLocalNotification:(MPushNotificationRequest *)request result:(void (^) (id result, NSError *error))handler;示例代码


#import <MobPush/MobPush.h>[MobPush addLocalNotification:request result:^(id result, NSError error) {};设置角标(setBadge)/*设置角标值到 Mob 服务器本地先调用 setApplicationIconBadgeNumber 函数来显示角标,再将该角标值同步到 Mob 服务器,@param badge 新的角标值(会覆盖服务器上保存的值)*/


  • (void)setBadge:(NSInteger)badge;


/**清除角标,但不清空通知栏消息*/


  • (void)clearBadge;示例代码


[MobPush setBadge:8];


[MobPush clearBadge];打开和关闭远程推送(stopPush)/**关闭远程推送(应用内推送和本地通知不受影响,只关闭远程推送)*/


  • (void)stopPush;


/**打开远程推送*/


  • (void)restartPush;示例代码


[MobPush stopPush];


[MobPush restartPush];应用处于前台时设置推送消息的提示类型(setAPNsShowForegroundType)/**设置应用在前台有 Badge、Sound、Alert 三种类型,默认 3 个选项都有,iOS 10 以后设置有效。如果不想前台有 Badge、Sound、Alert,设置 MPushAuthorizationOptionsNone@param type 类型*/


  • (void)setAPNsShowForegroundType:(MPushAuthorizationOptions)type;示例代码


//设置后,应用在前台时不展示通知横幅、角标、声音。(iOS 10 以后有效,iOS 10 以前本来就不展示)[MobPush setAPNsShowForegroundType:MPushAuthorizationOptionsNone];指定删除收到的本地推送(removeNotificationWithIdentifiers)/**删除指定的推送通知(可以删除未发送或者已经发送的本地通知)@param identifiers 推送请求标识数组,为 nil,删除所有通知*/


  • (void)removeNotificationWithIdentifiers:(NSArray <NSString *> *)identifiers;示例代码


[MobPush removeNotificationWithIdentifiers:nil];推送打开指定应用内指定页面(initWithMobPushScene)后台配置如果开发者想要对通知消息进行点击跳转到 app 内指定页面的操作,可以在开发者管理后台打开配置开关和参数设置。


Scheme 地址:为开发者自定义的控制器路径。


传递参数:为跳转控制器的初始化参数。


代码配置开发者需要在自己的应用内对所跳转的控制器进行相关代码设置。如下:(可参照 demo 中 PushViewController.m) 参考链接(可参考示例代码也可以参考链接去设置): https://www.jianshu.com/p/9abb125b5456


/**设置控制器路径 @return 控制器路径*/


  • (NSString *)MobPushPath;


/**初始化场景参数


@param params 场景参数 @return 控制器对象*/


  • (instancetype)initWithMobPushScene:(NSDictionary*)params;示例代码


#import <MobPush/UIViewController+MobPush.h>


// 还原标识 ios 可以自定义在对应 vc 中实现如下还原代码


  • (NSString *)MobPushPath{return @"mlink://com.mob.mobpush.link";}


//点击推送场景还原页面参数


  • (instancetype)initWithMobPushScene:(NSDictionary *)params{if (self = [super init]){

  • }return self;}富媒体推送使用(MobPushServiceExtension)添加 MobPushServiceExtension 依赖库


设置 Notification Service 最低运行版本为 10.0:


开启富媒体地址 Http 访问支持


使用 MobPushServiceExtension 进行富媒体推送在 NotificationService.m 文件中,导入 MobPushServiceExtension 的头文件:


#import <MobPushServiceExtension/MobPushServiceExtension.h>进入 MobPush 开发者后台通过 url(带有后缀格式的文件地址)或者文件的方式发送富媒体通知。(必须勾选 mutable-content 选项)调用 handelNotificationServiceRequestUrl 方法。接收到 APNs 通知后,SDK 判断是否有富媒体资源 request.content.userInfo[@“attachment”],如果富媒体资源存在则 SDK 下载资源,下载完成后以 Block 方式回调返回 attachments 资源数组对象和 error 错误信息。


示例代码


  • (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {


self.contentHandler = contentHandler;


self.bestAttemptContent = [request.content mutableCopy];


#pragma mark ----将 APNs 信息交由 MobPush 处理----


NSString * attachUrl = request.content.userInfo[@“attachment”];


[MobPushServiceExtension handelNotificationServiceRequestUrl:attachUrl withAttachmentsComplete:^(NSArray *attachments, NSError *error) {


if (attachments.count > 0) {


self.bestAttemptContent.attachments = attachments; self.contentHandler(self.bestAttemptContent);


    }else   {
复制代码


self.contentHandler(self.bestAttemptContent);


    }
}];
复制代码


}多媒体大小限制


自定义推送声音将声音文件拖入到项目中,在 MobPush 后台或者接口传入对应声音文件名称即可


iOS 送达统计开发者可使用 MobPushServiceExtension SDK 上报 APNs 信息的到达状态。


集成方法:


1.将 MobPushServiceExtension SDK 引入到您创建好的 Service Extension 工程中


2.在方法 didReceiveNotificationRequest:withContentHandler:方法中调用 deliverNotificationRequest:MobAppSecret:with:方法,以上报接收到的 APNs 消息,在该方法的 block 回调中进行 APNs 消息的显示


  • (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler{self.contentHandler = contentHandler;self.bestAttemptContent = [request.content mutableCopy];

  • [MobPushServiceExtension deliverNotificationRequest:request MobAppSecret:@"appSecret" with:^{weakSelf.contentHandler(weakSelf.bestAttemptContent);}];}iOS 自定义参数集成方法:


1.新增或更新自定义参数绑定含义:


  • @abstract 更新目标参数绑定的值

  • @param params 包含目标参数名及其值的键值对字典对象

  • @param handler 结果回调*/


  • (void)addCustomParamsWith:(NSDictionary *)paramshandler:(void(^)(NSArray *successKeys, NSArray *failedKeys, NSError *error))handler;(1)params: 为包含多个自定义参数名及其对应含义的字典对象(2)handler: 为操作结果回调,successKeys 成功参数名,failedKeys 为失败参数名,error 为错误信息具体调用方式如下所示:


[MobPush addCustomParamsWith:dict handler:^(NSArray *successKeys, NSArray *failedKeys, NSError *error) {__strong typeof(weakSelf) strongSelf = weakSelf;NSString *successKeyStr = @"";NSString *failedKeyStr = @"";NSString *desc = error ? error.localizedDescription : @"";if ([successKeys count]) {successKeyStr = [successKeys componentsJoinedByString:@","];}if ([failedKeys count]) {failedKeyStr = [failedKeys componentsJoinedByString:@","];}


    [strongSelf.configParams removeAllObjects];    dispatch_semaphore_signal(strongSelf.wrLock);
NSString *msg = [NSString stringWithFormat:@"Success: %@, Failed: %@, Error: %@", successKeyStr, failedKeyStr, desc]; [strongSelf showAlertControllerWithTitle:@"添加或更新参数定义" message:msg];}];
复制代码


2.删除目标自定义参数绑定含义:


/**


  • @abstract 删除目标参数绑定的值

  • @param params 包含目标参数名及其值的键值对字典对象

  • @param handler 结果回调,error 为错误信息*/


  • (void)deleteCustomParamsWith:(NSDictionary *)paramshandler:(void(^)(NSArray *successKeys, NSArray *failedKeys, NSError *error))handler;(1)params: 为包含多个自定义参数名及其对应含义的字典对象(2)handler: 为操作结果回调,successKeys 成功参数名,failedKeys 为失败参数名,error 为错误信息具体调用方式如下所示:


[MobPush deleteCustomParamsWith:dict handler:^(NSArray *successKeys, NSArray *failedKeys, NSError *error) {__strong typeof(weakSelf) strongSelf = weakSelf;NSString *successKeyStr = @"";NSString *failedKeyStr = @"";NSString *desc = error ? error.localizedDescription : @"";if ([successKeys count]) {successKeyStr = [successKeys componentsJoinedByString:@","];}if ([failedKeys count]) {failedKeyStr = [failedKeys componentsJoinedByString:@","];}NSString *msg = [NSString stringWithFormat:@"Success: %@, Failed: %@, Error: %@", successKeyStr, failedKeyStr, desc];


    [strongSelf.configParams removeAllObjects];    dispatch_semaphore_signal(strongSelf.wrLock);
[strongSelf showAlertControllerWithTitle:@"删除参数定义" message:msg];}];
复制代码


3.删除所有自定义参数绑定含义:


  • @abstract 删除所有目标参数绑定的值

  • @param handler 结果回调,error 为错误信息*/


  • (void)deleteAllCustomParamsWith:(void(^)(NSError *error))handler;(1)handler: 为操作结果回调, error 为错误信息具体调用方式如下所示:


[MobPush deleteAllCustomParamsWith:^(NSError *error) {__strong typeof(weakSelf) strongSelf = weakSelf;NSString *msg = error ? error.localizedDescription : @"success";


    [strongSelf.configParams removeAllObjects];    dispatch_semaphore_signal(strongSelf.wrLock);
[strongSelf showAlertControllerWithTitle:@"删除全部参数定义" message:msg];}];
复制代码


发布于: 刚刚阅读数: 5
用户头像

还未添加个人签名 2019-05-08 加入

还未添加个人简介

评论

发布
暂无评论
MobPush iOS端 SDK API_开发者_MobTech袤博科技_InfoQ写作社区