Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] iOS double welcome notification #1051

Merged
merged 3 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 79 additions & 85 deletions src/helpers/EventHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,34 @@ import LocalStorage from '../utils/LocalStorage';
import { CustomLinkManager } from '../managers/CustomLinkManager';

export default class EventHelper {
static _mutexPromise: Promise<void> = Promise.resolve();
static _mutexLocked = false;

static async onNotificationPermissionChange() {
await EventHelper.checkAndTriggerSubscriptionChanged();
static onNotificationPermissionChange() {
EventHelper.checkAndTriggerSubscriptionChanged();
}

static async onInternalSubscriptionSet(optedOut: boolean) {
LimitStore.put('subscription.optedOut', optedOut);
}

static async checkAndTriggerSubscriptionChanged() {
if (EventHelper._mutexLocked) {
await EventHelper._mutexPromise;
}

EventHelper._mutexLocked = true;
// eslint-disable-next-line no-async-promise-executor
EventHelper._mutexPromise = new Promise(async (resolve, reject) => {
try {
OneSignalUtils.logMethodCall('checkAndTriggerSubscriptionChanged');
const context: ContextSWInterface = OneSignal.context;
const subscriptionState = await context.subscriptionManager.getSubscriptionState();
const isPushEnabled = await OneSignal.privateIsPushNotificationsEnabled();
const appState = await Database.getAppState();
const { lastKnownPushEnabled } = appState;
const didStateChange = (
lastKnownPushEnabled === null ||
isPushEnabled !== lastKnownPushEnabled
);
if (!didStateChange) return;
Log.info(
`The user's subscription state changed from ` +
`${lastKnownPushEnabled === null ? '(not stored)' : lastKnownPushEnabled} ⟶ ${subscriptionState.subscribed}`
);
LocalStorage.setIsPushNotificationsEnabled(isPushEnabled);
appState.lastKnownPushEnabled = isPushEnabled;
await Database.setAppState(appState);
EventHelper.triggerSubscriptionChanged(isPushEnabled);
EventHelper._mutexLocked = false;
resolve();
} catch (e) {
EventHelper._mutexLocked = false;
reject(`checkAndTriggerSubscriptionChanged error: ${e}`);
}
});
OneSignalUtils.logMethodCall('checkAndTriggerSubscriptionChanged');
const context: ContextSWInterface = OneSignal.context;
const subscriptionState = await context.subscriptionManager.getSubscriptionState();
const isPushEnabled = await OneSignal.privateIsPushNotificationsEnabled();
const appState = await Database.getAppState();
const { lastKnownPushEnabled } = appState;
const didStateChange = (
lastKnownPushEnabled === null ||
isPushEnabled !== lastKnownPushEnabled
);
if (!didStateChange) return;
Log.info(
`The user's subscription state changed from ` +
`${lastKnownPushEnabled === null ? '(not stored)' : lastKnownPushEnabled} ⟶ ${subscriptionState.subscribed}`
);
LocalStorage.setIsPushNotificationsEnabled(isPushEnabled);
appState.lastKnownPushEnabled = isPushEnabled;
await Database.setAppState(appState);
EventHelper.triggerSubscriptionChanged(isPushEnabled);
}

static async _onSubscriptionChanged(newSubscriptionState: boolean | undefined) {
Expand All @@ -77,58 +59,70 @@ export default class EventHelper {
}
}

private static sendingOrSentWelcomeNotification = false;
private static async onSubscriptionChanged_showWelcomeNotification(isSubscribed: boolean | undefined) {
if (OneSignal.__doNotShowWelcomeNotification) {
Log.debug('Not showing welcome notification because user has previously subscribed.');
return;
}
if (isSubscribed === true) {
const { deviceId } = await Database.getSubscription();
const { appId } = await Database.getAppConfig();

const welcome_notification_opts = OneSignal.config.userConfig.welcomeNotification;
const welcome_notification_disabled =
welcome_notification_opts !== undefined && welcome_notification_opts['disable'] === true;
let title =
welcome_notification_opts !== undefined &&
welcome_notification_opts['title'] !== undefined &&
welcome_notification_opts['title'] !== null
? welcome_notification_opts['title']
: '';
let message =
welcome_notification_opts !== undefined &&
welcome_notification_opts['message'] !== undefined &&
welcome_notification_opts['message'] !== null &&
welcome_notification_opts['message'].length > 0
? welcome_notification_opts['message']
: 'Thanks for subscribing!';
const unopenableWelcomeNotificationUrl = new URL(location.href).origin + '?_osp=do_not_open';
const url =
welcome_notification_opts && welcome_notification_opts['url'] && welcome_notification_opts['url'].length > 0
? welcome_notification_opts['url']
: unopenableWelcomeNotificationUrl;
title = BrowserUtils.decodeHtmlEntities(title);
message = BrowserUtils.decodeHtmlEntities(message);

if (!welcome_notification_disabled) {
Log.debug('Sending welcome notification.');
OneSignalApiShared.sendNotification(
appId,
[deviceId],
{ en: title },
{ en: message },
url,
null,
{ __isOneSignalWelcomeNotification: true },
undefined
);
Event.trigger(OneSignal.EVENTS.WELCOME_NOTIFICATION_SENT, {
title: title,
message: message,
url: url
});
}
const welcome_notification_opts = OneSignal.config.userConfig.welcomeNotification;
const welcome_notification_disabled =
welcome_notification_opts !== undefined && welcome_notification_opts['disable'] === true;

if (welcome_notification_disabled) {
return;
}

if (isSubscribed !== true) {
return;
}

// Workaround only for this v15 branch; There are race conditions in the SDK
// that result in the onSubscriptionChanged firing more than once sometimes.
if (EventHelper.sendingOrSentWelcomeNotification) {
return;
}
EventHelper.sendingOrSentWelcomeNotification = true;

const { deviceId } = await Database.getSubscription();
const { appId } = await Database.getAppConfig();
let title =
welcome_notification_opts !== undefined &&
welcome_notification_opts['title'] !== undefined &&
welcome_notification_opts['title'] !== null
? welcome_notification_opts['title']
: '';
let message =
welcome_notification_opts !== undefined &&
welcome_notification_opts['message'] !== undefined &&
welcome_notification_opts['message'] !== null &&
welcome_notification_opts['message'].length > 0
? welcome_notification_opts['message']
: 'Thanks for subscribing!';
const unopenableWelcomeNotificationUrl = new URL(location.href).origin + '?_osp=do_not_open';
const url =
welcome_notification_opts && welcome_notification_opts['url'] && welcome_notification_opts['url'].length > 0
? welcome_notification_opts['url']
: unopenableWelcomeNotificationUrl;
title = BrowserUtils.decodeHtmlEntities(title);
message = BrowserUtils.decodeHtmlEntities(message);

Log.debug('Sending welcome notification.');
OneSignalApiShared.sendNotification(
appId,
[deviceId],
{ en: title },
{ en: message },
url,
null,
{ __isOneSignalWelcomeNotification: true },
undefined
);
Event.trigger(OneSignal.EVENTS.WELCOME_NOTIFICATION_SENT, {
title: title,
message: message,
url: url
});
}

private static async onSubscriptionChanged_evaluateNotifyButtonDisplayPredicate() {
Expand Down
3 changes: 0 additions & 3 deletions test/unit/public-sdk-apis/onSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
import { createSubscription } from "../../support/tester/utils";
import EventsTestHelper from '../../support/tester/EventsTestHelper';
import { DelayedPromptType } from '../../../src/models/Prompts';
import EventHelper from "../../../src/helpers/EventHelper";


const sinonSandbox: SinonSandbox = sinon.sandbox.create();
Expand All @@ -31,8 +30,6 @@ test.afterEach(function (_t: ExecutionContext) {
OneSignal._initCalled = false;
OneSignal.__initAlreadyCalled = false;
OneSignal._sessionInitAlreadyRunning = false;
EventHelper._mutexPromise = Promise.resolve();
EventHelper._mutexLocked = false;
});

/**
Expand Down