如何在React Native中将多个推送通知RNFirebase分组/合并?

时间:2019-06-21 10:41:34

标签: firebase react-native rnfirebase

我在React Native中对多个推送通知进行分组/合并时遇到问题,我正在使用RNFirebase(https://rnfirebase.io/),我查找了很多示例,但都失败了,我想对多个推送通知进行分组/合并如WhatsApp,如下面的图片。

enter image description here

这是我的脚本:

async componentDidMount() {
    if(Platform.OS === "android") {
      const channel = new firebase.notifications.Android.Channel('test-channel', 'Test Channel', firebase.notifications.Android.Importance.Max)
                      .setDescription('My apps test channel');
      // Create the channel
      firebase.notifications().android.createChannel(channel);
    }

    this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((Notification) => {

    });
    this.notificationListener = firebase.notifications().onNotification((Notification) => {
      const notification = Notification;
      if(Platform.OS === "android") {
        notification
          .setNotificationId(notification.notificationId)
          .setTitle(notification.title)
          .setSubtitle(notification.subtitle)
          .setBody(notification.body)
          .setData(notification.data)
          .setSound("default")
          .android.setGroup('test')
          .android.setGroupSummary(true)
          .android.setChannelId('test-channel')
          .android.setSmallIcon('ic_notification')
          .android.setLargeIcon('ic_launcher_round')
          .android.setAutoCancel(true)
          .android.setColor('#470a49');

        firebase.notifications()
          .displayNotification(notification);
      }else if(Platform.OS === "ios") {
        notification
          .setNotificationId(notification.notificationId)
          .setTitle(notification.title)
          .setSubtitle(notification.subtitle)
          .setBody(notification.body)
          .setData(notification.data)
          .setSound("default")
          .ios.setBadge(notification.ios.badge);

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

    this.notificationOpenedListener = firebase.notifications().onNotificationOpened((NotificationOpen) => {
      const notification = NotificationOpen.notification;

                firebase.notifications().removeDeliveredNotification(notification.notificationId);
    });

    //When notification received when app is closed
    this.initialNotification = firebase.notifications().getInitialNotification()
      .then((notificationOpen) => {
          if (notificationOpen) {
              const notification = notificationOpen.notification;

          }
      });
  }

  componentWillUnmount() {
    this.notificationDisplayedListener();
    this.notificationListener();
    this.notificationOpenedListener();
  }

请任何人帮助我。

谢谢。

0 个答案:

没有答案