简体   繁体   中英

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.

Here is what I want to implement: https://docs.expo.io/versions/latest/sdk/notifications/#eventsubscription

Notifications.addListener(listener) EventSubscription and then remove() (function) -- Unsubscribe the listener from future notifications. Notification

Here is the componentDidMount method:

  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(); 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM