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, 完成!

相关文章:

  • 2022-02-02
  • 2021-10-18
  • 2022-12-23
  • 2021-11-28
  • 2022-01-15
  • 2021-06-02
  • 2021-10-25
  • 2022-02-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-25
  • 2021-09-03
  • 2021-07-11
  • 2021-04-06
  • 2021-05-12
相关资源
相似解决方案