首页 > 解决方案 > 如何从 React Native Expo 推送通知中取消订阅(删除监听器)

问题描述

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

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

Notifications.addListener(listener) EventSubscription 然后 remove() (function) -- 取消订阅监听器未来的通知。通知

这是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);

}

一些帮助会非常棒

最好的问候 Musayyab

标签: javascriptreact-nativepush-notificationlistenerexpo

解决方案


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

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


推荐阅读