使用FCM发送通知时,在ios上针对单个事件获取双重通知弹出式窗口

时间:2019-03-07 11:47:00

标签: react-native react-native-firebase

问题说明::

我正在研究React本机应用程序,并使用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();
}

环境::

二进制文件:

  • 节点:10.15.0-/ usr / local / opt / node @ 10 / bin / node
  • 纱线:1.10.1-/ usr / local / bin / yarn
  • npm:6.4.1-/ usr / local / opt / node @ 10 / bin / npm
  • Watchman:4.9.0-/ usr / local / bin / watchman
  • 列表项

npm软件包:

  • 反应:^ 16.8.4 => 16.8.4
  • 本机:^ 0.58.6 => 0.58.6

npm全球软件包:

  • react-native-cli:2.0.1
  • react-native-firebase:5.2.3

2 个答案:

答案 0 :(得分:1)

在卸载组件时,您必须取消订阅侦听器。如果您不这样做,那么您将订阅两个侦听器。

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

答案 1 :(得分:0)

请确保通过后端发送此有效负载,我正在使用firebase admin SDK。 这将禁用操作系统通知,并触发本地通知。我正在使用此软件包进行本地通知https://github.com/zo0r/react-native-push-notification

async function sendPushNotification(message) {

try {
    await admin.messaging().sendToDevice(
        ['dCk27uEySymSdP_vA1C_QI:APA91bEZNyipZDjTLq0jrGnD0qcpKH2y3oTYg3GMgT0pSENNlJEiymOYXvxvnqTFtQaidDLt5KUgp4oDZsfLsQvfiVkL7m1bpMjekHsm-7x1ZDju4TYUMUZeUgYb0CyPwMhdr9RyzA1v'],
        {
            data: {
                owner: 'a',
                user: 'a',
            },
        },
        {
            // Required for background/quit data-only messages on iOS
            contentAvailable: true,
            // Required for background/quit data-only messages on Android
            priority: 'high',
        },
    );

    console.log('A');

}catch(e) {
    console.log('Gagal mengirim pesan');
}

}

这是我在App.js中监听通知的代码

RNFirebase.onMessage(RNFirebaseOnMessageHandler); RNFirebase.setBackgroundMessageHandler(RNFirebaseOnMessageHandler);

在处理程序中,我使用此

PushNotification.createChannel(
    {
      channelId: CHANNEL_ID,
      channelName: CHANNEL_NAME,
      channelDescription: CHANNEL_DESCRIPTION,
      soundName: CHANNEL_SOUND,
      importance: CHANNEL_IMPORTANCE,
      vibrate: CHANNEL_VIBRATE,
    },
    (created) => console.log(`createChannel returned '${created}'`), // (optional) callback returns whether the channel was created, false means it already existed.
  );
  PushNotification.localNotification({
    channelId: CHANNEL_ID,
    // @todo get ticker value from data payload
    ticker: 'CreateOrderLandingScreen',
    showWhen: true,
    autoCancel: true,
    largeIcon: CHANNEL_LARGE_ICON,
    smallIcon: CHANNEL_SMALL_ICON,
    bigText: 'aaaaa',
    subText: CHANNEL_SUB_TEXT,
    color: Colors.RED,
    vibrate: true,
    vibration: 300,
    priority: 'high',
    visibility: 'private',
    invokeApp: true,
    alertAction: 'view',
    id: 0,
    title:'aa',
    message: 'aaaa',
    userInfo: {},
    playSound: false,
    soundName: 'default',
  });