- react-native集成文档:go48pg.yuque.com/go48pg/pa41…
- 客户端打印日志:go48pg.yuque.com/go48pg/pa41…
1、安装以下依赖
"jcore-react-native": "1.9.5",
"jpush-react-native": "2.9.7"
2、andriod/build.gragle
buildscript {
repositories {
mavenLocal()
google()
}
}
allprojects {
repositories {
mavenLocal()
maven {
url 'https://repo.huaweicloud.com/repository/maven'
content {
excludeGroup "com.facebook.react"
}
}
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url("$rootDir/../node_modules/react-native/android")
}
maven {
// Android JSC is installed from npm
url("$rootDir/../node_modules/jsc-android/dist")
}
google()
maven{ url'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
}
}
3、andriod/app/build.gradle
defaultConfig{
applicationId '极光推送的应用包名'
manifestPlaceholders = [
JPUSH_APPKEY: "xxx", //在此替换你的APPKey
JPUSH_CHANNEL: "default" //在此替换你的channel
]
}
dependencies{
implementation project(':jpush-react-native') // 添加 jpush 依赖
implementation project(':jcore-react-native') // 添加 jcore 依赖
}
4、android/app/src/main/AndroidManifest.xml(可解决JCore appKey - not defined in manifest的问题)
<meta-data
android:name="JPUSH_CHANNEL"
android:value="${JPUSH_CHANNEL}" />
<meta-data
android:name="JPUSH_APPKEY"
android:value="${JPUSH_APPKEY}" />
5、react-native推送组件
import React from "react";
import { StyleSheet, Text, View, TouchableHighlight } from "react-native";
import JPush from "jpush-react-native";
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF",
},
setBtnStyle: {
width: 320,
justifyContent: "center",
alignItems: "center",
marginTop: 10,
borderWidth: 1,
borderColor: "#3e83d7",
borderRadius: 8,
backgroundColor: "#3e83d7",
padding: 10
},
textStyle: {
textAlign: "center",
fontSize: 25,
color: "#ffffff"
}
});
class Button extends React.Component {
render() {
return <TouchableHighlight
onPress={this.props.onPress}
underlayColor='#e4083f'
activeOpacity={0.5}
>
<View
style={styles.setBtnStyle}>
<Text
style={styles.textStyle}>
{this.props.title}
</Text>
</View>
</TouchableHighlight>;
}
}
export default class App extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
JPush.setLoggerEnable(true);
JPush.init({ appKey: "极光推送的appKey", channel: "dev", production: 1 });
// 连接状态
this.connectListener = result => {
console.log("connectListener:" + JSON.stringify(result));
};
JPush.addConnectEventListener(this.connectListener);
// 通知回调
this.notificationListener = result => {
console.log("notificationListener:" + JSON.stringify(result));
alert(JSON.stringify(result));
};
JPush.addNotificationListener(this.notificationListener);
// 本地通知回调
this.localNotificationListener = result => {
console.log("localNotificationListener:" + JSON.stringify(result));
};
JPush.addLocalNotificationListener(this.localNotificationListener);
// 自定义消息回调
this.customMessageListener = result => {
console.log("customMessageListener:" + JSON.stringify(result));
};
JPush.addCustomMessageListener(this.customMessageListener);
// tag alias事件回调
this.tagAliasListener = result => {
console.log("tagAliasListener:" + JSON.stringify(result));
};
JPush.addTagAliasListener(this.tagAliasListener);
// 手机号码事件回调
this.mobileNumberListener = result => {
console.log("mobileNumberListener:" + JSON.stringify(result));
};
JPush.addMobileNumberListener(this.mobileNumberListener);
}
render() {
return (
<View style={styles.container}>
<Button title="setLoggerEnable"
onPress={() => JPush.setLoggerEnable(true)
}/>
<Button title="getRegisterID"
onPress={() => JPush.getRegistrationID(result =>
console.log("registerID:" + JSON.stringify(result))
)}/>
<Button title="设置用户属性"
onPress={() => JPush.setProperties({ sequence: 1, pros: { 2: "b" } })}/>
{/* <Button title="addTags"
onPress={() => JPush.addTags({sequence: 1, tags: ["1", "2", "3"]})}/>
<Button title="updateTags"
onPress={() => JPush.updateTags({sequence: 2, tags: ["4", "5", "6"]})}/>
<Button title="deleteTag"
onPress={() => JPush.deleteTag({sequence: 3, tags: ["4", "5", "6"]})}/>
<Button title="deleteTags"
onPress={() => JPush.deleteTags({sequence: 4})}/>
<Button title="queryTag"
onPress={() => JPush.queryTag({sequence: 4, tag: "1"})}/>
<Button title="queryTags"
onPress={() => JPush.queryTags({sequence: 5})}/>
<Button title="setAlias"
onPress={() => JPush.setAlias({sequence: 6,alias:"xxx"})}/>
<Button title="deleteAlias"
onPress={() => JPush.deleteAlias({sequence: 7})}/>
<Button title="queryAlias"
onPress={() => JPush.queryAlias({sequence: 8})}/>
<Button title="setMobileNumber"
onPress={() => JPush.setMobileNumber({mobileNumber: "13888888888"})}/>
//仅ios
<Button title="setBadge"
onPress={() => JPush.setBadge({"badge":1,"appBadge":1})}/>
<Button title="initCrashHandler"
onPress={() => JPush.initCrashHandler()}/>
<Button title="addLocalNotification"
onPress={() => JPush.addLocalNotification({
messageID: "123456789",
title: "title123",
content: "content123",
extras: {"key123": "value123"}
})}/>
<Button title="removeLocalNotification"
onPress={() => JPush.removeLocalNotification({messageID: '123456789'})}/>*/}
</View>
);
}
}
问题:
1、react native 出现程序包com.facebook.react不存在blog.csdn.net/qq_38847655…
方法一:react-native版本低于0.63,在 android\build.gradle 添加如下内容:
def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())
allprojects {
configurations.all {
resolutionStrategy {
// Remove this override in 0.65+, as a proper fix is included in react-native itself.
force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
}
}
方法二:react-native版本大于0.63,根据官网issue(Android build failures happening since Nov 4th 2022 · Issue #35210)里找到对应的热更新补丁,更新 package.json 内容,重新 yarn install,然后 cd android && ./gradlew clean 清理缓存,之后应该就恢复正常了