簡體   English   中英

Firebase 雲消息通知 - 消息被多次接收

[英]Firebase Cloud Messaging Notification - message is received multiply times

我正在嘗試使用 react-native-firebase 在前台使用 react native 捕獲通知。

如果應用程序在后台,則通知只按應有的方式顯示一次(顯然是因為它未在應用程序內處理)。 但是當應用程序在前台時,捕獲通知的回調 function 以相當隨機的方式被多次觸發。 如果我刷新應用程序然后再次發送,通常數字會呈指數增長,可以達到 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,所以很簡單。 我的問題是我怎樣才能使回調 function messaging().onMessage 只觸發一次

如果您在卸載應用程序組件時不刪除消息監聽器,那么您的應用程序可以有多個監聽器,然后多次接收消息。 嘗試像這樣編輯代碼以在組件卸載時刪除偵聽器:

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

componentWillUnmount() {
    this.messageListener();
}

我發現問題出在熱重載上,顯然在您使用熱重載刷新應用程序時它不會刪除偵聽器,因此您需要手動從設備刷新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM