Skip to content

Commit cc7776e

Browse files
committed
add porting logic from old to new standard notifications
1 parent 9c761ea commit cc7776e

File tree

6 files changed

+40
-11
lines changed

6 files changed

+40
-11
lines changed

__test__/support/environment/TestEnvironmentHelpers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ export const setupSubModelStore = async ({
162162
token,
163163
onesignalId,
164164
});
165-
await Database.setPushId(pushModel.id);
166-
await Database.setPushToken(pushModel.token);
165+
await Database.setTokenAndId({
166+
token: pushModel.token,
167+
id: pushModel.id,
168+
});
167169
OneSignal.coreDirector.subscriptionModelStore.replaceAll(
168170
[pushModel],
169171
ModelChangeTags.NO_PROPOGATE,

src/core/types/subscription.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const SubscriptionType = {
1212
Email: 'Email',
1313
SMS: 'SMS',
1414
SafariPush: 'SafariPush',
15+
SafariLegacyPush: 'SafariLegacyPush',
1516
FirefoxPush: 'FirefoxPush',
1617
// And others but not relevant for Web SDK
1718
// macOSPush: 'macOSPush',

src/shared/helpers/EventHelper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default class EventHelper {
2727
}
2828

2929
static async checkAndTriggerSubscriptionChanged() {
30+
console.log('checkAndTriggerSubscriptionChanged');
3031
OneSignalUtils.logMethodCall('checkAndTriggerSubscriptionChanged');
3132
const context: ContextSWInterface = OneSignal.context;
3233
// isPushEnabled = subscribed && is not opted out

src/shared/helpers/SubscriptionHelper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export default class SubscriptionHelper {
3131
const rawSubscription = await context.subscriptionManager.subscribe(
3232
SubscriptionStrategyKind.ResubscribeExisting,
3333
);
34-
console.warn('rawSubscription', rawSubscription);
3534
subscription =
3635
await context.subscriptionManager.registerSubscription(
3736
rawSubscription,
@@ -57,6 +56,7 @@ export default class SubscriptionHelper {
5756
switch (type) {
5857
case SubscriptionType.ChromePush:
5958
case SubscriptionType.SafariPush:
59+
case SubscriptionType.SafariLegacyPush:
6060
case SubscriptionType.FirefoxPush:
6161
return true;
6262
default:

src/shared/managers/SubscriptionManager.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
NotificationType,
33
NotificationTypeValue,
4+
SubscriptionType,
45
} from 'src/core/types/subscription';
56
import { isCompleteSubscriptionObject } from '../../core/utils/typePredicates';
67
import 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]);

src/shared/services/Database.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,17 @@ export default class Database {
534534
static async setPushToken(pushToken: string | undefined): Promise<void> {
535535
await this.put('Options', { key: 'lastPushToken', value: pushToken });
536536
}
537+
static async setTokenAndId({
538+
token,
539+
id,
540+
}: {
541+
token?: string;
542+
id?: string;
543+
}): Promise<void> {
544+
if (token)
545+
await this.put('Options', { key: 'lastPushToken', value: token });
546+
if (id) await this.put('Options', { key: 'lastPushId', value: id });
547+
}
537548

538549
static async setIsPushEnabled(enabled: boolean): Promise<void> {
539550
return Database.singletonInstance.setIsPushEnabled(enabled);

0 commit comments

Comments
 (0)