MobPush丨iOS端SDK API

2年前 (2022) 程序员胖胖胖虎阿
235 0 0

概述
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内指定页面的操作,可以在开发者管理后台打开配置开关和参数设置。MobPush丨iOS端SDK API

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

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

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

/**
 设置控制器路径
 @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 依赖库MobPush丨iOS端SDK API

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

开启富媒体地址Http访问支持
MobPush丨iOS端SDK API

使用 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端SDK API

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

版权声明:程序员胖胖胖虎阿 发表于 2022年9月4日 上午9:48。
转载请注明:MobPush丨iOS端SDK API | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...