Skip to content

Commit

Permalink
remove no longer needed first page view check
Browse files Browse the repository at this point in the history
This check is no longer needed as the session manager and model repo
handle these checks.

The original motivation to remove this is to address a macOS Safari
notification permission prompting issue where you can't await on I/O,
otherwise the prompting is flagged not done from an end-user
interaction. The isPushNotificationsEnabled() does disk I/O which we
removed in this commit.
  • Loading branch information
jkasten2 committed Jul 26, 2023
1 parent 8ed1ae9 commit b724a62
Showing 1 changed file with 2 additions and 24 deletions.
26 changes: 2 additions & 24 deletions src/shared/helpers/SubscriptionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,18 @@ import Log from '../libraries/Log';
import { ContextSWInterface } from '../models/ContextSW';
import SdkEnvironment from '../managers/SdkEnvironment';
import { EnvironmentInfo } from '../../page/models/EnvironmentInfo';
import { SessionOrigin } from '../models/Session';
import { Browser } from '../models/Browser';
import { PushDeviceRecord } from '../models/PushDeviceRecord';
import { PermissionUtils } from '../utils/PermissionUtils';
import MainHelper from './MainHelper';

export default class SubscriptionHelper {
public static async registerForPush(): Promise<Subscription | null> {
const isPushEnabled = await OneSignal.context.subscriptionManager.isPushNotificationsEnabled();
return await SubscriptionHelper.internalRegisterForPush(isPushEnabled);
return await SubscriptionHelper.internalRegisterForPush();
}

public static async internalRegisterForPush(isPushEnabled: boolean): Promise<Subscription | null> {
public static async internalRegisterForPush(): Promise<Subscription | null> {
const context: ContextSWInterface = OneSignal.context;
let subscription: Subscription | null = null;

/*
Within the same page navigation (the same session), do not register for
push if the user is already subscribed, otherwise the user will have its
session count incremented on each page refresh. However, if the user is
not subscribed, subscribe.
*/
if (isPushEnabled && !context.pageViewManager.isFirstPageView()) {
Log.debug('Not registering for push because the user is subscribed and this is not the first page view.');
Log.debug("But we want to rekindle their session.");
const deviceId = await MainHelper.getDeviceId();
if (deviceId) {
const deviceRecord: PushDeviceRecord = await MainHelper.createDeviceRecord(OneSignal.config.appId, true);
await OneSignal.context.sessionManager.upsertSession(deviceId, deviceRecord, SessionOrigin.PageRefresh);
} else {
Log.error("Should have been impossible to have push as enabled but no device id.");
}
return null;
}

switch (SdkEnvironment.getWindowEnv()) {
case WindowEnvironmentKind.Host:
Expand Down

0 comments on commit b724a62

Please sign in to comment.