首页 > 其他分享 >MobPush iOS SDK API

MobPush iOS SDK API

时间:2023-03-24 13:05:52浏览次数:40  
标签:void iOS param API MobPush NSError error 推送

概述

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/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 依赖库

MobPush iOS SDK API_示例代码_02

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

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

MobPush iOS SDK API_推送_03

使用 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_推送_04

自定义推送声音

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

iOS送达统计

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

集成方法:

  1. 1.将*MobPushServiceExtension *SDK引入到您创建好的Service Extension工程中
  2. 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];

    __weak typeof(self) weakSelf = self;
    [MobPushServiceExtension deliverNotificationRequest:request MobAppSecret:@"appSecret" with:^{
        weakSelf.contentHandler(weakSelf.bestAttemptContent);
    }];
}

标签:void,iOS,param,API,MobPush,NSError,error,推送
From: https://blog.51cto.com/u_14192352/6147094

相关文章

  • vue-cli 初始化创建 vue2.9.6 项目路由守卫、封装axios、vuex
    阅读目录Vue如何封装Axios请求1安装axios2封装代码axios应用方式Vue中的路由守卫使用演示1全局守卫2组件级守卫3单个路由独享的守卫4官网整个路由守卫被触发流程的......
  • [FastAPI-21]响应状态码
    fromfastapiimportFastAPI,statusfrompydanticimportBaseModelapp=FastAPI()'''响应状态代码status'''classUser(BaseModel):username:str......
  • javaweb——使用axios和vue改造书城项目的购物车
    资料来源于:B站尚硅谷JavaWeb教程(全新技术栈,全程实战),本人才疏学浅,记录笔记以供日后回顾由于是多个视频内容混合在一起,因此只放了第一个链接视频链接代码示例index......
  • [FastAPI-18]Filed请求体校验
    importrandomfromfastapiimportFastAPIfrompydanticimportField,BaseModelimporttypingapp=FastAPI()'''请求体的每一个字段需要单独校验name长度最......
  • [FastAPI-14]pydantic多个请求体
    fromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()'''多个请求体{"user":{"username":"string","password":"string"......
  • [FastAPI-13]pydantic请求体接收数据
    fromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()'''创建继承BaseModel的类,定义模型user路径函数中定义形参user,类型为User通过对象use......
  • Android VS iOS:论移动端上的测试区别
     如今社会,移动设备的普及让移动端软件的测试工作变得更加复杂。移动设备的多样化和不断更新的操作系统为测试人员带来了诸多挑战。在移动端测试中,Android和iOS是两个最常......
  • 开源API网关APINTO:应用管理
    问题:公司的业务系统比较多,各种业务系统彼此调用,还有调用了第三方厂商的OpenAPI,现在公司面临着无法监控这些系统的调用关系以及调用量统计。更为关键的是,这些系统的鉴......
  • [FastAPI-11]Query参数校验
    importtypingfromfastapiimportFastAPI,Queryapp=FastAPI()'''查询参数使用Query校验类似路由转换使用Path校验物品名称最小3位,最大10位default=None参数......
  • 分享我通过 API 赚钱的思路
    写在最前我们经常看到非常多的API推荐,但又经常收藏到收藏夹里吃灰,仿佛收藏了就是用了。很多时候没有用起来,可能是因为想不到某类API可以用来做什么或者能应用在哪里......