Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更新应用程序时响应本机fcm令牌刷新_Javascript_Firebase_React Native_Firebase Cloud Messaging - Fatal编程技术网

Javascript 更新应用程序时响应本机fcm令牌刷新

Javascript 更新应用程序时响应本机fcm令牌刷新,javascript,firebase,react-native,firebase-cloud-messaging,Javascript,Firebase,React Native,Firebase Cloud Messaging,我正在我的RN应用程序中使用react native fcm组件进行推送通知。 它工作得很完美,但我注意到一个让我发疯的问题,当我们 在play store上发布新版本,并在令牌到期时更新应用程序, 我们已保存旧令牌,并向该fcm令牌发送通知, 因此用户停止接收通知 如果用户注销并再次登录,我会得到新的令牌,但当然我不能强迫我的用户这样做,在组件示例中,我发现了一个刷新令牌事件,但它不起作用,您知道为什么事件在刷新时没有收到新令牌吗?我做错了什么?谢谢 这是我的代码(为了可读性,我删除了一些部分

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

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

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

import React,{Component}来自'React';
从“react native”导入{ScrollView、View、Text、Linking、TouchableOpacity、AppState};
从'react redux'导入{connect};
从'react native FCM'导入FCM,{FCMEvent};
从“../api”导入{getTopNotification,updateUser};
从“../actions”导入{topNotificationChanged,注销};
类Home扩展组件{
构造函数(){
超级();
this.onLogOutPress.bind(this);
}
状态={topNotificationLink:'',appState:appState.currentState};
componentDidMount(){
AppState.addEventListener('change',this.handleAppStateChange);
this.refreshtTokenListener=FCM.on(FCMEvent.refreshtToken,fcmToken=>{
updateUser(this.props.user.id,this.props.user.token,{fcmToken});
});
}
组件将卸载(){
AppState.removeEventListener('change',this.handleAppStateChange);
this.refreshTokenListener.remove();
}
HandLeapStateChange=(nextAppState)=>{
if(this.state.appState.match(/inactive | background/)和&nextapstate=='active'){
这个。onFocus();
}
this.setState({appState:nextapstate});
}
render(){
返回(
{this.renderTopNotification()}
{this.renderNuOptions()}
);
}
}
常量mapStateToProps=状态=>{
返回{
用户:state.auth.user,
lang:state.auth.lang,
topNotificationText:state.main.topNotificationText
};
};
导出默认连接(mapStateToProps,{topNotificationChanged,logOut})(主页);

通过Apple Store或Play Store进行iOS更新时,您会遇到同样的问题吗?我还没有发布iOS版本。您解决了这个问题吗?我们有同样的问题。
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);
import firebase from 'react-native-firebase';
import { RemoteMessage } from 'react-native-firebase';    

componentDidMount() {
    this.checkPermission();
    this.onTokenRefreshListener = firebase.messaging().onTokenRefresh(fcmToken => {
      // Process your token as required
      console.log("Updated Token=" + fcmToken);
    });
    this.messageListener = firebase.messaging().onMessage((message) => {
      // Process your message as required
    });

  }
  componentWillUnmount() {
    this.onTokenRefreshListener();
    this.messageListener();
  }
async checkPermission() {
    const enabled = await firebase.messaging().hasPermission();
    console.log("checkPermission=" + enabled);
    if (enabled) {
      // this.getFCMToken();
      // testTocken();
      let fcmToken = await AsyncStorage.getItem('fcmToken', "");
      console.log("getToken=" + enabled);

      if (!fcmToken) {
        fcmToken = await firebase.messaging().getToken();
        alert(fcmToken);
        if (fcmToken) {
          // user has a device token
          await AsyncStorage.setItem('fcmToken', fcmToken);
        }
      }
    } else {
      this.requestPermission();
    }
  }

  async requestPermission() {
    try {
      await firebase.messaging().requestPermission();
      // User has authorised
      // this.getFCMToken();
      let fcmToken = await AsyncStorage.getItem('fcmToken', "");
      console.log("getToken=" + enabled);

      if (!fcmToken) {
        fcmToken = await firebase.messaging().getToken();
        alert(fcmToken);
        if (fcmToken) {
          // user has a device token
          await AsyncStorage.setItem('fcmToken', fcmToken);
        }
      }
    } catch (error) {
      // User has rejected permissions
      console.log('permission rejected');
    }
  }
const oldToken = await firebase.messaging().getToken();
await firebase.messaging().deleteToken();
const newToken = await firebase.messaging().getToken();
if (oldToken === newToken) {
    console.error('Token has not been refreshed');
}