在 React Native (@react-native-firebase/messaging) 中的特定时间后删除通知
Remove notification after a certain time in React Native (@react-native-firebase/messaging)
我使用@react-native-firebase/messaging 在 React Native 中使用推送通知。我在后端使用 FCM,它目前在 iOS 和 Android.
上显示 OS 锁屏通知
我想在特定时间后或一段时间后清除给定的通知。有没有办法做到这一点?现在,当我发送通知时,如果我不点击它,它会停留几天。我想在一个小时后或下午 4 点或其他时间取消通知。欢迎前端 and/or 后端解决方案。
我原以为“ttl”(生存时间)参数会执行此操作,但事实并非如此。
TTL 参数仅指定向用户设备发送通知。例如。 phone离线2小时或不在线后仍然发送消息。
我不确定默认的 firebase 包是否有办法,但 more advanced version of it 似乎能够处理该用例:
https://notifee.app/react-native/reference/canceldisplayednotification
我认为您应该能够在后台任务中调用该方法(例如,在收到另一个(静默的)通知后)。
遗憾的是我还不能自己测试。
您可以使用像 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
});
}
})
在 iOS 上,您可以使用 badge
设置。如果将其设置为 0,它将删除所有通知。对于您的情况,您可以安排一个“清理”任务,在一定时间后触发以下请求。
{
message: {
notification: {
body: "" // has to be empty
},
android: {
notification: {
channel_id: 'some_channel'
}
},
apns: {
payload: {
aps: {
category: 'some_category',
badge: 0
}
}
},
token: device_token
}
}
不幸的是,我还没有找到 android 的类似解决方案。
我使用@react-native-firebase/messaging 在 React Native 中使用推送通知。我在后端使用 FCM,它目前在 iOS 和 Android.
上显示 OS 锁屏通知我想在特定时间后或一段时间后清除给定的通知。有没有办法做到这一点?现在,当我发送通知时,如果我不点击它,它会停留几天。我想在一个小时后或下午 4 点或其他时间取消通知。欢迎前端 and/or 后端解决方案。
我原以为“ttl”(生存时间)参数会执行此操作,但事实并非如此。
TTL 参数仅指定向用户设备发送通知。例如。 phone离线2小时或不在线后仍然发送消息。
我不确定默认的 firebase 包是否有办法,但 more advanced version of it 似乎能够处理该用例:
https://notifee.app/react-native/reference/canceldisplayednotification
我认为您应该能够在后台任务中调用该方法(例如,在收到另一个(静默的)通知后)。
遗憾的是我还不能自己测试。
您可以使用像 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
});
}
})
在 iOS 上,您可以使用 badge
设置。如果将其设置为 0,它将删除所有通知。对于您的情况,您可以安排一个“清理”任务,在一定时间后触发以下请求。
{
message: {
notification: {
body: "" // has to be empty
},
android: {
notification: {
channel_id: 'some_channel'
}
},
apns: {
payload: {
aps: {
category: 'some_category',
badge: 0
}
}
},
token: device_token
}
}
不幸的是,我还没有找到 android 的类似解决方案。