javascript - 如何从 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
解决方案
只是一个快速的回复,因为我认为大多数答案或评论至少都错过了你的观点。您可以通过执行此操作停止应用程序侦听通知。_notificationSubscription.remove(); 但是我假设您正在寻找一种取消订阅用户所有未来通知的方法(您通常会在设置菜单或选项菜单中提供什么,因此如果他们要求您的应用不向他们发送通知,您就不会向用户发送垃圾邮件),这个当然可以在大多数手机中通过手机实际设置完成,因此您的应用程序需要遵守此请求,或者像谷歌和苹果这样的应用程序商店将阻止您的应用程序被允许向任何用户发送通知,无论订阅状态如何。
目前处理这个问题的唯一方法是远程通知的服务器端,本地的你可以使用 asyncstorage 来存储设置并在推送任何定时通知之前引用它。您将需要设置对通知服务器的调用以从数据库中删除通知令牌,从而防止您的系统向该设备发送任何未来的通知。
推荐阅读
- java - 配置 SpringBootApplication 以从属性读取时遇到问题
- python - 双正则表达式搜索循环产生问题
- swift - Swift 中的保护声明
- python - 将 Simple Imputer 与 Pandas 数据框一起使用?
- angular - 指令正在更改父禁用属性
- pandas - Pandas - 创建最大键值对的 DataFrame
- vuejs2 - 如何在 ckeditor 5 vue 中使用代码示例?
- wordpress - 图片上传时缺少临时文件夹问题
- python - 条件循环橄榄球抽签的列表操作
- javascript - 保持填充 DOM 的顺序与对象顺序相同