[英]How to unsubscribe(remove a listener) from React Native Expo push notifications
I want to give users an option to be able to unsubscribe from the notifications.我想让用户可以选择取消订阅通知。 I have already gone through the Expo documentation but I am still not sure how to implement it in my code.
我已经阅读了 Expo 文档,但我仍然不确定如何在我的代码中实现它。
Here is what I want to implement: https://docs.expo.io/versions/latest/sdk/notifications/#eventsubscription这是我想要实现的: https : //docs.expo.io/versions/latest/sdk/notifications/#eventsubscription
Notifications.addListener(listener) EventSubscription
and then remove() (function) -- Unsubscribe the listener from future notifications. Notifications.addListener(listener) EventSubscription
然后 remove() (函数)——从未来的通知中取消订阅监听器。 Notification通知
Here is the componentDidMount
method:这是
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);
} }
Some Help would be very great一些帮助会非常好
Best Regards Musayyab最好的问候穆萨亚布
Just a quick reply as I think most answers or comments at least are missing your point.只是快速回复,因为我认为大多数答案或评论至少都没有抓住你的观点。 You can stop the app listening for notification by doing this._notificationSubscription.remove();
您可以通过执行 this._notificationSubscription.remove(); 来停止应用程序监听通知。 But what I assume your looking for is a method to unsubscribe the user from all future notifications (what you would generally offer in a settings menu or options menu so you dont spam the user if they request your app to not send them notifications), this can of course be done in most phones from the phones actual settings and as such you app needs to obey this request or app stores like google and apple will stop your app from being allowed to send notifications at all to any user regardless of subscription state.
但是我假设您要寻找的是一种从所有未来通知中取消订阅用户的方法(您通常会在设置菜单或选项菜单中提供什么,这样如果他们要求您的应用程序不向他们发送通知,您就不会向用户发送垃圾邮件),这当然可以通过手机的实际设置在大多数手机中完成,因此您的应用程序需要遵守此请求,或者谷歌和苹果等应用程序商店将完全禁止您的应用程序向任何用户发送通知,无论订阅状态如何。
Currently the only way to handle this is server side for remote notifications, local ones you could just use asyncstorage to store a setting and reference this before pushing any timed notifications.目前处理这个问题的唯一方法是远程通知的服务器端,本地通知你可以只使用 asyncstorage 来存储设置并在推送任何定时通知之前引用它。 You will want to setup a call to your notifications server to remove the notification token from your database thus preventing your system from sending that device any future notifications.
您将需要设置对通知服务器的调用以从数据库中删除通知令牌,从而防止您的系统向该设备发送任何未来通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.