在 React Native 中经过一定时间后删除通知 (@react-native-firebase/messaging)

ham*_*boy 5 firebase react-native react-native-firebase

我使用 @react-native-firebase/messaging 在 React Native 中工作推送通知。我在后端使用 FCM,它当前在 iOS 和 Android 上显示操作系统锁定屏幕通知。

我想在一段时间后或经过一段时间后清除给定的通知。有没有办法做到这一点?现在,当我发送通知时,如果我不点击它,它会保留几天。我想在一个小时后或下午 4 点或其他时间后记下通知。欢迎前端和/或后端解决方案。

我曾假设“ttl”(生存时间)参数执行此操作,但事实并非如此。

Ste*_*tro 3

你可以使用像react-native-background-fetch这样的后台处理程序

在你的onMessage或backgroundMessage中使用scheduleTask()在你想要的时间安排一个backgroundTask。

您可以使用react-native-push-notification来显示通知,它有一个方法cancelLocalNotifications()来取消通知在任务中您可以根据id清除通知

PushNotification.configure({
  onNotification: (notification) => {
    var {id} = remoteMessage.data
    
    BackgroundFetch.configure({
      minimumFetchInterval: 15
    }, async (taskId) => {
    
      PushNotification().cancelLocalNotifications(id)
      BackgroundFetch.finish(taskId);
    })
    
    BackgroundFetch.scheduleTask({
      taskId: id,
      forceAlarmManager: true,
      delay: 5000 
    });

  }
})
Run Code Online (Sandbox Code Playgroud)