@@ -80,6 +80,8 @@ export default class ControllerInterface {
80
80
81
81
const swReg = await this . getSWRegistration_ ( ) ;
82
82
const publicVapidKey = await this . getPublicVapidKey_ ( ) ;
83
+ // If a PushSubscription exists it's returned, otherwise a new subscription
84
+ // is generated and returned.
83
85
const pushSubscription = await this . getPushSubscription (
84
86
swReg ,
85
87
publicVapidKey
@@ -134,8 +136,10 @@ export default class ControllerInterface {
134
136
}
135
137
136
138
// If the token is no longer valid (for example if the VAPID details
137
- // have changed), delete the existing token, and create a new one.
138
- await this . deleteToken ( tokenDetails [ 'fcmToken' ] ) ;
139
+ // have changed), delete the existing token from the FCM client and server
140
+ // database. No need to unsubscribe from the Service Worker as we have a
141
+ // good push subscription that we'd like to use in getNewToken.
142
+ await this . deleteTokenFromDB ( tokenDetails [ 'fcmToken' ] ) ;
139
143
return this . getNewToken ( swReg , pushSubscription , publicVapidKey ) ;
140
144
}
141
145
@@ -228,29 +232,33 @@ export default class ControllerInterface {
228
232
* whether or not the unsubscribe request was processed successfully.
229
233
* @export
230
234
*/
231
- deleteToken ( token : string ) : Promise < Boolean > {
232
- return this . tokenDetailsModel_
233
- . deleteToken ( token )
234
- . then ( details => {
235
- return this . iidModel_ . deleteToken (
236
- details [ 'fcmSenderId' ] ,
237
- details [ 'fcmToken' ] ,
238
- details [ 'fcmPushSet' ]
239
- ) ;
240
- } )
241
- . then ( ( ) => {
242
- return this . getSWRegistration_ ( )
243
- . then ( registration => {
244
- if ( registration ) {
245
- return registration . pushManager . getSubscription ( ) ;
246
- }
247
- } )
248
- . then ( subscription => {
249
- if ( subscription ) {
250
- return subscription . unsubscribe ( ) ;
251
- }
252
- } ) ;
253
- } ) ;
235
+ async deleteToken ( token : string ) : Promise < Boolean > {
236
+ // Delete the token details from the database.
237
+ await this . deleteTokenFromDB ( token ) ;
238
+ // Unsubscribe from the SW.
239
+ const registration = await this . getSWRegistration_ ( ) ;
240
+ if ( registration ) {
241
+ const pushSubscription = await registration . pushManager . getSubscription ( ) ;
242
+ if ( pushSubscription ) {
243
+ return pushSubscription . unsubscribe ( ) ;
244
+ }
245
+ }
246
+ // If there's no SW, consider it a success.
247
+ return true ;
248
+ }
249
+
250
+ /**
251
+ * This method will delete the token from the client database, and make a
252
+ * call to FCM to remove it from the server DB. Does not temper with the
253
+ * push subscription.
254
+ */
255
+ private async deleteTokenFromDB ( token : string ) : Promise < void > {
256
+ const details = await this . tokenDetailsModel_ . deleteToken ( token ) ;
257
+ await this . iidModel_ . deleteToken (
258
+ details [ 'fcmSenderId' ] ,
259
+ details [ 'fcmToken' ] ,
260
+ details [ 'fcmPushSet' ]
261
+ ) ;
254
262
}
255
263
256
264
getSWRegistration_ ( ) : Promise < ServiceWorkerRegistration > {
@@ -261,14 +269,6 @@ export default class ControllerInterface {
261
269
throw this . errorFactory_ . create ( Errors . codes . SHOULD_BE_INHERITED ) ;
262
270
}
263
271
264
- //
265
- // The following methods should only be available in the window.
266
- //
267
-
268
- requestPermission ( ) {
269
- throw this . errorFactory_ . create ( Errors . codes . AVAILABLE_IN_WINDOW ) ;
270
- }
271
-
272
272
/**
273
273
* Gets a PushSubscription for the current user.
274
274
*/
@@ -288,6 +288,14 @@ export default class ControllerInterface {
288
288
} ) ;
289
289
}
290
290
291
+ //
292
+ // The following methods should only be available in the window.
293
+ //
294
+
295
+ requestPermission ( ) {
296
+ throw this . errorFactory_ . create ( Errors . codes . AVAILABLE_IN_WINDOW ) ;
297
+ }
298
+
291
299
/**
292
300
* @export
293
301
* @param {!ServiceWorkerRegistration } registration
0 commit comments