React Native Expo app how to unregister from Notifications

Viewed 796

In my react native expo app I am successfully registering to the notification service with Notifications.getExpoPushTokenAsync() and it is working without issue, my question is, how do I stop receiving notifications? Is there a way to clear the push token on the device only?

The problem I am having is if a user logs out and a different user logs in on the same device, they will still receive the previous users notifications since the device still is still registered. Notifications.removePushTokenSubscription() seemed like it may work but it only removes the push token listener created with Notifications.addPushTokenListener() which (I think) is unrelated to this issue

My current logout function attempt looks like this:

  const logOutUser = async(notificationToken) => {
    if(notificationToken){
      var subscription = await Notifications.addPushTokenListener(notificationToken);
      await Notifications.removePushTokenSubscription(subscription);
    }
  }

Am I thinking about this problem wrong? Thanks!

1 Answers

You can't unregistered from Notifications, there is no function that does that simply because the expo token is related to your device,not your account, instead you can do this:

  1. When user login you should send the expo token to your back end and store it in an array of expoTokens, because user can have his account open in multiple phones.
  1. Store the expo token in your AsyncStorage.
  1. When the user logout you should delete the expo token from array of expoTokens array in your Backend Database.
  1. and finely delete it from AsyncStorage .

Hope it works for you!

Related