如何取消订阅(删除侦听器)React Native Expo 推送通知

Mus*_*eed 3 javascript listener push-notification react-native expo

我想为用户提供一个能够取消订阅通知的选项。我已经阅读了 Expo 文档,但我仍然不确定如何在我的代码中实现它。

这是我想要实现的:https ://docs.expo.io/versions/latest/sdk/notifications/#eventsubscription

Notifications.addListener(listener) EventSubscription 然后remove()(函数)——取消监听器对未来通知的订阅。通知

方法如下componentDidMount

  componentDidMount() {
registerForPushNotificationsAsync();

// Handle notifications that are received or selected while the app
// is open. If the app was closed and then opened by tapping the
// notification (rather than just tapping the app icon to open it),
// this function will fire on the next tick after the app starts
// with the notification data.
this._notificationSubscription = Notifications.addListener(this._handleNotification);
Run Code Online (Sandbox Code Playgroud)

}

一些帮助会非常好

最好的问候穆萨亚布

IDE*_*DED 5

只是快速回复,因为我认为大多数答案或评论至少都没有抓住你的要点。您可以通过执行此操作来停止应用程序监听通知。_notificationSubscription.remove(); 但我假设您正在寻找一种取消用户订阅所有未来通知的方法(您通常会在设置菜单或选项菜单中提供这种方法,这样如果用户请求您的应用程序不向他们发送通知,您就不会向用户发送垃圾邮件),这当然,可以在大多数手机中通过手机的实际设置来完成,因此您的应用程序需要遵守此请求,否则谷歌和苹果等应用程序商店将阻止您的应用程序被允许向任何用户发送通知,无论订阅状态如何。

目前处理此问题的唯一方法是服务器端的远程通知,本地通知您可以使用异步存储来存储设置并在推送任何定时通知之前引用它。您将需要设置对通知服务器的调用,以从数据库中删除通知令牌,从而防止您的系统将来向该设备发送任何通知。