javascript - 更新 React Native Android 后 OneSignal 无法正常工作
问题描述
我遵循了 3.2.12 版的指导方针,
并按照官方文档中的说明进行操作,但没有生成玩家ID,请帮忙。
升级到 3.2.12 后初始化停止,现在它们不支持 componentWillMount() 上的初始化
import React, { Component } from "react";
import {
Text,
View,
Image,
TouchableOpacity,
I18nManager,
AsyncStorage,
Platform
} from "react-native";
import {
Container,
Right,
Item,
Input,
Header,
Left,
Body,
Title,
Form
} from "native-base";
import FontAwesome from "react-native-vector-icons/FontAwesome";
import Ionicons from 'react-native-vector-icons/Ionicons';
// Screen Styles
import styles from "../Theme/Styles/Signin";
import Logo from "../image/qualpros.png";
import axios from 'axios';
import AwesomeAlert from 'react-native-awesome-alerts';
import OneSignal from 'react-native-onesignal';
class TutorSignInScreen extends Component {
static navigationOptions = {
header: null,
showAlert: false,
message: '',
}
state = {
data: [],
email: null,
password: null,
device_id: null,
showAlert : false
}
constructor(properties) {
super(properties);
OneSignal.init("my-onesignal-id");
OneSignal.addEventListener('received', this.onReceived);
OneSignal.addEventListener('opened', this.onOpened);
OneSignal.addEventListener('ids', this.onIds);
}
componentWillUnmount() {
OneSignal.removeEventListener("received", this.onReceived);
OneSignal.removeEventListener("opened", this.onOpened);
OneSignal.removeEventListener("ids", this.onIds);
}
onReceived(notification) {
console.log("Notification received: ", notification);
}
onOpened(openResult) {
console.log("Message: ", openResult.notification.payload.body);
console.log("Data: ", openResult.notification.payload.additionalData);
console.log("isActive: ", openResult.notification.isAppInFocus);
console.log("openResult: ", openResult);
}
onIds(device) {
let playerid = device.userId;
//console.log("Device info: ", playerid);
AsyncStorage.setItem('playerid', playerid);
this.setState ({
device_id: playerid
})
}
showAlert = () => {
this.setState({
showAlert: true
});
};
hideAlert = () => {
this.setState({
showAlert: false
});
};
signIn = async () => {
let device_type = (Platform.OS === 'ios') ? 'ios' : 'android'
try {
let { data } = await axios.post('url', {
email: this.state.email,
password: this.state.password,
//device_id: this.state.device_id,
device_id: '123test',
device_type: device_type
})
.then((response) => {
if (response.data.data.status === 'success') {
this.setState({ data: response.data.data })
console.log(response.data.data)
AsyncStorage.setItem('username', response.data.data.user_info.username);
AsyncStorage.setItem('userType', response.data.data.user_info.user_role_name);
AsyncStorage.setItem('latitude', response.data.data.user_info.latitude);
AsyncStorage.setItem('longitude', response.data.data.user_info.longitude);
AsyncStorage.setItem('user_id', response.data.data.user_info.user_id.toString());
AsyncStorage.setItem('user_role_id', response.data.data.user_info.user_role_id.toString());
this.props.navigation.navigate(response.data.data.user_info.user_role_name === 'Student' ? 'App' : 'Tutor')
} else {
this.setState({
message: response.data.data.message,
showAlert: true,
})
}
})
} catch (err) {
console.log(err);
}
}
showAlert = () => {
this.setState({
showAlert: true
});
};
hideAlert = () => {
this.setState({
showAlert: false
});
};
render() {
return (
<Container>
<Header style={styles.header}>
<Left style={styles.left}>
<TouchableOpacity
style={styles.backArrow}
onPress={() => this.props.navigation.navigate("Welcome")}
>
<View style={{ flexDirection: "row" }}>
<FontAwesome name="angle-left" size={30} color="black" />
<Text style={{ margin: 5, marginTop: 6, color: "black", fontSize: 12}}>Back</Text>
</View>
</TouchableOpacity>
</Left>
<Body style={styles.body} />
<Right style={styles.right} />
</Header>
<View style={styles.logosec}>
<Image source={Logo} style={styles.logostyle} />
</View>
<Form style={styles.form}>
<Item rounded style={styles.inputStyle}>
<Input
textAlign={I18nManager.isRTL ? "right" : "left"}
placeholder="Email"
style={styles.inputmain}
onChangeText={(email) => { this.setState({ email }) }}
autoCapitalize='none'
/>
</Item>
<Item rounded style={[styles.inputStyle, { marginTop: 10 }]}>
<Input
placeholder="Password"
secureTextEntry={true}
textAlign={I18nManager.isRTL ? "right" : "left"}
style={styles.inputmain}
onChangeText={(password) => { this.setState({ password }) }}
autoCapitalize='none'
/>
</Item>
<TouchableOpacity
info
style={styles.signInbtn}
onPress={this.signIn}
>
<Text autoCapitalize="words" style={styles.buttongetstarted}>
Sign In
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => this.props.navigation.navigate('ForgetPassword')}>
<Text autoCapitalize="words" style={styles.buttongettext}>
Forgot your password?
</Text>
</TouchableOpacity>
</Form>
<View style={styles.bottomView}>
{/* <TouchableOpacity
style={styles.fbButton}
onPress={() => alert("Facebook button Clicked")}
>
<View iconRight style={styles.fbview}>
<Ionicons name="logo-linkedin" size={30} color="white" />
<Text autoCapitalize="words" style={styles.fbButtonText}>
Sign in with LinkedIn
</Text>
</View>
</TouchableOpacity> */}
<TouchableOpacity
style={styles.bottomText}
onPress={() => { this.props.navigation.navigate('TutorSignUpScreen') }}>
<Text style={styles.bottomText01}>
Don't have an account?{" "}
<Text style={styles.bottomText02}>Sign up</Text>
</Text>
</TouchableOpacity>
</View>
<AwesomeAlert
show={this.state.showAlert}
showProgress={false}
//title="QualPros!"
message={this.state.message}
closeOnTouchOutside={true}
closeOnHardwareBackPress={false}
showConfirmButton={true}
confirmText="Ok"
confirmButtonColor="#d91009"
onConfirmPressed={() => {
this.hideAlert();
}}
/>
</Container>
);
}
}
export default TutorSignInScreen;
环境:“react-native-onesignal”:“^3.2.12”,“react”:“16.6.1”,“react-native”:“0.57.7”
解决方案
推荐阅读
- c - 如何从其他函数中的函数访问 (const char*)?
- android - 如何调整 Scrollview 的高度和宽度以适应多种屏幕分辨率
- elasticsearch - 服务发现不会发现多个节点
- html - div下方的未知空间?
- angular2-nativescript - 在 nativescript-angular 如何检查是否
是空的? - mongodb - Mongoose {$exists: false} 不工作,为什么?
- android-studio-3.0 - 使用 Apache POI 时如何修复 Android Studio 中的 Java 编译器错误
- algorithm - 是否将数组拆分为 2 个子数组并递归求解它们仍然 O(log(n))?
- swift - 为什么没有调用observeValues 块?
- c# - 尝试创建通用绑定方法时出错