首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新应用时刷新react-native-fcm令牌

更新应用时刷新react-native-fcm令牌
EN

Stack Overflow用户
提问于 2017-06-07 23:03:22
回答 0查看 3.2K关注 0票数 5

我在我的RN应用中使用react-native-fcm组件进行推送通知。它运行完美,但我注意到一个令我抓狂的问题,当我们在play store上发布新版本,应用程序更新令牌过期时,我们已经保存了旧令牌,并向该fcm令牌发送通知,因此用户停止接收通知。

如果用户注销并再次登录,我将获得新的令牌,但我当然不能强制我的用户这样做,在组件示例中,我发现了一个刷新令牌事件,但它不工作,您知道为什么事件在刷新时没有收到新的令牌吗?我做错了什么?谢谢。

以下是我的代码(为了可读性,我删除了一些部分):

代码语言:javascript
复制
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);
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44416337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档