Skip to content

Commit

Permalink
Merge pull request #1108 from OneSignal/fix/welcome_notification
Browse files Browse the repository at this point in the history
[Fix] Welcome Notification
  • Loading branch information
jkasten2 authored Sep 29, 2023
2 parents 95b2670 + 03a65de commit beffd4f
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/shared/helpers/EventHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ export default class EventHelper {
static async _onSubscriptionChanged(
change: SubscriptionChangeEvent | undefined,
) {
// EventHelper.onSubscriptionChanged_showWelcomeNotification(change?.current.enabled);
EventHelper.onSubscriptionChanged_showWelcomeNotification(
change?.current?.optedIn,
change?.current?.id,
);
EventHelper.onSubscriptionChanged_sendCategorySlidedownTags(
change?.current?.optedIn,
);
Expand All @@ -107,9 +110,14 @@ export default class EventHelper {
}
}

private static sendingOrSentWelcomeNotification = false;
/**
* NOTE: This uses the OneSignal REST API POST /notifications with
* include_player_ids. This field will be dropped by 2025 so a
* replacement will needed by then.
*/
private static async onSubscriptionChanged_showWelcomeNotification(
isSubscribed: boolean | undefined,
pushSubscriptionId: string | undefined | null,
) {
if (OneSignal.__doNotShowWelcomeNotification) {
Log.debug(
Expand All @@ -131,14 +139,10 @@ export default class EventHelper {
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) {
if (!pushSubscriptionId) {
return;
}
EventHelper.sendingOrSentWelcomeNotification = true;

const { deviceId } = await Database.getSubscription();
const { appId } = await Database.getAppConfig();
let title =
welcome_notification_opts !== undefined &&
Expand Down Expand Up @@ -167,7 +171,7 @@ export default class EventHelper {
Log.debug('Sending welcome notification.');
OneSignalApiShared.sendNotification(
appId,
[deviceId],
[pushSubscriptionId],
{ en: title },
{ en: message },
url,
Expand Down

0 comments on commit beffd4f

Please sign in to comment.