【问题标题】:react-native-fcm token refresh when app is updated更新应用程序时react-native-fcm令牌刷新
【发布时间】:2017-11-09 00:35:27
【问题描述】:

我在我的 RN 应用程序中使用 react-native-fcm 组件来推送通知。 它运行良好,但我注意到一个让我发疯的问题,当我们 在 Play 商店发布新版本,应用更新令牌过期, 我们已经保存了旧令牌并向该 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);

【问题讨论】:

  • 您通过 Apple Store 或 Play Store 更新 iOS 时是否遇到同样的问题?
  • 我还没有发布iOS版本
  • 您解决了这个问题吗?我们有同样的问题。

标签: javascript firebase react-native firebase-cloud-messaging


【解决方案1】:
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');
} 

【讨论】:

    【解决方案2】:
    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');
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2017-05-10
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      相关资源
      最近更新 更多