import {
AsyncStorage, FlatList, Image, StatusBar, TouchableOpacity, View, Dimensions,ToastAndroid,
ActivityIndicator, Platform, ProgressBarAndroid, Text, PixelRatio,ImageBackground,BackHandler,
NativeModules,Modal,PermissionsAndroid,
} from "react-native";
import React from "react";
export default class HomeScreen extends React.PureComponent {
componentDidMount() {
if (Platform.OS === 'android') {
JPushModule.initPush()
JPushModule.getInfo(map => {
this.setState({
appkey: map.myAppKey,
imei: map.myImei,
package: map.myPackageName,
deviceId: map.myDeviceId,
version: map.myVersion
})
})
JPushModule.notifyJSDidLoad(resultCode => {
if (resultCode === 0) {
}
})
} else {
JPushModule.setupPush()
}
JPushModule.addReceiveCustomMsgListener(map => {
this.setState({
pushMsg: map.message
})
console.log('extras: ' + map.extras)
})
JPushModule.addReceiveNotificationListener(map => {
console.log('alertContent: ' + map.alertContent)
console.log('extras: ' + map.extras)
// var extra = JSON.parse(map.extras);
// console.log(extra.key + ": " + extra.value);
AsyncStorage.setItem('receivedNotification','true');
this.setState({receivedNotification:true});
NativeModules.VersionModule.showBadgeCount(1);
})
JPushModule.addReceiveOpenNotificationListener(map => {
console.log('Opening notification!')
console.log('map.extra: ' + map.extras)
console.log('map.alertContent: ' + map.alertContent)
console.log('map.id', map.id)
//this.jumpSecondActivity()
// JPushModule.jumpToPushActivity("SecondActivity");
//alert(JSON.stringify(map.extras));
AsyncStorage.setItem('receivedNotification','false');
this.setState({receivedNotification:false});
NativeModules.VersionModule.removeBadgeCount();
this._toMessageDetail(map,map.extras);
})
JPushModule.addGetRegistrationIdListener(registrationId => {
console.log('Device register succeed, registrationId ' + registrationId)
})
this._findMessage();
}
componentWillUnmount() {
if (Platform.OS === 'android') {
BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
}
JPushModule.removeReceiveCustomMsgListener(receiveCustomMsgEvent)
JPushModule.removeReceiveNotificationListener(receiveNotificationEvent)
JPushModule.removeReceiveOpenNotificationListener(openNotificationEvent)
JPushModule.removeGetRegistrationIdListener(getRegistrationIdEvent)
console.log('Will clear all notifications')
JPushModule.clearAllNotifications()
}
constructor(props) {
super(props);
this.state = {
receivedNotification:false,
}
}
render() {
return (
<View style={{flex:1,backgroundColor:'#FFFFFF'}}>
<View style={{}}>
<TouchableOpacity style={{padding:10}} onPress={this._handleMessage.bind(this)} activeOpacity={0.7} disabled={this.state.error}>
<ImageBackground style={{width:20,height:15,justifyContent:'flex-start',alignItems:'flex-end',}}
source={require('../img/icon_notification.png')} resizeMode='contain'>
{this.state.receivedNotification?<View style={{backgroundColor:'red',width:6,height:6,borderRadius:3,borderColor:'red'}}/>:null}
{/* <Image style={{width:20,height:15,resizeMode:Image.resizeMode.contain}} source={require('../img/icon_notification.png')} /> */}
</ImageBackground>
</TouchableOpacity>
</View>
</View>)
}
async _findMessage(){
AsyncStorage.setItem('receivedNotification','false');
let value = await AsyncStorage.getItem('receivedNotification');
//alert(value)
if(value&&value==='true'){
this.setState({receivedNotification:true});
//显示桌面红点
NativeModules.VersionModule.showBadgeCount(1);
}else{
this.setState({receivedNotification:false});
//移除桌面红点
NativeModules.VersionModule.removeBadgeCount();
}
}
async _handleMessage(){
let value = await AsyncStorage.getItem('user');
let user = JSON.parse(value);
if(user===null||user===undefined){
this.props.navigation.navigate('SignIn',{
callback: (refresh) => {
if(refresh){
this._fetchData();
}
},
})
}else{
AsyncStorage.setItem('receivedNotification','false');
this.setState({receivedNotification:false});
NativeModules.VersionModule.removeBadgeCount();
this.props.navigation.navigate('MessageListScreen',{
callback: (refresh) => {
if(refresh){
this._refreshProfile();
}
},
});
}
}
网友评论