Skip to content

Commit ce2dddb

Browse files
committed
fix(demo): persist notification clicks to localStorage
1 parent afed1d4 commit ce2dddb

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

examples/demo_pods/src/hooks/useOneSignal.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,22 @@ export function useOneSignal(): UseOneSignalReturn {
175175

176176
const handleNotificationClick = (e: NotificationClickEvent) => {
177177
console.log(`Notification click: ${e.notification.title ?? ''}`);
178+
// Persist to localStorage so cold-start clicks are still inspectable
179+
// after the Safari Web Inspector reattaches to the WKWebView.
180+
try {
181+
const existing = JSON.parse(localStorage.getItem('lastNotificationClicks') ?? '[]');
182+
existing.push({
183+
notificationId: e.notification.notificationId,
184+
title: e.notification.title ?? null,
185+
body: e.notification.body ?? null,
186+
actionId: e.result.actionId ?? null,
187+
url: e.result.url ?? null,
188+
receivedAt: new Date().toISOString(),
189+
});
190+
localStorage.setItem('lastNotificationClicks', JSON.stringify(existing.slice(-20)));
191+
} catch (err) {
192+
console.warn('Failed to persist notification click to localStorage', err);
193+
}
178194
};
179195

180196
const handleForegroundWillDisplay = (e: NotificationWillDisplayEvent) => {
@@ -280,7 +296,7 @@ export function useOneSignal(): UseOneSignalReturn {
280296

281297
console.log('Loaded OneSignal');
282298
return () => {
283-
console.log('Cleaning up OneSignal listeners');
299+
console.log('Cleaning up OneSignal listenerszzz');
284300
OneSignal.InAppMessages.removeEventListener('willDisplay', handleIamWillDisplay);
285301
OneSignal.InAppMessages.removeEventListener('didDisplay', handleIamDidDisplay);
286302
OneSignal.InAppMessages.removeEventListener('willDismiss', handleIamWillDismiss);

ios/Sources/OneSignalCapacitorPlugin/OneSignalCapacitorPlugin.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import Foundation
22
import Capacitor
33
import OneSignalFramework
44
import OneSignalLiveActivities
5+
#if SWIFT_PACKAGE
56
import OSCapacitorLaunchOptions
7+
#endif
68

79
@objc(OneSignalCapacitorPlugin)
810
public class OneSignalCapacitorPlugin: CAPPlugin, CAPBridgedPlugin,

0 commit comments

Comments
 (0)