Reputation: 1343
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.
Upvotes: 5
Views: 15927
Reputation: 877
Simply add below given code in your logout function -
for react-native-firebase <= v5.x.x
firebase.messaging().deleteToken()
for > 5.x.x
or using @react-native-firebase/messaging
import messaging from '@react-native-firebase/messaging';
messaging().deleteToken()
Upvotes: 4
Reputation: 374
Recently I try to use FCM too, and found the issue usually due to the function comes from, i.e. where to import the functions.
I think you already have installed firebase
package, call the function below will trigger delete token on firebase.
import { getMessaging, deleteToken } from 'firebase/messaging';
const messaging = getMessaging(firebaseApp);
deleteToken(messaging);
Upvotes: 0
Reputation: 11
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')
}
};
Upvotes: 0
Reputation: 663
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();
Upvotes: 0