Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] notification click event not firing if it opens a new tab #1132

Merged
merged 5 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading