我正在使用一个信号反应本机库来接收通知,即使我收到通知也一切正常,但是问题是当我想打开特定屏幕时。当我单击通知时,出现未定义错误,这是我的代码。
export default class App extends Component {
constructor(props){
super(props);
}
componentWillMount() {
OneSignal.init("key");
OneSignal.addEventListener('received', this.onReceived);
OneSignal.addEventListener('opened', this.onOpened);
OneSignal.addEventListener('ids', this.onIds);
}
componentWillUnmount() {
OneSignal.removeEventListener('received', this.onReceived);
OneSignal.removeEventListener('opened', this.onOpened);
OneSignal.removeEventListener('ids', this.onIds);
}
onReceived(notification) {
console.log("Notification received: ", notification);
}
onOpened(openResult) {
console.log('Message: ', openResult.notification.payload.body);
console.log('Data: ', openResult.notification.payload.additionalData, openResult.notification.payload.additionalData.catalog);
console.log('isActive: ', openResult.notification.isAppInFocus);
console.log('openResult: ', openResult);
//if (typeof openResult.notification.payload.additionalData.catalog !== 'undefined'){
this.props.navigation.navigate('CatalogInfo', {
query: openResult.notification.payload.additionalData.catalog,
});
//}
}
onIds(device) {
console.log('Device info: ', device);
}
render() {
return (
<RootStack />
);
}
}
const NavigationScreen = createStackNavigator(
{
Login: LoginScreen,
Register: RegisterScreen
}
)
const CustomDrawerComp = (props) => (
<SafeAreaView styles={{flex: 1}}>
<ScrollView>
<DrawerItems {...props} />
</ScrollView>
</SafeAreaView>
)
const MainApp = createDrawerNavigator(
{
Home: HomeScreen,
Setting: SettingScreen,
CatalogInfo: CatalogInfoScreen,
Slider: ImageSlider,
Category: CategoryScreen
},
{
contentComponent: CustomDrawerComp
}
)
const RootStack = createSwitchNavigator(
{
AuthLoading: AuthLoadingScreen,
App: MainApp,
Auth: NavigationScreen,
},
{
initialRouteName: 'AuthLoading',
}
);
即使我单击通知,this.props.navigation仍未定义
答案 0 :(得分:0)
我有同样的问题。我尝试在App.js中执行此操作,但是导航无法在其中进行,至少要在其中添加参数。
因此,我最终将其与AsyncStorage一起存储在我的主组件上(第一个屏幕),从AsyncStorage加载数据,如果其中包含数据,则导航到特定屏幕。
导航正常,当我单击通知并关闭应用程序时,只有onOpened不会运行。该应用程序正常打开,但没有导航。再次关闭该应用程序,然后重新打开它,并显示带有通知文本的特定屏幕。