From 4f28e61632846645221ff48005b1944e4b19093d Mon Sep 17 00:00:00 2001 From: Mattermost Build Date: Mon, 11 Jan 2021 19:39:51 +0100 Subject: [PATCH] MM-31873 fix race condition that prevented the device id to be registered on Android (#5104) (#5108) (cherry picked from commit 0d590c742fbf265d5cb3e1c9721e64c375aab2fc) Co-authored-by: Elias Nahum --- app/init/push_notifications.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/init/push_notifications.ts b/app/init/push_notifications.ts index 0d8fa27b5..11a5ff568 100644 --- a/app/init/push_notifications.ts +++ b/app/init/push_notifications.ts @@ -202,7 +202,6 @@ class PushNotifications { onRemoteNotificationsRegistered = (event: Registered) => { if (!this.configured) { - this.configured = true; const {deviceToken} = event; let prefix; @@ -217,11 +216,22 @@ class PushNotifications { EphemeralStore.deviceToken = `${prefix}:${deviceToken}`; if (Store.redux) { + this.configured = true; const dispatch = Store.redux.dispatch as DispatchFunc; waitForHydration(Store.redux, () => { this.requestNotificationReplyPermissions(); dispatch(setDeviceToken(EphemeralStore.deviceToken)); }); + } else { + // The redux store is not ready, so we retry it to set the + // token to prevent sessions being registered without a device id + // This code may be executed on fast devices cause the token registration + // is faster than the redux store configuration. + // Note: Should not be needed once WDB is implemented + const remoteTimeout = setTimeout(() => { + clearTimeout(remoteTimeout); + this.onRemoteNotificationsRegistered(event); + }, 200); } } };