3

I use React Native FCM for messaging, and when the user logs out the application I want to delete the FCM token so that the user doesn't get Notified again.

Below is my code for logout.

_signOutAsync = async () => {
    this.logoutEvent()
    API.post('customer/auth/logout', null, {
      headers: {
        Authorization:
          'Bearer ' + (await AsyncStorage.getItem(Config.ACCESS_TOKEN))
      }
    }).then((response) => {
      console.log(response)
    })
    this.clearData()
  }

Thanks.

zidniryi
  • 1,023
  • 2
  • 13
  • 26

3 Answers3

3

Simply add below given code in your logout function.

firebase.messaging().deleteToken()
Kailash
  • 276
  • 1
  • 15
  • 2
    this doesn't work as even after deleting the token it will generate the same FCM token again. – Deepak Kumar Oct 30 '20 at 08:27
  • If you face this issue, then refer to my solution. – Shyam Jan 25 '21 at 22:41
  • this didn't work for me, after deleteToken, logging in and and calling getToken would not log you back in. Instead, using unregisterDeviceForRemoteMessages / registerDeviceForRemoteMessages worked. See https://rnfirebase.io/reference/messaging#registerDeviceForRemoteMessages – Ghan Aug 06 '21 at 19:02
0
await firebase.messaging().deleteToken();

is the solution.

BUT, if you get the same token even after deleting, install the npm package react-native-restart, and do the below step to get a new token

messaging()
            .deleteToken(undefined,'*')
            .then(() => {
                RNRestart.Restart();
Shyam
  • 503
  • 5
  • 9
0

Install the npm package react-native-restart and Simply call like this :-

const logoutAndClearAsyncStorage = async () => {
  try {
    await AsyncStorage.clear()
    await firebase.messaging().deleteToken().then(() => {
      RNRestart.Restart()
      navigation.replace('LoginStack', { screen: 'WelcomeScreen' });
    })
  } catch (error) {
    console.log(error, 'logout')
  }
};

I hope it helps.

Tyler2P
  • 2,182
  • 12
  • 17
  • 28