Skip to content

Commit

Permalink
Merge pull request #1132 from OneSignal/fix/notification_click_event_…
Browse files Browse the repository at this point in the history
…firing

[Fix] notification click event not firing if it opens a new tab
  • Loading branch information
jkasten2 authored Nov 14, 2023
2 parents f8df45e + 901f308 commit c473c62
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
18 changes: 18 additions & 0 deletions __test__/unit/notifications/eventListeners.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
8 changes: 3 additions & 5 deletions express_webpack/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
});
});

Expand All @@ -152,6 +149,7 @@
}

function showEventAlert(eventName, payload) {
console.log(`OneSignal event ${eventName} fired!`, payload);
if (!showEventAlertToggleSetting) {
return;
}
Expand Down
5 changes: 5 additions & 0 deletions src/onesignal/NotificationsNamespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<K extends NotificationEventName>(
Expand Down
9 changes: 8 additions & 1 deletion src/shared/helpers/EventHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
NotificationClickEvent,
NotificationClickEventInternal,
} from '../models/NotificationEvent';
import { awaitOneSignalInitAndSupported } from '../utils/utils';

export default class EventHelper {
static onNotificationPermissionChange() {
Expand Down Expand Up @@ -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,
) {
Expand Down

0 comments on commit c473c62

Please sign in to comment.