首页 > 解决方案 > 使用 FCM 发送通知时,在 ios 上获取单个事件的双重通知弹出窗口

问题描述

问题描述::

我正在开发 react native 应用程序并使用 react native firebase 消息传递服务进行推送通知。我在IOS平台上遇到问题。我收到单个事件的双重通知弹出窗口。

我正在遵循的步骤来生成案例::

  1. 安装应用程序后,如果我登录并通过 FCM 发送通知,我只会收到一个弹出窗口。在此之后我注销并再次登录,现在这一次我得到了单个通知的双弹出窗口。在这种情况下,我不会从后台清除应用程序。

  2. 如果每次注销后我都从后台清除应用程序,我只收到一个单一事件的弹出窗口。

  3. 当我从应用程序中注销并从 FCM 强行发送通知时,我在应用程序初始化屏幕(登录屏幕)上出现双重弹出窗口。

我在用户登录时生成一个新的设备令牌并将此令牌保存在本地存储中,我们在注销时清除本地存储数据。

代码::

async mountDashboard() {
    const enabled = await firebase.messaging().hasPermission();
    if (enabled) {
        const fcmToken = await firebase.messaging().getToken();
        await AsyncStorage.setItem(LocalStorageKeys.DEVICE_TOKEN, fcmToken);
        if (fcmToken) {
            //--- here we are saving our token and sendind data to API
        }
    }

    // in Foreground
    this.notificationListener = firebase.notifications().onNotification((notification) => {
        new RegisterLocalNotification(notification);
    });

    // App in Foreground and background
    let notificationHandler = new NotificationsHandler();
    this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
        notificationHandler.handleNotification(notificationOpen.notification);
    });

    // app close notification handler
    AppCloseNotificationHandler.getInstance().handleNotification();

}

componentDidMount() {
    this.mountDashboard();
}

环境::

二进制文件:

npm 包:

npm 全局包:

标签: react-nativereact-native-firebase

解决方案


当组件卸载时,您必须取消订阅您的侦听器。如果你不这样做,你就订阅了两个听众。

componentWillUnmount() {
    this.notificationListener(); // it's unsubscribing your listener
}

推荐阅读