通知 - 清除安卓通知栏中的所有通知

5

我正在向我的应用发送多个通知。 我想要实现的是,每当用户点击一个通知时,通知托盘中的所有通知都会消失。

我尝试添加

notification.android.setAutoCancel(true)

这个技巧只适用于一个通知(即被单击的那一个)。

我也尝试过:

firebase.notifications().removeAllDeliveredNotifications() 

这样做没有任何效果。

我该怎么做才能实现这个目标?

这是完整的代码:

      componentDidMount() {
    firebase.notifications().removeAllDeliveredNotifications()

    this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification) => {
    });

    this.notificationListener = firebase.notifications().onNotification(async (notification) => {
      // Process your notification as required
      notification.android.setAutoCancel(true)
      firebase.notifications().removeAllDeliveredNotifications()
  }



    async componentWillMount() {
    this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification) => {
    });
    this.notificationListener = firebase.notifications().onNotification((notification) => {
    });

    this.notificationDisplayedListener();
    this.notificationListener();
    }
1个回答

5
尝试将删除通知的代码 (removeAllDeliveredNotifications()) 从 onNotification 监听器移至 onNotificationOpened 监听器。由于你正在尝试在通知到达时删除通知,可能会出现竞争条件。
此外,因为您希望在用户点击一个通知时清除所有通知。
注:请将通知监听器保留在 componentDidMountcomponentWillMount 中,不要同时使用这两个方法。

太棒了,谢谢!我已经尝试过多次删除它(不在两个位置上拥有监听器),但是通知出现了某些问题停止工作 :| - Dan Dinu
嘿,我有同样的问题,你能帮忙看一下吗?@NishantNair https://dev59.com/FrXna4cB1Zd3GeqPIEYf#57181291 - DevAS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接