From 302bd679b24406d35cd55391c6d8fbe4122fa324 Mon Sep 17 00:00:00 2001 From: Josh Kasten Date: Tue, 14 Nov 2023 04:00:36 +0000 Subject: [PATCH 1/5] fix notification click event firing on page open Fix issue where OneSignal.Notifications.addEventListener('click', function() {}) would not fire when a notification was clicked that opens a new page. Fixed by calling fireStoredNotificationClicks when a click listener is added which checks the indexDb for any notification clicks that were not processed for a specific URL. --- src/onesignal/NotificationsNamespace.ts | 5 +++++ src/shared/helpers/EventHelper.ts | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) 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, ) { From 9f14f3ac37d0a792ff3ff83075715fe60d11790d Mon Sep 17 00:00:00 2001 From: Josh Kasten Date: Tue, 14 Nov 2023 04:18:15 +0000 Subject: [PATCH 2/5] rm duplicate notification click event in example --- express_webpack/index.html | 3 --- 1 file changed, 3 deletions(-) diff --git a/express_webpack/index.html b/express_webpack/index.html index a85e97b58..afdc6a645 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 */ From 69299ec88da1b803e9383d619a0c01bbaccb76a4 Mon Sep 17 00:00:00 2001 From: Josh Kasten Date: Tue, 14 Nov 2023 04:18:37 +0000 Subject: [PATCH 3/5] fix foregroundWillDisplay event in example --- express_webpack/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/express_webpack/index.html b/express_webpack/index.html index afdc6a645..6fd651973 100644 --- a/express_webpack/index.html +++ b/express_webpack/index.html @@ -124,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); }); }); From 3070b39a88ba787244a31aa19982c28d66f48426 Mon Sep 17 00:00:00 2001 From: Josh Kasten Date: Tue, 14 Nov 2023 04:19:15 +0000 Subject: [PATCH 4/5] logging in example to show all events to console --- express_webpack/index.html | 1 + 1 file changed, 1 insertion(+) diff --git a/express_webpack/index.html b/express_webpack/index.html index 6fd651973..e4dfa0586 100644 --- a/express_webpack/index.html +++ b/express_webpack/index.html @@ -149,6 +149,7 @@ } function showEventAlert(eventName, payload) { + console.log(`OneSignal event ${eventName} fired!`, payload); if (!showEventAlertToggleSetting) { return; } From 901f308cfca74604b6743095f36b4d69385f4d7d Mon Sep 17 00:00:00 2001 From: Josh Kasten Date: Tue, 14 Nov 2023 20:33:39 +0000 Subject: [PATCH 5/5] add test for notification click listner --- .../unit/notifications/eventListeners.test.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 __test__/unit/notifications/eventListeners.test.ts 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); + }); +});