rent_chat/App.js

255 lines
7.0 KiB
JavaScript

//ios Certificates key : rentchatYs962816
// twilio
// FRIENDLY NAME : rentchat
// SID : SKd9452f332bc79084af9b03b8098c18cd
// KEY TYPE : Standard
// SECRET : ImrUyVKfoQqGxC7xuBbhCcIfCePBIlU0
import React, {Component} from 'react';
import {StatusBar, Platform, Text, TextInput} from 'react-native';
import {
SafeAreaView,
createStackNavigator,
createAppContainer,
} from 'react-navigation';
import Router from './src/router';
import JMessage from 'jmessage-react-plugin';
import {colors} from './src/assets/styles/colors-theme';
import {handleNavigationChange} from './src/common/history';
import LoadingView from './src/common/loading';
import {Provider, observer} from 'mobx-react';
import NavigationService from './src/services/NavigationService';
import JPush from 'jpush-react-native';
import stores from './src/store';
import {
checkNotifications,
requestNotifications,
} from 'react-native-permissions';
//import * as stores from './src/stores/index';
import {handleErrors} from './src/common/global-error-handler';
import Event from './src/common/event';
import PushService from './src/common/PushService';
import NotifService from './src/common/NotifService';
@observer
export default class App extends Component {
constructor(props) {
super(props);
Text.defaultProps = Text.defaultProps || {};
TextInput.defaultProps = TextInput.defaultProps || {};
// Ignore dynamic type scaling on iOS
Text.defaultProps.allowFontScaling = false;
TextInput.defaultProps.allowFontScaling = false;
this.timer = null;
this.state = {
loadingCount: 0,
};
//this.notif = new NotifService(this.onRegister.bind(this));
// require('promise/setimmediate/rejection-tracking').enable({
// allRejections: true,
// onUnhandled: (id, error) => {
// handleErrors(error);
// }
// })
// this._handleGlobalError = this.handleGlobalError.bind(this)
// this._handleShowLoading = this.handleShowLoading.bind(this)
// this._handleHideLoading = this.handleHideLoading.bind(this)
}
checkPermission() {
checkNotifications().then(({status, settings}) => {
console.log(status);
if (status == 'DENIED') {
this.requestNotifications();
}
});
}
requestNotifications() {
requestNotifications([
'alert',
'sound',
'badge',
]).then(({status, settings}) => {});
}
async componentWillMount() {}
notificationClickAction(data) {
console.log(data);
if (data.hasOwnProperty('extras')) {
console.log('oh...');
if (data.extras.hasOwnProperty('_j_type')) {
if (data.extras._j_type == 'jmessage') {
if (data.notificationEventType == 'notificationOpened') {
console.log(data.extras.custom.id);
console.log(data.extras.custom.type);
console.log(data.extras.custom.nickname);
if (data.extras.custom.type == 'group') {
var data = {
id: data.extras.custom.id,
type: 'group',
nickname: 'Helper',
};
try {
NavigationService.navigate('chat', {conversation: data});
} catch (error) {
console.log(error);
}
} else {
var data = {
username: data.extras.custom.id,
type: 'single',
nickname: data.extras.custom.nickname,
};
try {
NavigationService.navigate('chat', {conversation: data});
} catch (error) {
console.log(error);
}
}
}
}
}
}
}
async componentDidMount() {
await this.checkPermission();
var params = {
appkey: '89d24c00f8f896623e0bccef',
isOpenMessageRoaming: false,
isProduction: true,
channel: '',
};
await JMessage.init(params);
await JPush.init();
JMessage.getUserInfo(
{
username: '4dbc5b294f184772a043573d7ad9b8c8',
appKey: '89d24c00f8f896623e0bccef',
},
userInfo => {
// do something.
console.log(userInfo);
},
error => {
var code = error.code;
var desc = error.description;
},
);
JMessage.setDebugMode({enable: false});
JMessage.setBadge(0, success => {});
JMessage.getMyInfo(myInfo => {
if (myInfo.username) {
} else {
console.log(myInfo);
}
});
JMessage.addSyncOfflineMessageListener(message => {
console.log(
'| JIGUANG |===addSyncOfflineMessageListener====' +
JSON.stringify(message),
);
});
this.connectListener = result => {
console.log('connectListener:' + JSON.stringify(result));
};
JPush.addNotificationListener(result => {
console.log(`notificationListener: ${JSON.stringify(result)}`);
this.notificationClickAction(result);
});
JPush.addCustomMessagegListener(result => {
console.log(`customMessageListener: ${JSON.stringify(result)}`);
});
//PushService.configure()
// 监听全局报错
// Event.listen('GLOBAL_ERROR', this._handleGlobalError, this)
// 显示加载动画
// Event.listen('SHOW_LOADING', this._handleShowLoading, this)
// 隐藏加载动画
// Event.listen('HIDE_LOADING', this._handleHideLoading, this)
}
//组件卸载之前移除监听
componentWillUnmount() {
// Event.remove('GLOBAL_ERROR', this)
//Event.remove('SHOW_LOADING', this)
// Event.remove('HIDE_LOADING', this)
// JPush.removeListener(result => {
// console.log(`remove listener: ${JSON.stringify(result)}`);
// });
}
onRegister(token) {
console.log(token);
}
onNotif(notif) {
console.log(notif);
}
render() {
return (
<Provider {...stores}>
<SafeAreaView
style={{flex: 1, backgroundColor: colors.mainColor}}
forceInset={{
top: 'always',
bottom: 'always',
}}>
<StatusBar
animated={true}
barStyle={'light-content'}
backgroundColor={colors.mainColor}
translucent={true}
/>
<Router
onNavigationStateChange={handleNavigationChange}
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
}}
/>
<LoadingView visible={this.state.loadingCount > 0} />
</SafeAreaView>
</Provider>
);
}
/**
* showLoading
*/
handleShowLoading() {
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(() => {
this.setState({
loadingCount: this.state.loadingCount + 1,
});
}, 50);
}
/**
* hideLoading
* @param bForece
*/
handleHideLoading(bForece) {
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(() => {
if (this.state.loadingCount > 0) {
this.setState({
loadingCount: bForece ? 0 : this.state.loadingCount - 1,
});
}
}, 50);
}
}