diff --git a/__test__/unit/notifications/eventListeners.test.ts b/__test__/unit/notifications/eventListeners.test.ts new file mode 100644 index 000000000..5c86e7ecd --- /dev/null +++ b/__test__/unit/notifications/eventListeners.test.ts @@ -0,0 +1,18 @@ +import { TestEnvironment } from '../../support/environment/TestEnvironment'; +import EventHelper from '../../../src/shared/helpers/EventHelper'; + +describe('Notification Events', () => { + beforeEach(async () => { + TestEnvironment.initialize(); + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + test('Adding click listener fires internal EventHelper', async () => { + const stub = test.stub(EventHelper, 'fireStoredNotificationClicks'); + OneSignal.Notifications.addEventListener('click', null); + expect(stub).toHaveBeenCalledTimes(1); + }); +}); diff --git a/express_webpack/index.html b/express_webpack/index.html index a85e97b58..e4dfa0586 100644 --- a/express_webpack/index.html +++ b/express_webpack/index.html @@ -102,9 +102,6 @@ onesignal.User.PushSubscription.addEventListener('subscriptionChange', function (event) { showEventAlert('subscriptionChange', { event }); }); - onesignal.Notifications.addEventListener('click', event => { - showEventAlert('click', { event }); - }); }); /* E V E N T L I S T E N E R S */ @@ -127,8 +124,8 @@ }); OneSignalDeferred.push(function(onesignal) { - onesignal.Notifications.addEventListener('willDisplay', function (event) { - showEventAlert('willDisplay', { event }); + onesignal.Notifications.addEventListener('foregroundWillDisplay', function (event) { + showEventAlert('foregroundWillDisplay', event); }); }); @@ -152,6 +149,7 @@ } function showEventAlert(eventName, payload) { + console.log(`OneSignal event ${eventName} fired!`, payload); if (!showEventAlertToggleSetting) { return; } diff --git a/src/onesignal/NotificationsNamespace.ts b/src/onesignal/NotificationsNamespace.ts index d8ba4518f..d38dae08b 100644 --- a/src/onesignal/NotificationsNamespace.ts +++ b/src/onesignal/NotificationsNamespace.ts @@ -13,6 +13,7 @@ import { EventListenerBase } from '../page/userModel/EventListenerBase'; import { NotificationEventName } from '../page/models/NotificationEventName'; import { NotificationPermission } from '../shared/models/NotificationPermission'; import NotificationEventTypeMap from '../page/models/NotificationEventTypeMap'; +import EventHelper from '../shared/helpers/EventHelper'; export default class NotificationsNamespace extends EventListenerBase { private _permission: boolean; @@ -146,6 +147,10 @@ export default class NotificationsNamespace extends EventListenerBase { listener: (obj: NotificationEventTypeMap[K]) => void, ): void { OneSignal.emitter.on(event, listener); + + if (event === 'click') { + EventHelper.fireStoredNotificationClicks(); + } } removeEventListener( diff --git a/src/shared/helpers/EventHelper.ts b/src/shared/helpers/EventHelper.ts index ad359bd86..b94a65708 100755 --- a/src/shared/helpers/EventHelper.ts +++ b/src/shared/helpers/EventHelper.ts @@ -14,6 +14,7 @@ import { NotificationClickEvent, NotificationClickEventInternal, } from '../models/NotificationEvent'; +import { awaitOneSignalInitAndSupported } from '../utils/utils'; export default class EventHelper { static onNotificationPermissionChange() { @@ -243,7 +244,13 @@ export default class EventHelper { * This method is fired for both HTTPS and HTTP sites, so for HTTP sites, the host URL needs to be used, not the * subdomain.onesignal.com URL. */ - static async fireStoredNotificationClicks(url: string = document.URL) { + static async fireStoredNotificationClicks() { + await awaitOneSignalInitAndSupported(); + const url = + OneSignal.config.pageUrl || + OneSignal.config.userConfig.pageUrl || + document.URL; + async function fireEventWithNotification( selectedEvent: NotificationClickEventInternal, ) {