【问题标题】:React native firebase notification shows while the app is running在应用程序运行时显示 React 本机 Firebase 通知
【发布时间】:2020-07-21 23:20:04
【问题描述】:

我使用 react native firebase 5.6.0 制作了一个应用程序。我从 firebase 收到通知并用这段代码显示它们:

 this.notificationListener = firebase
  .notifications()
  .onNotification((notification: Notification) => {
    notification.android
      .setChannelId('channel')
      .android.setSmallIcon('ic_launcher')
      .android.setPriority(firebase.notifications.Android.Priority.Max)

      .android.setColor('#121243');

    firebase.notifications().displayNotification(notification);
  });

问题是推送通知即使在应用程序处于前台并正在运行时也会显示。我希望它们仅在应用处于后台时显示。

【问题讨论】:

    标签: react-native react-native-firebase


    【解决方案1】:

    我已经使用 AppState 解决了这个问题

      this.notificationListener = firebase
      .notifications()
      .onNotification((notification: Notification) => {
    if (AppState.currentState != 'active') {
        notification.android
          .setChannelId('channel')
          .android.setSmallIcon('ic_launcher')
          .android.setPriority(firebase.notifications.Android.Priority.Max)
    
          .android.setColor('#121243');
    
        firebase.notifications().displayNotification(notification);
    }
      });
    

    因此,如果应用处于活动状态,则不会显示通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-14
      • 2019-07-31
      • 2018-11-25
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多