11import {
22 NotificationType ,
33 NotificationTypeValue ,
4+ SubscriptionType ,
45} from 'src/core/types/subscription' ;
56import { isCompleteSubscriptionObject } from '../../core/utils/typePredicates' ;
67import UserDirector from '../../onesignal/UserDirector' ;
@@ -138,7 +139,6 @@ export class SubscriptionManager {
138139
139140 rawPushSubscription =
140141 await this . subscribeFcmFromPage ( subscriptionStrategy ) ;
141- console . warn ( 'rawPushSubscription 2' , rawPushSubscription ) ;
142142 await this . _updatePushSubscriptionModelWithRawSubscription (
143143 rawPushSubscription ,
144144 ) ;
@@ -153,24 +153,38 @@ export class SubscriptionManager {
153153 private async _updatePushSubscriptionModelWithRawSubscription (
154154 rawPushSubscription : RawPushSubscription ,
155155 ) {
156+ console . log ( 'updatePushSubscriptionModelWithRawSubscription' , {
157+ rawPushSubscription,
158+ } ) ;
156159 const pushModel = await OneSignal . coreDirector . getPushSubscriptionModel ( ) ;
160+ console . log ( 'pushModel' , { pushModel } ) ;
161+
157162 // EventHelper checkAndTriggerSubscriptionChanged is called before this function when permission is granted and so
158163 // it will save the push token/id to the database so we don't need to save the token afer generating
159164 if ( ! pushModel ) {
160165 OneSignal . coreDirector . generatePushSubscriptionModel ( rawPushSubscription ) ;
161166 return UserDirector . createUserOnServer ( ) ;
162-
163- // Bug w/ v160400 release where isCreatingUser was improperly set and never reset
164- // so a check if pushModel id is needed to recreate the user
165- }
166- if ( ! pushModel . id ) {
167- return UserDirector . createUserOnServer ( ) ;
168167 }
169168
170- // resubscribing. update existing push subscription model
169+ // Bug w/ v160400 release where isCreatingUser was improperly set and never reset
170+ // so a check if pushModel id is needed to recreate the user
171+ if ( ! pushModel . id ) return UserDirector . createUserOnServer ( ) ;
172+
171173 const serializedSubscriptionRecord = new FuturePushSubscriptionRecord (
172174 rawPushSubscription ,
173175 ) . serialize ( ) ;
176+
177+ // for legacy safari push, switch to new format (e.g. old token 'ebsm3...' to -> https://web.push.apple.com/... with populated web_auth and web_p256)
178+ if ( pushModel . type === SubscriptionType . SafariLegacyPush ) {
179+ if ( ! window . Notification ) return ;
180+ await Database . setTokenAndId ( {
181+ token : serializedSubscriptionRecord . token ,
182+ id : pushModel . id ,
183+ } ) ;
184+ pushModel . setProperty ( 'type' , SubscriptionType . SafariPush ) ;
185+ }
186+
187+ // update existing push subscription model
174188 for ( const key in serializedSubscriptionRecord ) {
175189 const modelKey = key as keyof typeof serializedSubscriptionRecord ;
176190 pushModel . setProperty ( modelKey , serializedSubscriptionRecord [ modelKey ] ) ;
0 commit comments