本插件是基于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();

}

}

注意事项

合规指南:MobTech集成文档-MobTech

常见问题:MobTech集成文档-MobTech

官方集成文档

高级配置及说明,详见官方集成文档: MobTech集成文档-MobTech

参考阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: 


大家都在找:

react native:react native是什么

javascript:javascript怎么开启

c#:c#编程软件

大家都在看: