|
1 | 1 | import { PUBLIC_PROVISIONER_URL } from "$env/static/public"; |
2 | | -import { addNotification } from "$lib/stores/notifications"; |
| 2 | +import { addNotification, hasNotification } from "$lib/stores/notifications"; |
3 | 3 | import { |
4 | 4 | isPermissionGranted, |
| 5 | + onNotificationReceived, |
5 | 6 | registerForPushNotifications, |
6 | 7 | requestPermission, |
7 | 8 | sendNotification, |
8 | 9 | } from "@choochmeque/tauri-plugin-notifications-api"; |
| 10 | +import type { PluginListener } from "@tauri-apps/api/core"; |
9 | 11 | import { invoke } from "@tauri-apps/api/core"; |
10 | 12 |
|
11 | 13 | export interface DeviceRegistration { |
@@ -401,6 +403,33 @@ class NotificationService { |
401 | 403 | if (platform !== "android" && platform !== "ios") return undefined; |
402 | 404 | return this.getPushNotificationToken(); |
403 | 405 | } |
| 406 | + /** |
| 407 | + * Listen for push notifications arriving while the app is in the foreground. |
| 408 | + * Stores them in the notification panel so they appear in the notifications tab. |
| 409 | + * Returns a cleanup function to remove the listener. |
| 410 | + */ |
| 411 | + async listenForForegroundNotifications(): Promise<PluginListener> { |
| 412 | + return onNotificationReceived((notification) => { |
| 413 | + if (notification.source !== "push") return; |
| 414 | + const title = notification.title ?? ""; |
| 415 | + if (!title) return; |
| 416 | + |
| 417 | + const raw = (notification.extra ?? {}) as Record<string, unknown>; |
| 418 | + const data = Object.fromEntries( |
| 419 | + Object.entries(raw).filter( |
| 420 | + (entry): entry is [string, string] => |
| 421 | + typeof entry[1] === "string", |
| 422 | + ), |
| 423 | + ); |
| 424 | + |
| 425 | + const body = notification.body ?? ""; |
| 426 | + const payload = Object.keys(data).length > 0 ? data : undefined; |
| 427 | + if (!hasNotification(title, body, payload)) { |
| 428 | + addNotification({ title, body, data: payload }); |
| 429 | + } |
| 430 | + }); |
| 431 | + } |
| 432 | + |
404 | 433 | /** |
405 | 434 | * Get eName from vault (helper method) |
406 | 435 | */ |
|
0 commit comments