简体   繁体   English

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

[英]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.

相关问题 如何取消订阅 React-Native 中的 Firestore 侦听器? - How do I unsubscribe from a Firestore listener in React-Native? 在React Native上使用Expo发送多个推送通知 - Sending multiple push notifications using Expo on React Native 如何安排反应原生 expo 的推送通知? - How to schedule push notifcations for react native expo? 如何在 React Native Expo 通知中安排“生日”通知? - How to schedule a "birthday"" notification in React Native Expo Notifications? 取消订阅事件监听器反应钩子 - Unsubscribe from event listener react hooks 如何在另一个屏幕中删除侦听器 - React Native - How to remove listener in another screen - React Native 有没有办法在新通知到达时删除已发送的通知? 反应本机推送通知 - Is there any way to remove delivered notifications upon arrival of a new one? react-native-push-notifications 从 APK 和 IPA 应用程序(React Native 和/或 Expo)中删除 Javascript Bundle - Remove Javascript Bundle from APK and IPA apps (React Native and/or Expo) 如何在 React Native 中实现 Twilio android 推送通知? - How to implement Twilio android push notifications in React Native? 反应本机 DeviceEventEmitter 取消订阅事件 - react native DeviceEventEmitter unsubscribe from event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM