-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathAppNavigationState.js
93 lines (86 loc) · 2.87 KB
/
AppNavigationState.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import React, { Component, } from 'react';
import { BackHandler, ToastAndroid, Platform, } from 'react-native';
import { connect, } from 'react-redux';
import { NavigationActions, } from 'react-navigation';
import JPushModule from 'jpush-react-native';
import Routers from './routers/app';
// state持久化
const persistenceKey = 'persistenceKey';
const persistNavigationState = async navState => {
try {
console.log('navState_______', navState);
await AsyncStorage.setItem(persistenceKey, JSON.stringify(navState));
} catch (err) {
// handle the error according to your needs
}
};
const loadNavigationState = async () => {
const jsonString = await AsyncStorage.getItem(persistenceKey);
return JSON.parse(jsonString);
};
const getPersistenceFunctions = __DEV__
? {
persistNavigationState,
loadNavigationState,
}
: undefined;
@connect(state => ({ nav: state.nav, }))
export default class AppNavigationState extends Component {
componentDidMount() {
if (Platform.OS === 'android') {
// 通知 JPushModule 初始化完成,发送缓存事件。
JPushModule.notifyJSDidLoad(() => {});
} else {
JPushModule.initPush();
}
// 接收自定义消息
JPushModule.addReceiveCustomMsgListener(message => {
// this.setState({ pushMsg: message, });
});
// 接收推送通知
JPushModule.addReceiveNotificationListener(message => {
// console.log(`receive notification: ${message}`);
});
// 打开通知
JPushModule.addReceiveOpenNotificationListener(() => {
console.log('Opening notification!');
!!this.root && this.root._navigation.navigate('Gong');
});
}
componentWillUnmount() {
!!this.onBackPress && BackHandler.removeEventListener('hardwareBackPress', this.onBackPress);
this.lastBackPressed = null;
JPushModule.removeReceiveCustomMsgListener();
JPushModule.removeReceiveNotificationListener();
JPushModule.removeReceiveOpenNotificationListener();
JPushModule.clearAllNotifications();
}
onBackPress = () => {
const { dispatch, nav, } = this.props;
if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
return false;
}
this.lastBackPressed = Date.now();
ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT);
dispatch(NavigationActions.back());
return true;
};
render() {
return (
<Routers
ref={ref => {
this.root = ref;
}}
{...getPersistenceFunctions}
onNavigationStateChange={(prevState, currentState) => {
const appState = currentState.routes;
if ((appState && appState.length > 1) || appState[0].index > 0) {
BackHandler.removeEventListener('hardwareBackPress', this.onBackPress);
} else {
BackHandler.addEventListener('hardwareBackPress', this.onBackPress);
}
}}
/>
);
}
}