【问题标题】:Clear Previous Expo Push Notifications清除以前的世博会推送通知
【发布时间】:2017-09-02 00:13:17
【问题描述】:

当有人发送消息时,我在 Expo 中的应用会推送通知,但如果该人发送多条消息,则会推送第二个通知。

我可以做些什么来清除之前的通知,或者只是更新通知而不是在列表中添加第二个通知?

基本上我需要强制覆盖或关闭以前的通知。

我希望使用的方法是添加一个侦听器,该侦听器在附加之前清除通知,但似乎这仅在应用程序处于前台时才有效。

目前有推荐的方法吗?

【问题讨论】:

  • 您能解释一下您是如何处理这个问题的吗?我有完全一样的问题。谢谢

标签: react-native push-notification notifications expo


【解决方案1】:

您可以清除expo-notifications 中的任何或所有之前的通知。您的问题不清楚,但我猜如果收到新通知,您想清除所有以前的通知。您必须找到清除代码通知的最佳位置。但这里有一些提示(在 useEffect 挂钩中使用以下代码)-

// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
responseListener.current =
  Notifications.addNotificationResponseReceivedListener((response) => {
    // DISMISS ALL NOTIFICATION AFTER INTERACTION
    Notifications.dismissAllNotificationsAsync();
  });

如果您想在这种情况下关闭特定通知,请使用dismissNotificationAsync(identifier: string): Promise 方法。

以防万一,如果您想在应用处于前台时收到新通知时关闭所有通知(在 useEffect 挂钩中使用以下代码)。

// This listener is fired whenever a notification is received while the app is foregrounded
notificationListener.current =
  Notifications.addNotificationReceivedListener((notification) => {
    // setNotification(notification);
    // DISMISS ALL NOTIFICATION UPON RECEIVING NOTIFICATION WHILE IN FOREGROUND
    Notifications.dismissAllNotificationsAsync();
  });

您可以在任何您喜欢的地方使用Notifications.dismissAllNotificationsAsync() 方法或dismissNotificationAsync(identifier: string): Promise 方法,但您需要找出最适合使用它们的位置。

【讨论】:

  • 我无法验证这一点,因为我从事的项目不再有效。为此,我无法测试您的解决方案在应用程序处于后台时是否有效,这是我的根本问题
【解决方案2】:
  _handleNotification = (notification) => {
  this.setState({notification: notification});
  console.log('get notification', this.state.notification);
  let localnotificationId = this.state.notification.notificationId;
  setTimeout(function () {
    Notifications.dismissNotificationAsync(localnotificationId);
  }, 10000)

};

这就是我在 NodeJS 中的做法

【讨论】:

    猜你喜欢
    • 2021-03-02
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 2021-07-07
    • 2022-06-19
    相关资源
    最近更新 更多