我在我的RN应用中使用react-native-fcm组件进行推送通知。它运行完美,但我注意到一个令我抓狂的问题,当我们在play store上发布新版本,应用程序更新令牌过期时,我们已经保存了旧令牌,并向该fcm令牌发送通知,因此用户停止接收通知。
如果用户注销并再次登录,我将获得新的令牌,但我当然不能强制我的用户这样做,在组件示例中,我发现了一个刷新令牌事件,但它不工作,您知道为什么事件在刷新时没有收到新的令牌吗?我做错了什么?谢谢。
以下是我的代码(为了可读性,我删除了一些部分):
import React, { Component } from 'react';
import { ScrollView, View, Text, Linking, TouchableOpacity, AppState } from 'react-native';
import { connect } from 'react-redux';
import FCM, { FCMEvent } from 'react-native-fcm';
import { getTopNotification, updateUser } from '../api';
import { topNotificationChanged, logOut } from '../actions';
class Home extends Component {
constructor() {
super();
this.onLogOutPress.bind(this);
}
state = { topNotificationLink: '', appState: AppState.currentState };
componentDidMount() {
AppState.addEventListener('change', this.handleAppStateChange);
this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, fcmToken => {
updateUser(this.props.user.id, this.props.user.token, { fcmToken });
});
}
componentWillUnmount() {
AppState.removeEventListener('change', this.handleAppStateChange);
this.refreshTokenListener.remove();
}
handleAppStateChange = (nextAppState) => {
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
this.onFocus();
}
this.setState({ appState: nextAppState });
}
render() {
return (
<ScrollView>
{this.renderTopNotification()}
{this.renderMenuOptions()}
</ScrollView>
);
}
}
const mapStateToProps = state => {
return {
user: state.auth.user,
lang: state.auth.lang,
topNotificationText: state.main.topNotificationText
};
};
export default connect(mapStateToProps, { topNotificationChanged, logOut })(Home);https://stackoverflow.com/questions/44416337
复制相似问题