Skip to content

Commit 8f2a376

Browse files
authored
fix: voip token not able to be got if user switched (#1683)
### Overview Consider this scenario where as soon as app is opened. User A logged in and then immediately logged off and then user B logs in. ``` 1. user A logs in 2. useEffect registers listeners to listen for push 3. user A logs out immediately - listeners are removed 4. apple sends push token now , but SDK is not listening as no user is logged in 5. user B logs in 6. useEffect registers listeners to listen for push 7. apple DOES NOT SEND push token now because it was sent earlier ``` ### Implementation notes If we look at the voip notification library, if we call registerVoipToken() method, the library sends the JS event for new push token with the last push token that it received. Now after this in point 7 we will receive a event in JS saying that it received a push token
1 parent 3bd3096 commit 8f2a376

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

packages/react-native-sdk/src/hooks/push/useIosVoipPushEventsSetupEffect.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,19 @@ export const useIosVoipPushEventsSetupEffect = () => {
9494
return;
9595
}
9696
const voipPushNotification = getVoipPushNotificationLib();
97+
98+
// even though we do this natively, we have to still register here again
99+
// natively this will make sure "register" event for JS is sent with the last push token
100+
// Necessary if client changed before we got the event here or user logged out and logged in again
101+
voipPushNotification.registerVoipToken();
102+
97103
const onTokenReceived = (token: string) => {
98104
const userId = client.streamClient._user?.id ?? '';
99105
if (!userId) {
106+
logger(
107+
'debug',
108+
`Skipped sending voip token to stream no user id was present - token: ${token}`
109+
);
100110
setUnsentToken(token);
101111
return;
102112
}
@@ -112,11 +122,17 @@ export const useIosVoipPushEventsSetupEffect = () => {
112122
if (!push_provider_name) {
113123
return;
114124
}
115-
logger('debug', 'Sending voip token to stream, token: ' + token);
125+
logger(
126+
'debug',
127+
`Sending voip token to stream, token: ${token} userId: ${userId}`
128+
);
116129
client
117130
.addVoipDevice(token, 'apn', push_provider_name)
118131
.then(() => {
119-
logger('debug', 'Sent voip token to stream, token: ' + token);
132+
logger(
133+
'debug',
134+
`Sent voip token to stream, token: ${token} userId: ${userId}`
135+
);
120136
setLogoutCallback(client, token, lastVoipTokenRef);
121137
lastVoipTokenRef.current = { token, userId };
122138
})
@@ -145,6 +161,11 @@ export const useIosVoipPushEventsSetupEffect = () => {
145161
}
146162
});
147163
return () => {
164+
logger(
165+
'debug',
166+
'Voip event listeners are removed for user: ' +
167+
client.streamClient._user?.id
168+
);
148169
voipPushNotification.removeEventListener('didLoadWithEvents');
149170
voipPushNotification.removeEventListener('register');
150171
};

0 commit comments

Comments
 (0)