本插件是根据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';
Android端装备
注册MobSDK
在项目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 {}
}
在gradle.properties中增加代码
MobSDK.spEdition=FP
增加混杂代码
-keep class com.mob.**{*;}
-dontwarn com.mob.**
iOS端装备
渠道装备
参阅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 }
API接口调用
提交用户隐私授权成果
为保证您的App在集成MobSDK之后能够满足工信部相关合规要求,您应确保App装置初次冷启动且获得用户阅读您《隐私方针》授权之后,调用MobSDK.submitPolicyGrantResult回传隐私协议授权成果。
反之,如果用户不赞同您App《隐私方针》授权,则不能调用MobSDK.submitPolicyGrantResult回传隐私协议授权成果。 请参阅链接合规攻略
/**
* 回传用户隐私授权成果
* @param isGranted用户是否赞同隐私协议
*/
MobPushModule.submitPolicyGrantResult(Boolean isGranted);
获取RegistrationID
/**
* 获取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();
}
}
注意事项
合规攻略:www.mob.com/wiki/detail…
常见问题:www.mob.com/wiki/detail…
官方集成文档
高级装备及阐明,详见官方集成文档: www.mob.com/wiki/detail…