Skip to content

Commit eb3afb4

Browse files
authored
fix: added workaround for possible multiple createDevice calls on remounting (#1532)
small workaround where SDK wont send same tokens again
1 parent 7be61d2 commit eb3afb4

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
} from '../../utils/push/rxSubjects';
2020
import { RxUtils, getLogger } from '@stream-io/video-client';
2121

22-
let lastVoipToken: string | undefined = '';
22+
let lastVoipToken = { token: '', userId: '' };
2323

2424
/**
2525
* This hook is used to do the initial setup of listeners
@@ -32,23 +32,13 @@ export const useIosVoipPushEventsSetupEffect = () => {
3232
if (Platform.OS !== 'ios' || !pushConfig || !client) {
3333
return;
3434
}
35-
if (lastVoipToken) {
36-
// send token to stream (userId might have switched on the same device)
37-
const push_provider_name = pushConfig.ios.pushProviderName;
38-
if (!push_provider_name) {
39-
return;
40-
}
41-
client
42-
.addVoipDevice(lastVoipToken, 'apn', push_provider_name)
43-
.catch((err) => {
44-
const logger = getLogger(['useIosVoipPushEventsSetupEffect']);
45-
logger('warn', 'Failed to send lastVoipToken to stream', err);
46-
});
47-
}
4835
const voipPushNotification = getVoipPushNotificationLib();
4936
const onTokenReceived = (token: string) => {
50-
// send token to stream
51-
lastVoipToken = token;
37+
const userId = client.streamClient._user?.id ?? '';
38+
if (lastVoipToken.token === token && lastVoipToken.userId === userId) {
39+
return;
40+
}
41+
lastVoipToken = { token, userId };
5242
const push_provider_name = pushConfig.ios.pushProviderName;
5343
if (!push_provider_name) {
5444
return;
@@ -59,6 +49,7 @@ export const useIosVoipPushEventsSetupEffect = () => {
5949
});
6050
// set the logout callback
6151
setPushLogoutCallback(async () => {
52+
lastVoipToken = { token: '', userId: '' };
6253
try {
6354
await client.removeDevice(token);
6455
} catch (err) {

packages/react-native-sdk/src/utils/push/android.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type onBackgroundEventFunctionParams = Parameters<
4646

4747
type Event = Parameters<onBackgroundEventFunctionParams>[0];
4848

49+
let lastFirebaseToken = { token: '', userId: '' };
50+
4951
// EventType = NotifeeLib['EventType'];
5052

5153
/** Setup Firebase push message handler **/
@@ -139,7 +141,16 @@ export async function initAndroidPushToken(
139141
return;
140142
}
141143
const setDeviceToken = async (token: string) => {
144+
const userId = client.streamClient._user?.id ?? '';
145+
if (
146+
lastFirebaseToken.token === token &&
147+
lastFirebaseToken.userId === userId
148+
) {
149+
return;
150+
}
151+
lastFirebaseToken = { token, userId };
142152
setPushLogoutCallback(async () => {
153+
lastFirebaseToken = { token: '', userId: '' };
143154
try {
144155
await client.removeDevice(token);
145156
} catch (err) {

packages/react-native-sdk/src/utils/push/ios.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type StreamPayload =
3232
}
3333
| undefined;
3434

35+
let lastApnToken = { token: '', userId: '' };
36+
3537
function processNonRingingNotificationStreamPayload(
3638
streamPayload: StreamPayload
3739
) {
@@ -142,7 +144,13 @@ export async function initIosNonVoipToken(
142144
return;
143145
}
144146
const setDeviceToken = async (token: string) => {
147+
const userId = client.streamClient._user?.id ?? '';
148+
if (lastApnToken.token === token && lastApnToken.userId === userId) {
149+
return;
150+
}
151+
lastApnToken = { token, userId };
145152
setPushLogoutCallback(async () => {
153+
lastApnToken = { token: '', userId: '' };
146154
try {
147155
await client.removeDevice(token);
148156
} catch (err) {

0 commit comments

Comments
 (0)