本插件是基于MobPushSDK功能的扩展,使用此插件能帮助您在使用React Native开发应用时,快速的集成、使用推送功能。
在 package.json
文件中添加插件依赖
// 依赖版本按插件实际版本填写
"dependencies": {
"react": "18.1.0",
"react-native-mobpush": "version"
}
打开终端/命令提示行并进入到项目目录中(即包含package.json
文件的目录),运行如下命令安装:
yarn add react-native
or
npm add react-native
在React Native工程中导入如下头文件即可使用
import MobPush from 'react-native-mobpush';
在项目Gradle文件中注册MobSDK
buildscript {
repositories {
// 1.添加MobSDK Maven地址
maven {
url "https://mvn.mob.com/android"
}
}
dependencies {
// 2.注册MobSDK
classpath "com.mob.sdk:MobSDK2:+"
}
}
在项目App Module的Gradle文件中添加插件和扩展
apply plugin: 'com.mob.sdk'
MobSDK {
appKey "替换为MobTech官方申请的appkey"
appSecret "替换为MobTech官方申请的appkey对应的appSecret"
MobPush {}
}
MobSDK.spEdition=FP
-keep class com.mob.**{*;}
-dontwarn com.mob.**
参考iOS集成文档
实现文档中 Xcode配置:配置AppKey和AppSecret
iOS端基于原生MobPush SDK提供了额外的Enum选项,如下步骤可使用:
引入iOS原生模块
const MobPushModule = NativeModules.MobPushModule;
可通过模块调用对应Enum选项
MobPushModule.MPushAuthorizationOptionsBadge MobPushModule.MPushAuthorizationOptionsAlert MobPushModule.MPushAuthorizationOptionsSound
etc.
可使用Enum选项
enum MPushAuthorizationOptions {
MPushAuthorizationOptionsNone
MPushAuthorizationOptionsBadge
MPushAuthorizationOptionsSound
MPushAuthorizationOptionsAlert
}
enum MSendMessageType {
MSendMessageTypeAPNs
MSendMessageTypeCustom
MSendMessageTypeTimed
}
为保证您的App在集成MobSDK之后能够满足工信部相关合规要求,您应确保App安装首次冷启动且取得用户阅读您《隐私政策》授权之后,调用MobSDK.submitPolicyGrantResult回传隐私协议授权结果。
反之,如果用户不同意您App《隐私政策》授权,则不能调用MobSDK.submitPolicyGrantResult回传隐私协议授权结果。 请参考链接合规指南
/**
* 回传用户隐私授权结果
* @param isGranted用户是否同意隐私协议
*/
MobPushModule.submitPolicyGrantResult(Boolean isGranted);
/**
* 获取RegistrationID
* @param {Function} callback = (result) => {"success":bool,"res":regID,"error":err}
*/
static getRegistrationID(callback) {
MobPushModule.getRegistrationID(callback);
}
/*
* 消息事件监听
*
* @param {Function} callback = (result) => {"success":bool,"res":String,"error":err}
*
* success:结果,true为操作成功
*
* res: 消息结构体 JSON字符串
* */
const onLocalMessageReceive = 'onLocalMessageReceive'
const onCustomMessageReceive = 'onCustomMessageReceive'
const onNotifyMessageReceive = 'onNotifyMessageReceive'
const onNotifyMessageOpenedReceive = 'onNotifyMessageOpenedReceive'
static addNotficationListener(callback) {
const emitter = new NativeEventEmitter(MobPushModule);
const customSubscription = emitter.addListener(onCustomMessageReceive, result => {
callback(result)
}
)
const apnsSubscription = emitter.addListener(onNotifyMessageReceive, result => {
callback(result)
}
)
const localSubscription = emitter.addListener(onLocalMessageReceive, result => {
callback(result)
}
)
const clickedSubscription = emitter.addListener(onNotifyMessageOpenedReceive, result => {
callback(result)
}
)
listeners[callback] = [
customSubscription,
apnsSubscription,
localSubscription,
clickedSubscription
];
}
/*
* 新增标签
*
* 这个接口是增加逻辑,而不是覆盖逻辑
*
* @param params = {"tags": [String]}
* */
static addTags(params) {
MobPushModule.addTags(params);
}
/*
* 删除指定标签
*
* @param tags = String Array
* */
static deleteTags(params) {
MobPushModule.deleteTags(params);
}
/*
* 清除所有标签
* */
static cleanAllTags() {
MobPushModule.cleanAllTags();
}
/*
* 查询所有标签
* */
static getAllTags() {
MobPushModule.getAllTags();
}
/*
* 查询所有别名
* */
static getAlias() {
MobPushModule.getAlias();
}
/*
* 新增别名
* @param alias = String
* */
static setAlias(alias) {
MobPushModule.setAlias(alias);
}
/*
* 删除别名
* */
static deleteAlias() {
MobPushModule.deleteAlias();
}
/**
* 推送服务是否关闭
* @param {Function} callback = (result) => {"success":bool,"res":isStopeed,"error":err}
*/
static isPushStopped(callback) {
MobPushModule.isPushStopped(callback);
}
/**
* 关闭推送服务
*/
static stopPush() {
MobPushModule.stopPush();
}
/**
* 开启推送服务
*/
static restartPush() {
MobPushModule.restartPush();
}
Android:
/**
* 设置角标是否开启
*/
static setShowBadge(showbadge) {
if (Platform.OS == 'android') {
MobPushModule.setShowBadge(showbadge);
}
}
/**
* 角标是否关闭
* @param {Function} callback = (result) => {"success":bool,"res":isStopeed,"error":err}
*/
static getShowBadge(callback) {
if (Platform.OS == 'android') {
MobPushModule.getShowBadge(callback);
}
}
iOS:
/**
* 设置角标到服务器
*/
static setBadgeCount(count) {
if (Platform.OS == 'ios') {
MobPushModule.setShowBadgeCount(count);
}
}
/**
* 获取服务器角标
*/
static getBadgeCount(callback) {
if (Platform.OS == 'ios') {
MobPushModule.getShowBadgeCount(callback);
}
}
/**
* 清除角标
*/
static clearBadge() {
if (Platform.OS == 'ios') {
MobPushModule.clearBadge();
}
}
合规指南:MobTech集成文档-MobTech
常见问题:MobTech集成文档-MobTech
高级配置及说明,详见官方集成文档: MobTech集成文档-MobTech