【问题标题】:Firebase Cloud Messaging Notification - message is received multiply timesFirebase 云消息传递通知 - 多次收到消息
【发布时间】:2020-07-28 08:31:36
【问题描述】:

我正在尝试使用 react-native-firebase 在前台使用 react-native-firebase 捕捉通知。

如果应用程序在后台,通知只会显示一次(显然是因为它没有在应用程序内处理)。但是当应用程序在前台时,捕获通知的回调函数会以非常随机的方式多次触发。如果我刷新应用然后再次发送,通常这个数字会成倍增长,可以达到 12 倍或更多。

我非常简单的代码:

import React, { Component } from 'react';
import {
  View,
  Text,
} from 'react-native';


import messaging from '@react-native-firebase/messaging';


class App extends Component {

  async componentDidMount() {
    messaging().onMessage(async message => console.log("message received!!!")
  }


  render(){
    return (
      <View>
        <Text>Notification App</Text>
      </View>
    )
  }
}

export default App;

为了发送通知,我使用 Firebase 云消息测试系统:

我知道我的 FCM 令牌 ID,所以很容易。我的问题是如何让回调函数 messing().onMessage 只触发一次

【问题讨论】:

    标签: react-native react-native-firebase


    【解决方案1】:

    如果您在卸载应用组件时不移除消息侦听器,那么您的应用可以有多个侦听器,然后多次接收消息。 尝试像这样编辑您的代码以在组件卸载时删除侦听器:

    async componentDidMount() {
        this.messageListener = messaging().onMessage(async message => console.log("message received!!!")
      }
    
    componentWillUnmount() {
        this.messageListener();
    }
    

    【讨论】:

    • 非常感谢,我发现问题是热重载,显然在你使用热重载刷新应用程序时它不会删除监听器,所以你需要手动从设备刷新.不过,非常感谢您的意见,当然删除监听器是一个好习惯
    【解决方案2】:

    我发现问题在于热重载,显然在您使用热重载刷新应用程序时,它不会删除侦听器,因此您需要手动从设备刷新。

    【讨论】:

    • 所以这个问题没有出现在产品中?
    • 即使我的应用通过 Firebase 应用分发分发给测试人员,我也遇到了这个问题?任何机会可能是什么情况?我已经删除了上面建议的监听器。
    猜你喜欢
    • 2020-07-16
    • 2020-06-16
    • 2019-02-06
    • 2017-12-24
    • 1970-01-01
    • 2016-12-25
    • 2017-05-05
    • 2019-10-08
    • 2018-03-13
    相关资源
    最近更新 更多