@@ -160,25 +160,39 @@ export class CoreModuleDirector {
160160 return modelStores . pushSubscriptions . models as { [ key : string ] : OSModel < SupportedSubscription > } ;
161161 }
162162
163+ private async getPushSubscriptionModelByCurrentToken ( )
164+ :Promise < OSModel < SupportedSubscription > | undefined > {
165+ logMethodCall ( "CoreModuleDirector.getPushSubscriptionModelByCurrentToken" ) ;
166+ const pushToken = await MainHelper . getCurrentPushToken ( ) ;
167+ if ( pushToken ) {
168+ return this . getSubscriptionOfTypeWithToken ( ModelName . PushSubscriptions , pushToken ) ;
169+ }
170+ return undefined ;
171+ }
172+
173+ // Browser may return a different PushToken value, use the last-known value as a fallback.
174+ // - This happens if you disable notification permissions then re-enable them.
175+ private async getPushSubscriptionModelByLastKnownToken ( )
176+ :Promise < OSModel < SupportedSubscription > | undefined > {
177+ logMethodCall ( "CoreModuleDirector.getPushSubscriptionModelByLastKnownToken" ) ;
178+ const { lastKnownPushToken } = await Database . getAppState ( ) ;
179+ if ( lastKnownPushToken ) {
180+ return this . getSubscriptionOfTypeWithToken ( ModelName . PushSubscriptions , lastKnownPushToken ) ;
181+ }
182+ return undefined ;
183+ }
184+
163185 /**
164186 * Gets the current push subscription model for the current browser.
165187 * @returns The push subscription model for the current browser, or undefined if no push subscription exists.
166188 */
167- // TO DO: make synchronous by making getting the current push token synchronous
168- public async getCurrentPushSubscriptionModel ( ) : Promise < OSModel < SupportedSubscription > | undefined > {
189+ public async getPushSubscriptionModel ( )
190+ : Promise < OSModel < SupportedSubscription > | undefined > {
169191 logMethodCall ( "CoreModuleDirector.getPushSubscriptionModel" ) ;
170- let pushToken = await MainHelper . getCurrentPushToken ( ) ;
171-
172- if ( ! pushToken ) {
173- const appState = await Database . getAppState ( ) ;
174- pushToken = appState . lastKnownPushToken ;
175-
176- if ( ! pushToken ) {
177- Log . debug ( "No push token found, returning undefined" ) ;
178- return undefined ;
179- }
180- }
181- return this . getSubscriptionOfTypeWithToken ( ModelName . PushSubscriptions , pushToken ) ;
192+ return (
193+ await this . getPushSubscriptionModelByCurrentToken ( ) ||
194+ await this . getPushSubscriptionModelByLastKnownToken ( )
195+ ) ;
182196 }
183197
184198 public getIdentityModel ( ) : OSModel < SupportedIdentity > | undefined {
@@ -199,7 +213,7 @@ export class CoreModuleDirector {
199213 logMethodCall ( "CoreModuleDirector.getAllSubscriptionsModels" ) ;
200214 const emailSubscriptions = this . getEmailSubscriptionModels ( ) ;
201215 const smsSubscriptions = this . getSmsSubscriptionModels ( ) ;
202- const pushSubscription = await this . getCurrentPushSubscriptionModel ( ) ;
216+ const pushSubscription = await this . getPushSubscriptionModel ( ) ;
203217
204218 const subscriptions = Object . values ( emailSubscriptions )
205219 . concat ( Object . values ( smsSubscriptions ) )
0 commit comments