import {
AppRegistry,
StyleSheet,
Text,
View,
Platform,
NativeAppEventEmitter,
DeviceEventEmitter,
} from 'react-native';
//添加DeviceEventEmitter
componentDidMount() {
if(Platform.OS=='android'){
this.subscription = DeviceEventEmitter.addListener('userNameDidChange',(userName) => {
alert('通知');
})
}else {
this.subscription = NativeAppEventEmitter.addListener('userNameDidChange',(userName) => {
alert('通知');
})
}
},
移除DeviceEventEmitter
componentWillUnmount() {
// 移除
this.subscription.remove();
},
发送通知
DeviceEventEmitter.emit('userNameDidChange', '通知来了');
OK, 完成!