Skip to content

Commit f97123f

Browse files
committed
feat: Implement in-app reminder alerts
Improved reminder notifications by adding in-app alerts when the app is already open. The service worker now checks for visible clients and sends a message to display an alert instead of a system notification if a client is visible and focused.
1 parent 5c22c34 commit f97123f

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

android/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "com.notask.app"
88
minSdkVersion rootProject.ext.minSdkVersion
99
targetSdkVersion rootProject.ext.targetSdkVersion
10-
versionCode 1518012
11-
versionName "15.18.12"
10+
versionCode 1518013
11+
versionName "15.18.13"
1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313
aaptOptions {
1414
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"type": "module",
33
"name": "my-notes-and-tasks-modern",
4-
"version": "15.18.12",
4+
"version": "15.18.13",
55
"private": true,
66
"license": "Licensed CC BY-ND 4.0. No derivatives allowed.",
77
"scripts": {

public/sw.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,33 @@ async function showReminderNotification(data) {
10141014
fullScreenIntent: options.android?.fullScreenIntent
10151015
});
10161016

1017+
// Check if any client (window/tab) is currently visible
1018+
const clients = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
1019+
let hasVisibleClient = false;
1020+
1021+
for (const client of clients) {
1022+
if (client.visibilityState === 'visible' && client.focused) {
1023+
hasVisibleClient = true;
1024+
// Send message to the visible client to show an in-app alert
1025+
client.postMessage({
1026+
type: 'SHOW_REMINDER_ALERT',
1027+
data: {
1028+
title,
1029+
body: options.body,
1030+
itemId: data.itemId,
1031+
itemTitle: data.itemTitle
1032+
}
1033+
});
1034+
console.log('🔔 SW: Sent reminder alert to visible client:', client.id);
1035+
break;
1036+
}
1037+
}
1038+
1039+
if (hasVisibleClient) {
1040+
console.log('🔔 SW: Page is visible, in-app alert sent instead of notification');
1041+
return; // Don't show system notification
1042+
}
1043+
10171044
await self.registration.showNotification(title, options);
10181045
console.log('🔔 SW: Enhanced reminder notification displayed successfully');
10191046

src/App.jsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,39 @@ const App = () => {
3030
return () => window.removeEventListener("storage", handleStorage);
3131
}, []);
3232

33-
// Initialize notification service
33+
// Initialize notification service and service worker message listener
3434
useEffect(() => {
3535
const initNotificationService = async () => {
3636
try {
3737
const { notificationService } = await import('./services/notificationService.js');
3838
await notificationService.initialize();
39-
await notificationService.registerActionTypes();
4039
console.log('🔔 Notification service initialized in App');
4140
} catch (error) {
4241
console.error('❌ Failed to initialize notification service:', error);
4342
}
4443
};
4544

45+
// Service worker message listener for reminder alerts
46+
const handleServiceWorkerMessage = (event) => {
47+
if (event.data?.type === 'SHOW_REMINDER_ALERT') {
48+
const { title, body, itemTitle } = event.data.data;
49+
console.log('🔔 Showing desktop reminder alert:', itemTitle);
50+
alert(`${title}\n${body}`);
51+
}
52+
};
53+
54+
if ('serviceWorker' in navigator) {
55+
navigator.serviceWorker.addEventListener('message', handleServiceWorkerMessage);
56+
}
57+
4658
initNotificationService();
59+
60+
// Cleanup
61+
return () => {
62+
if ('serviceWorker' in navigator) {
63+
navigator.serviceWorker.removeEventListener('message', handleServiceWorkerMessage);
64+
}
65+
};
4766
}, []);
4867

4968

0 commit comments

Comments
 (0)