Skip to content

Commit

Permalink
Merge pull request #841 from OneSignal/randomize-confirmed-delivery
Browse files Browse the repository at this point in the history
Randomize Confirmed Delivery to 25 second max delay
  • Loading branch information
rgomezp authored Jun 29, 2021
2 parents af98055 + 1b115b1 commit c2b07aa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/service-worker/ServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ import ServiceWorkerHelper from "../helpers/ServiceWorkerHelper";
import { NotificationReceived, NotificationClicked } from "../models/Notification";
import { cancelableTimeout } from "../helpers/sw/CancelableTimeout";
import { DeviceRecord } from '../models/DeviceRecord';
import { awaitableTimeout } from "../utils/AwaitableTimeout";

declare var self: ServiceWorkerGlobalScope & OSServiceWorkerFields;

const MAX_CONFIRMED_DELIVERY_DELAY = 25;

/**
* The main service worker script fetching and displaying notifications to users in the background even when the client
* site is not running. The worker is registered via the navigator.serviceWorker.register() call after the user first
Expand Down Expand Up @@ -354,18 +357,19 @@ export class ServiceWorker {
if (!hasRequiredParams) {
return null;
}

// JSON.stringify() does not include undefined values
// Our response will not contain those fields here which have undefined values
const postData = {
player_id : deviceId,
player_id : deviceId,
app_id : appId
};

Log.debug(`Called %csendConfirmedDelivery(${
JSON.stringify(notification, null, 4)
})`, Utils.getConsoleStyle('code'));


await awaitableTimeout(Math.floor(Math.random() * MAX_CONFIRMED_DELIVERY_DELAY * 1_000));
return await OneSignalApiBase.put(`notifications/${notification.id}/report_received`, postData);
}

Expand Down
7 changes: 6 additions & 1 deletion test/unit/context/sw/ServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { MockPushEvent } from '../../../support/mocks/service-workers/models/Moc
import { MockPushMessageData } from '../../../support/mocks/service-workers/models/MockPushMessageData';
import OneSignalUtils from '../../../../src/utils/OneSignalUtils';
import { setupFakePlayerId } from '../../../support/tester/utils';
import * as awaitableTimeout from '../../../../src/utils/AwaitableTimeout';

declare var self: MockServiceWorkerGlobalScope;

Expand All @@ -24,7 +25,6 @@ const appConfig = TestEnvironment.getFakeAppConfig();

test.beforeEach(async function() {
sandbox = sinon.sandbox.create();

await TestEnvironment.initializeForServiceWorker();

await Database.setAppConfig(appConfig);
Expand Down Expand Up @@ -291,6 +291,7 @@ test('onNotificationClicked - notification PUT Before openWindow', async t => {
}

test('sendConfirmedDelivery - notification is null - feature flag is y', async t => {
sandbox.stub(awaitableTimeout, 'awaitableTimeout');
const notificationId = null;
const notificationPutCall = mockNotificationPutCall(notificationId);
await fakeSetSubscription();
Expand All @@ -300,6 +301,7 @@ test('onNotificationClicked - notification PUT Before openWindow', async t => {
});

test('sendConfirmedDelivery - notification is valid - feature flag is y', async t => {
sandbox.stub(awaitableTimeout, 'awaitableTimeout');
const notificationId = Random.getRandomUuid();
const notificationPutCall = mockNotificationPutCall(notificationId);
await fakeSetSubscription();
Expand All @@ -309,6 +311,7 @@ test('onNotificationClicked - notification PUT Before openWindow', async t => {
});

test('sendConfirmedDelivery - notification is valid - feature flag is n', async t => {
sandbox.stub(awaitableTimeout, 'awaitableTimeout');
const notificationId = Random.getRandomUuid();
const notificationPutCall = mockNotificationPutCall(notificationId);
await fakeSetSubscription();
Expand All @@ -318,6 +321,7 @@ test('onNotificationClicked - notification PUT Before openWindow', async t => {
});

test('sendConfirmedDelivery - notification is valid - feature flag is undefined', async t => {
sandbox.stub(awaitableTimeout, 'awaitableTimeout');
const notificationId = Random.getRandomUuid();
const notificationPutCall = mockNotificationPutCall(notificationId);
await fakeSetSubscription();
Expand All @@ -327,6 +331,7 @@ test('onNotificationClicked - notification PUT Before openWindow', async t => {
});

test('sendConfirmedDelivery - notification is valid - feature flag is null', async t => {
sandbox.stub(awaitableTimeout, 'awaitableTimeout');
const notificationId = Random.getRandomUuid();
const notificationPutCall = mockNotificationPutCall(notificationId);
await fakeSetSubscription();
Expand Down

0 comments on commit c2b07aa

Please sign in to comment.