{"record":{"id":"04b02a1411d8fd81","repo":"jlcodes99/cockpit-tools","slug":"no-task-found-for-notification-notificationid","errorCode":null,"errorMessage":"No task found for notification ${notificationId}","messagePattern":"No task found for notification (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/utils/wakeupNotificationListener.ts","lineNumber":28,"sourceCode":"  taskId: string;\n  notificationId: number;\n}\n\n// 存储通知 ID 到任务 ID 的映射\nconst notificationTaskMap = new Map<number, string>();\n\nexport function mapNotificationToTask(notificationId: number, taskId: string): void {\n  notificationTaskMap.set(notificationId, taskId);\n}\n\nexport function initWakeupNotificationListener(): void {\n  // 监听通知动作事件\n  listen<NotificationAction>('notification://action', async (event) => {\n    const { actionId, notificationId } = event.payload;\n\n    const taskId = notificationTaskMap.get(notificationId);\n    if (!taskId) {\n      console.warn(`No task found for notification ${notificationId}`);\n      return;\n    }\n\n    // 清理映射\n    notificationTaskMap.delete(notificationId);\n\n    if (actionId === 'confirm') {\n      try {\n        await invoke('confirm_wakeup_task', { taskId });\n        console.log(`Wakeup task ${taskId} confirmed and executed`);\n      } catch (error) {\n        console.error(`Failed to confirm wakeup task: ${error}`);\n      }\n    } else if (actionId === 'cancel') {\n      try {\n        await invoke('cancel_wakeup_task', { taskId });\n        console.log(`Wakeup task ${taskId} cancelled`);\n      } catch (error) {","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/jlcodes99/cockpit-tools/blob/1ed8b77992d62ca81fabf744deb0839ad361d5bf/src/utils/wakeupNotificationListener.ts#L10-L46","documentation":"initWakeupNotificationListener registers a handler for Tauri 'notification://action' events. When the user clicks a wakeup notification, the handler looks up notificationId in the module-level notificationTaskMap to find which task scheduled it. If the mapping is missing, it warns and returns early — the click has no effect and the stale notification is left on screen.","triggerScenarios":"User clicks a wakeup/scheduled notification whose id is not in notificationTaskMap: the app restarted since the notification was posted, the task was deleted before firing, or the notification was created by a previous session/app version.","commonSituations":"OS delivers a scheduled notification after an app reboot (map is in-memory and empty), duplicate notifications from an earlier session, or the task completed and its mapping was cleaned before the click arrived.","solutions":["Dismiss the stale notification manually; it refers to a task the current session no longer tracks.","Persist notificationTaskMap (or the task schedule) so mappings survive app restarts.","On startup, cancel previously scheduled OS notifications so old ones can't be clicked.","If the task should still exist, verify the code path that deleted the map entry (or the task) didn't run prematurely."],"exampleFix":"// before\nconst taskId = notificationTaskMap.get(notificationId);\nif (!taskId) {\n  console.warn(`No task found for notification ${notificationId}`);\n  return;\n}\n// after\nconst taskId = notificationTaskMap.get(notificationId);\nif (!taskId) {\n  console.warn(`No task found for notification ${notificationId}`);\n  cancelNotification(notificationId); // dismiss stale notification\n  return;\n}","handlingStrategy":"fallback","validationCode":"// before firing a notification, register its mapping and verify it\nnotificationTaskMap.set(notificationId, taskId);\nif (notificationTaskMap.get(notificationId) !== taskId) {\n  throw new Error('failed to register notification mapping');\n}\n","typeGuard":"function isRegisteredNotification(id: string): boolean {\n  return notificationTaskMap.has(id);\n}\n","tryCatchPattern":"listen<NotificationAction>('notification://action', async (event) => {\n  const taskId = notificationTaskMap.get(event.payload.notificationId);\n  if (!taskId) {\n    console.warn(`No task found for notification ${event.payload.notificationId}`);\n    await cancelNotification(event.payload.notificationId); // dismiss stale\n    return;\n  }\n  // ... handle task\n});","preventionTips":["Persist notification→task mappings or cancel scheduled notifications on app exit/startup","Cancel OS notifications when their task is deleted","Treat unmapped notification clicks as no-ops that also dismiss the notification"],"tags":["notifications","tauri","state","race-condition"],"backgroundTag":"stale-notification-callback","analyzedSha":"1ed8b77992d62ca81fabf744deb0839ad361d5bf","analyzedAt":"2026-09-05T09:51:41.178Z","contentChangedAt":"2026-09-05T09:51:41.178Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}