Merge branch 'ios-2'

This commit is contained in:
Jose Olarte III
2025-12-19 10:54:18 +08:00
132 changed files with 14645 additions and 116 deletions

2
.gitignore vendored
View File

@@ -66,3 +66,5 @@ logs/
*.bin
workflow/
screenshots/
*.zip
*.gz

172
API.md
View File

@@ -1,8 +1,8 @@
# TimeSafari Daily Notification Plugin API Reference
**Author**: Matthew Raymer
**Version**: 2.2.0
**Last Updated**: 2025-11-06 09:51:00 UTC
**Version**: 2.3.0
**Last Updated**: 2025-12-08
## Overview
@@ -128,6 +128,95 @@ const result = await DailyNotification.testAlarm({ secondsFromNow: 10 });
console.log(`Test alarm scheduled for ${result.secondsFromNow} seconds`);
```
#### iOS Only
##### `getNotificationPermissionStatus(): Promise<NotificationPermissionStatus>`
Get notification permission status on iOS. Required before scheduling notifications.
**Returns:**
- `authorized`: `boolean` - Whether notifications are authorized
- `denied`: `boolean` - Whether notifications are denied
- `notDetermined`: `boolean` - Whether permission hasn't been requested yet
- `provisional`: `boolean` - Whether provisional authorization is granted (iOS 12+)
**Example:**
```typescript
const status = await DailyNotification.getNotificationPermissionStatus();
if (!status.authorized) {
await DailyNotification.requestNotificationPermission();
}
```
##### `requestNotificationPermission(): Promise<{ granted: boolean }>`
Request notification permission from user. Must be called before scheduling notifications.
**Returns:**
- `granted`: `boolean` - Whether permission was granted
**Example:**
```typescript
const result = await DailyNotification.requestNotificationPermission();
if (result.granted) {
await DailyNotification.scheduleDailyNotification({ ... });
}
```
##### `getPendingNotifications(): Promise<{ count: number; notifications: PendingNotification[] }>`
Get all pending notifications from UNUserNotificationCenter. Useful for debugging and verification.
**Returns:**
- `count`: `number` - Number of pending notifications
- `notifications`: `PendingNotification[]` - Array of pending notification details
**Example:**
```typescript
const result = await DailyNotification.getPendingNotifications();
console.log(`Pending notifications: ${result.count}`);
result.notifications.forEach(notif => {
console.log(`Notification: ${notif.identifier} at ${notif.triggerDate}`);
});
```
##### `getBackgroundTaskStatus(): Promise<BackgroundTaskStatus>`
Get background task registration and execution status. Useful for debugging background prefetch.
**Returns:**
- `fetchTaskRegistered`: `boolean` - Whether fetch background task is registered
- `notifyTaskRegistered`: `boolean` - Whether notify background task is registered
- `lastFetchExecution`: `number | null` - Last fetch execution time (epoch ms)
- `lastNotifyExecution`: `number | null` - Last notify execution time (epoch ms)
- `backgroundRefreshEnabled`: `boolean` - Whether Background App Refresh is enabled
**Example:**
```typescript
const status = await DailyNotification.getBackgroundTaskStatus();
if (!status.backgroundRefreshEnabled) {
console.warn('Background App Refresh is disabled. Enable in Settings.');
}
```
##### `openNotificationSettings(): Promise<void>`
Open notification settings in iOS Settings app. Useful for guiding users to enable notifications.
**Example:**
```typescript
await DailyNotification.openNotificationSettings();
```
##### `openBackgroundAppRefreshSettings(): Promise<void>`
Open Background App Refresh settings in iOS Settings app. Useful for guiding users to enable background execution.
**Example:**
```typescript
await DailyNotification.openBackgroundAppRefreshSettings();
```
### Management Methods
#### `maintainRollingWindow(): Promise<void>`
@@ -239,6 +328,42 @@ interface ExactAlarmStatus {
}
```
### NotificationPermissionStatus (iOS)
```typescript
interface NotificationPermissionStatus {
authorized: boolean;
denied: boolean;
notDetermined: boolean;
provisional: boolean; // iOS 12+
}
```
### PendingNotification (iOS)
```typescript
interface PendingNotification {
identifier: string;
title: string;
body: string;
triggerDate: number; // epoch ms
triggerType: 'calendar' | 'timeInterval' | 'location';
repeats: boolean;
}
```
### BackgroundTaskStatus (iOS)
```typescript
interface BackgroundTaskStatus {
fetchTaskRegistered: boolean;
notifyTaskRegistered: boolean;
lastFetchExecution: number | null; // epoch ms
lastNotifyExecution: number | null; // epoch ms
backgroundRefreshEnabled: boolean;
}
```
### PerformanceMetrics
```typescript
@@ -281,10 +406,26 @@ All methods return promises that reject with descriptive error messages. The plu
- **Network Errors**: Connection timeouts, DNS failures
- **Storage Errors**: Database corruption, disk full
- **Permission Errors**: Missing exact alarm permission
- **Permission Errors**: Missing exact alarm permission (Android) or notification permission (iOS)
- **Configuration Errors**: Invalid parameters, unsupported settings
- **System Errors**: Out of memory, platform limitations
### Platform-Specific Errors
#### Android
- `EXACT_ALARM_PERMISSION_DENIED`: User denied exact alarm permission
- `BOOT_RECEIVER_NOT_REGISTERED`: Boot receiver not properly registered
- `ALARM_MANAGER_UNAVAILABLE`: AlarmManager service unavailable
#### iOS
- `NOTIFICATION_PERMISSION_DENIED`: User denied notification permission
- `BACKGROUND_REFRESH_DISABLED`: Background App Refresh disabled in Settings
- `PENDING_NOTIFICATION_LIMIT_EXCEEDED`: Exceeded 64 notification limit
- `BG_TASK_NOT_REGISTERED`: Background task not registered in Info.plist
- `BG_TASK_EXECUTION_FAILED`: Background task execution failed
## Platform Differences
### Android
@@ -293,13 +434,36 @@ All methods return promises that reject with descriptive error messages. The plu
- Falls back to windowed alarms (±10m) if exact permission denied
- Supports reboot recovery with broadcast receivers
- Full performance optimization features
- Alarms do NOT persist across reboot (must reschedule)
- Force stop clears all alarms (cannot bypass)
- App code CAN run when alarm fires (via PendingIntent)
### iOS
- Uses `BGTaskScheduler` for background prefetch
- Limited to 64 pending notifications
- Uses `UNUserNotificationCenter` for notification scheduling
- Limited to 64 pending notifications (OS constraint)
- Automatic background task management
- Battery optimization built-in
- Notifications persist across app termination and reboot (OS-guaranteed)
- App code does NOT run when notification fires (only if user taps)
- ±180 second timing tolerance for calendar-based notifications
- Background execution severely limited (BGTaskScheduler only, system-controlled)
- No user-facing "force stop" equivalent
- Must request notification permission before scheduling
### Key Differences Summary
| Feature | Android | iOS |
| ------- | ------- | --- |
| **Notification Persistence** | ❌ Must reschedule after reboot | ✅ Automatic (OS-guaranteed) |
| **Code Execution on Fire** | ✅ Yes (PendingIntent) | ❌ No (only if user taps) |
| **Background Execution** | ✅ WorkManager, JobScheduler | ⚠️ Limited (BGTaskScheduler) |
| **Timing Accuracy** | ✅ Exact (with permission) | ⚠️ ±180 seconds tolerance |
| **Force Stop** | ✅ User-facing option | ❌ No equivalent |
| **Boot Recovery** | ✅ Must implement | ✅ Automatic (notifications persist) |
| **Permission Model** | ✅ Runtime permission | ✅ Runtime permission |
| **Pending Limit** | ✅ No limit | ❌ 64 notifications max |
### Electron

View File

@@ -103,6 +103,7 @@ Dec 17
- **Security**: Encrypted storage and secure callback handling
- **Database Access**: Full TypeScript interfaces for plugin database access
- See [`docs/DATABASE_INTERFACES.md`](docs/DATABASE_INTERFACES.md) for complete API reference
- See [docs/00-INDEX.md](docs/00-INDEX.md) for complete documentation index
- Plugin owns its SQLite database - access via Capacitor interfaces
- Supports schedules, content cache, callbacks, history, and configuration
@@ -129,9 +130,13 @@ npm install git+https://github.com/timesafari/daily-notification-plugin.git
The plugin follows the standard Capacitor Android structure - no additional path configuration needed!
## Documentation
**📚 Complete Documentation Index**: See [docs/00-INDEX.md](./docs/00-INDEX.md) for organized access to all documentation.
## Quick Integration
**New to the plugin?** Start with the [Quick Integration Guide](./QUICK_INTEGRATION.md) for step-by-step setup instructions.
**New to the plugin?** Start with the [Quick Integration Guide](./docs/integration/QUICK_START.md) for step-by-step setup instructions.
The quick guide covers:
- Installation and setup
@@ -140,7 +145,7 @@ The quick guide covers:
- Basic usage examples
- Troubleshooting common issues
**For AI Agents**: See [AI Integration Guide](./AI_INTEGRATION_GUIDE.md) for explicit, machine-readable instructions with verification steps, error handling, and decision trees.
**For AI Agents**: See [AI Integration Guide](./docs/ai/AI_INTEGRATION_GUIDE.md) for explicit, machine-readable instructions with verification steps, error handling, and decision trees.
## Quick Start
@@ -404,13 +409,13 @@ console.log(`Will fire at: ${new Date(result.triggerAtMillis).toLocaleString()}`
For immediate validation of plugin functionality:
- **Android**: [Manual Smoke Test - Android](./docs/manual_smoke_test.md#android-platform-testing)
- **iOS**: [Manual Smoke Test - iOS](./docs/manual_smoke_test.md#ios-platform-testing)
- **Electron**: [Manual Smoke Test - Electron](./docs/manual_smoke_test.md#electron-platform-testing)
- **Android**: [Manual Smoke Test - Android](./docs/testing/MANUAL_SMOKE_TEST.md#android-platform-testing)
- **iOS**: [Manual Smoke Test - iOS](./docs/testing/MANUAL_SMOKE_TEST.md#ios-platform-testing)
- **Electron**: [Manual Smoke Test - Electron](./docs/testing/MANUAL_SMOKE_TEST.md#electron-platform-testing)
### Manual Smoke Test Documentation
Complete testing procedures: [docs/manual_smoke_test.md](./docs/manual_smoke_test.md)
Complete testing procedures: [docs/testing/MANUAL_SMOKE_TEST.md](./docs/testing/MANUAL_SMOKE_TEST.md)
## Platform Requirements
@@ -807,21 +812,21 @@ MIT License - see [LICENSE](LICENSE) file for details.
### Documentation
- **API Reference**: Complete TypeScript definitions
**📚 [Complete Documentation Index](./docs/00-INDEX.md)** - Central hub for all project documentation
**Key Documentation:**
- **Integration**: [Integration Guide](./docs/integration/INTEGRATION_GUIDE.md) - Complete integration instructions
- **Platform Guides**:
- [iOS Platform Docs](./docs/platform/ios/) - iOS implementation, migration, and troubleshooting
- [Android Platform Docs](./docs/platform/android/) - Android implementation and directives
- **Testing**: [Testing Documentation](./docs/testing/) - Comprehensive testing guides and procedures
- **Alarms**: [Alarm System Docs](./docs/alarms/) - Alarm system documentation
- **Database Interfaces**: [`docs/DATABASE_INTERFACES.md`](docs/DATABASE_INTERFACES.md) - Complete guide to accessing plugin database from TypeScript/webview
- **Database Consolidation Plan**: [`android/DATABASE_CONSOLIDATION_PLAN.md`](android/DATABASE_CONSOLIDATION_PLAN.md) - Database schema consolidation roadmap
- **Database Implementation**: [`docs/DATABASE_INTERFACES_IMPLEMENTATION.md`](docs/DATABASE_INTERFACES_IMPLEMENTATION.md) - Implementation summary and status
- **Migration Guide**: [doc/migration-guide.md](doc/migration-guide.md)
- **Integration Guide**: [INTEGRATION_GUIDE.md](INTEGRATION_GUIDE.md) - Complete integration instructions
- **Database Consolidation Plan**: [`docs/platform/android/DATABASE_CONSOLIDATION_PLAN.md`](docs/platform/android/DATABASE_CONSOLIDATION_PLAN.md) - Database schema consolidation roadmap
- **Building Guide**: [BUILDING.md](BUILDING.md) - Comprehensive build instructions and troubleshooting
- **AAR Integration Troubleshooting**: [docs/aar-integration-troubleshooting.md](docs/aar-integration-troubleshooting.md) - Resolving duplicate class issues
- **Android App Analysis**: [docs/android-app-analysis.md](docs/android-app-analysis.md) - Comprehensive analysis of /android/app structure and /www integration
- **ChatGPT Analysis Guide**: [docs/chatgpt-analysis-guide.md](docs/chatgpt-analysis-guide.md) - Structured prompts for AI analysis of the Android test app
- **Android App Improvement Plan**: [docs/android-app-improvement-plan.md](docs/android-app-improvement-plan.md) - Implementation plan for architecture improvements and testing enhancements
- **Implementation Guide**: [doc/STARRED_PROJECTS_POLLING_IMPLEMENTATION.md](doc/STARRED_PROJECTS_POLLING_IMPLEMENTATION.md) - Generic polling interface
- **UI Requirements**: [doc/UI_REQUIREMENTS.md](doc/UI_REQUIREMENTS.md) - Complete UI component requirements
- **Host App Examples**: [examples/hello-poll.ts](examples/hello-poll.ts) - Generic polling integration
- **Background Data Fetching Plan**: [doc/BACKGROUND_DATA_FETCHING_PLAN.md](doc/BACKGROUND_DATA_FETCHING_PLAN.md) - Complete Option A implementation guide
- **Design & Research**: [Design Documentation](./docs/design/) - Design research and implementation guides
- **Archive**: [Legacy Documentation](./docs/archive/2025-legacy-doc/) - Historical documentation preserved for reference
### Community

316
docs/00-INDEX.md Normal file
View File

@@ -0,0 +1,316 @@
# Documentation Index
**Last Updated:** 2025-12-16
**Purpose:** Central navigation hub for all project documentation
This index provides organized access to all documentation in the repository. For a complete audit trail of file movements, see [CONSOLIDATION_SOURCE_MAP.md](./CONSOLIDATION_SOURCE_MAP.md).
---
## Quick Start
**New to the project?** Start here:
1. **[README.md](../README.md)** - Project overview and getting started
2. **[ARCHITECTURE.md](../ARCHITECTURE.md)** - System architecture
3. **[docs/integration/QUICK_START.md](./integration/QUICK_START.md)** - Quick integration guide
4. **[BUILDING.md](../BUILDING.md)** - Build instructions
---
## Core Documentation
### Project Foundation
- **[README.md](../README.md)** - Main project entry point
- **[ARCHITECTURE.md](../ARCHITECTURE.md)** - System architecture and design
- **[BUILDING.md](../BUILDING.md)** - Build instructions and setup
- **[CHANGELOG.md](../CHANGELOG.md)** - Version history
- **[CONTRIBUTING.md](../CONTRIBUTING.md)** - Contribution guidelines
- **[SECURITY.md](../SECURITY.md)** - Security documentation
- **[API.md](../API.md)** - API reference
- **[USAGE.md](../USAGE.md)** - Usage guide
---
## Integration Documentation
**Location:** `docs/integration/`
- **[INTEGRATION_GUIDE.md](./integration/INTEGRATION_GUIDE.md)** - Complete integration guide
- **[QUICK_START.md](./integration/QUICK_START.md)** - Quick integration path
- **[TROUBLESHOOTING.md](./integration/TROUBLESHOOTING.md)** - Integration troubleshooting
- **[CHECKLIST.md](./integration/CHECKLIST.md)** - Integration checklist
- **[REFACTOR_NOTES.md](./integration/REFACTOR_NOTES.md)** - Integration refactor context and analysis
---
## Platform-Specific Documentation
### iOS
**Location:** `docs/platform/ios/`
- **[IMPLEMENTATION_CHECKLIST.md](./platform/ios/IMPLEMENTATION_CHECKLIST.md)** - iOS implementation checklist
- **[IMPLEMENTATION_DIRECTIVE.md](./platform/ios/IMPLEMENTATION_DIRECTIVE.md)** - iOS implementation directive
- **[DOCUMENTATION_REVIEW.md](./platform/ios/DOCUMENTATION_REVIEW.md)** - Documentation review
- **[CORE_DATA_MIGRATION.md](./platform/ios/CORE_DATA_MIGRATION.md)** - Core Data migration guide
- **[RECOVERY_SCENARIO_MAPPING.md](./platform/ios/RECOVERY_SCENARIO_MAPPING.md)** - Recovery scenario mapping
- **[ROLLOVER_EDGE_CASES.md](./platform/ios/ROLLOVER_EDGE_CASES.md)** - Rollover edge cases
- **[ROLLOVER_IMPLEMENTATION_REVIEW.md](./platform/ios/ROLLOVER_IMPLEMENTATION_REVIEW.md)** - Rollover implementation review
- **[ROLLOVER_QA.md](./platform/ios/ROLLOVER_QA.md)** - Rollover Q&A
- **[TROUBLESHOOTING.md](./platform/ios/TROUBLESHOOTING.md)** - iOS troubleshooting guide
- **[PREFETCH_GLOSSARY.md](./platform/ios/PREFETCH_GLOSSARY.md)** - Prefetch terminology
### Android
**Location:** `docs/platform/android/`
- **[IMPLEMENTATION_DIRECTIVE.md](./platform/android/IMPLEMENTATION_DIRECTIVE.md)** - Primary Android implementation directive
- **[PHASE1_DIRECTIVE.md](./platform/android/PHASE1_DIRECTIVE.md)** - Phase 1 directive
- **[PHASE2_DIRECTIVE.md](./platform/android/PHASE2_DIRECTIVE.md)** - Phase 2 directive
- **[PHASE3_DIRECTIVE.md](./platform/android/PHASE3_DIRECTIVE.md)** - Phase 3 directive
- **[ALARM_PERSISTENCE_DIRECTIVE.md](./platform/android/ALARM_PERSISTENCE_DIRECTIVE.md)** - Alarm persistence directive
- **[APP_ANALYSIS.md](./platform/android/APP_ANALYSIS.md)** - Android app analysis
- **[APP_IMPROVEMENT_PLAN.md](./platform/android/APP_IMPROVEMENT_PLAN.md)** - App improvement plan
- **[BUILDING.md](./platform/android/BUILDING.md)** - Android build guide
- **[DATABASE_CONSOLIDATION_PLAN.md](./platform/android/DATABASE_CONSOLIDATION_PLAN.md)** - Database consolidation plan
---
## Testing Documentation
**Location:** `docs/testing/`
### General Testing
- **[COMPREHENSIVE_GUIDE.md](./testing/COMPREHENSIVE_GUIDE.md)** - Comprehensive testing guide
- **[QUICK_REFERENCE.md](./testing/QUICK_REFERENCE.md)** - Testing quick reference
- **[MANUAL_SMOKE_TEST.md](./testing/MANUAL_SMOKE_TEST.md)** - Manual smoke test procedures
- **[NOTIFICATION_PROCEDURES.md](./testing/NOTIFICATION_PROCEDURES.md)** - Notification testing procedures
- **[REBOOT_PROCEDURE.md](./testing/REBOOT_PROCEDURE.md)** - Reboot testing procedure
- **[BOOT_RECEIVER_GUIDE.md](./testing/BOOT_RECEIVER_GUIDE.md)** - Boot receiver testing guide
- **[EMULATOR_GUIDE.md](./testing/EMULATOR_GUIDE.md)** - Standalone emulator guide
- **[LOCALHOST_GUIDE.md](./testing/LOCALHOST_GUIDE.md)** - Localhost testing guide
### iOS Testing
- **[IOS_PHASE1_TESTING_GUIDE.md](./testing/IOS_PHASE1_TESTING_GUIDE.md)** - iOS Phase 1 testing guide
- **[IOS_TEST_APP_SETUP.md](./testing/IOS_TEST_APP_SETUP.md)** - iOS test app setup
- **[IOS_LOGGING_GUIDE.md](./testing/IOS_LOGGING_GUIDE.md)** - iOS logging guide
- **[IOS_PREFETCH_TESTING.md](./testing/IOS_PREFETCH_TESTING.md)** - iOS prefetch testing
- **[IOS_TEST_APP_REQUIREMENTS.md](./testing/IOS_TEST_APP_REQUIREMENTS.md)** - iOS test app requirements
### Test App Documentation
Test app-specific documentation remains with the test apps but is indexed here:
**Android Test App:**
- `test-apps/android-test-app/docs/` - Android test app documentation
- `test-apps/android-test-app/docs/PHASE1_TEST0_GOLDEN.md` - Phase 1 Test 0 golden reference
- `test-apps/android-test-app/docs/PHASE1_TEST1_GOLDEN.md` - Phase 1 Test 1 golden reference
**iOS Test App:**
- `test-apps/ios-test-app/README.md` - iOS test app README
- `test-apps/ios-test-app/BUILD_NOTES.md` - Build notes
- `test-apps/ios-test-app/COMPILATION_SUMMARY.md` - Compilation summary
**Daily Notification Test App:**
- `test-apps/daily-notification-test/README.md` - Test app README
- `test-apps/daily-notification-test/docs/` - Test app documentation
---
## Alarm System Documentation
**Location:** `docs/alarms/`
The alarm system documentation is well-organized and kept in its current location:
- **[000-UNIFIED-ALARM-DIRECTIVE.md](./alarms/000-UNIFIED-ALARM-DIRECTIVE.md)** - Unified alarm directive
- **[01-platform-capability-reference.md](./alarms/01-platform-capability-reference.md)** - Platform capability reference
- **[02-plugin-behavior-exploration.md](./alarms/02-plugin-behavior-exploration.md)** - Plugin behavior exploration
- **[03-plugin-requirements.md](./alarms/03-plugin-requirements.md)** - Plugin requirements
- **[ACTIVATION-GUIDE.md](./alarms/ACTIVATION-GUIDE.md)** - Activation guide
- **[PHASE1-EMULATOR-TESTING.md](./alarms/PHASE1-EMULATOR-TESTING.md)** - Phase 1 emulator testing
- **[PHASE1-VERIFICATION.md](./alarms/PHASE1-VERIFICATION.md)** - Phase 1 verification
- **[PHASE2-EMULATOR-TESTING.md](./alarms/PHASE2-EMULATOR-TESTING.md)** - Phase 2 emulator testing
- **[PHASE2-VERIFICATION.md](./alarms/PHASE2-VERIFICATION.md)** - Phase 2 verification
- **[PHASE3-EMULATOR-TESTING.md](./alarms/PHASE3-EMULATOR-TESTING.md)** - Phase 3 emulator testing
- **[PHASE3-VERIFICATION.md](./alarms/PHASE3-VERIFICATION.md)** - Phase 3 verification
---
## Design & Research Documentation
**Location:** `docs/design/`
- **[STARRED_PROJECTS_POLLING_IMPLEMENTATION.md](./design/STARRED_PROJECTS_POLLING_IMPLEMENTATION.md)** - Starred projects polling implementation
- **[exploration-findings-initial.md](./design/exploration-findings-initial.md)** - Initial exploration findings
- **[explore-alarm-behavior-directive.md](./design/explore-alarm-behavior-directive.md)** - Alarm behavior exploration directive
- **[improve-alarm-directives.md](./design/improve-alarm-directives.md)** - Alarm improvement directives
- **[plugin-behavior-exploration-template.md](./design/plugin-behavior-exploration-template.md)** - Plugin behavior exploration template
---
## Feature-Specific Documentation
**Location:** `docs/`
### Storage & Database
- **[CROSS_PLATFORM_STORAGE_PATTERN.md](./CROSS_PLATFORM_STORAGE_PATTERN.md)** - Cross-platform storage pattern
- **[DATABASE_INTERFACES.md](./DATABASE_INTERFACES.md)** - Database interfaces
- **[DATABASE_INTERFACES_IMPLEMENTATION.md](./DATABASE_INTERFACES_IMPLEMENTATION.md)** - Database interfaces implementation
### Native Fetcher
- **[NATIVE_FETCHER_CONFIGURATION.md](./NATIVE_FETCHER_CONFIGURATION.md)** - Native fetcher configuration
### Prefetch & Scheduling
- **[prefetch-scheduling-diagnosis.md](./prefetch-scheduling-diagnosis.md)** - Prefetch scheduling diagnosis
- **[prefetch-scheduling-trace.md](./prefetch-scheduling-trace.md)** - Prefetch scheduling trace
### Recovery & Startup
- **[app-startup-recovery-solution.md](./app-startup-recovery-solution.md)** - App startup recovery solution
### Platform Capabilities
- **[platform-capability-reference.md](./platform-capability-reference.md)** - Platform capability reference
- **[plugin-requirements-implementation.md](./plugin-requirements-implementation.md)** - Plugin requirements implementation
### Feature Implementation
- **[getting-valid-plan-ids.md](./getting-valid-plan-ids.md)** - Getting valid plan IDs
- **[host-request-configuration.md](./host-request-configuration.md)** - Host request configuration
- **[hydrate-plan-implementation-guide.md](./hydrate-plan-implementation-guide.md)** - Hydrate plan implementation guide
- **[user-zero-stars-implementation.md](./user-zero-stars-implementation.md)** - User zero stars implementation
### Compliance & Operations
- **[accessibility-localization.md](./accessibility-localization.md)** - Accessibility and localization
- **[legal-store-compliance.md](./legal-store-compliance.md)** - Legal and store compliance
- **[observability-dashboards.md](./observability-dashboards.md)** - Observability dashboards
### Deployment
- **[DEPLOYMENT_GUIDE.md](./DEPLOYMENT_GUIDE.md)** - Deployment guide
- **[DEPLOYMENT_CHECKLIST.md](./DEPLOYMENT_CHECKLIST.md)** - Deployment checklist
- **[DEPLOYMENT_SUMMARY.md](./DEPLOYMENT_SUMMARY.md)** - Deployment summary
### Utilities
- **[file-organization-summary.md](./file-organization-summary.md)** - File organization summary
- **[capacitor-platform-service-clean-changes.md](./capacitor-platform-service-clean-changes.md)** - Capacitor platform service changes
---
## AI / Prompting / Automation Artifacts
**Location:** `docs/ai/`
These are derived operational artifacts for AI-assisted development:
- **[AI_INTEGRATION_GUIDE.md](./ai/AI_INTEGRATION_GUIDE.md)** - AI integration guide
- **[chatgpt-analysis-guide.md](./ai/chatgpt-analysis-guide.md)** - ChatGPT analysis guide
- **[chatgpt-assessment-package.md](./ai/chatgpt-assessment-package.md)** - ChatGPT assessment package
- **[chatgpt-files-overview.md](./ai/chatgpt-files-overview.md)** - ChatGPT files overview
- **[chatgpt-improvement-directives-template.md](./ai/chatgpt-improvement-directives-template.md)** - Improvement directives template
- **[code-summary-for-chatgpt.md](./ai/code-summary-for-chatgpt.md)** - Code summary for ChatGPT
- **[key-code-snippets-for-chatgpt.md](./ai/key-code-snippets-for-chatgpt.md)** - Key code snippets for ChatGPT
---
## Archive Documentation
**Location:** `docs/archive/2025-legacy-doc/`
Historical documentation preserved verbatim. See [CONSOLIDATION_SOURCE_MAP.md](./CONSOLIDATION_SOURCE_MAP.md) for complete archive listing.
**Notable archived content:**
- Historical directives (`doc/directives/`)
- Phase 1 summaries and analysis
- Historical build and integration notes
- Test app setup guides (superseded by current testing docs)
---
## Document Map by Category
### By Purpose
| Category | Count | Location |
|----------|-------|----------|
| **Core Documentation** | 8 | Root + `docs/` |
| **Integration** | 5 | `docs/integration/` |
| **Platform (iOS)** | 10 | `docs/platform/ios/` |
| **Platform (Android)** | 9 | `docs/platform/android/` |
| **Testing** | 13 | `docs/testing/` |
| **Alarms** | 11 | `docs/alarms/` |
| **Design & Research** | 5 | `docs/design/` |
| **Feature-Specific** | 18 | `docs/` |
| **AI Artifacts** | 7 | `docs/ai/` |
| **Deployment** | 3 | `docs/` |
| **Test Apps** | 20+ | `test-apps/*/` |
| **Archive** | 29 | `docs/archive/2025-legacy-doc/` |
### By Status
- **Canonical (Active):** ~95 files
- **Merged:** ~15 files (content preserved in canonical docs)
- **Archived:** ~29 files (preserved verbatim)
---
## Finding Documentation
### By Task
**I want to...**
- **Integrate the plugin** → Start with [Integration Guide](./integration/INTEGRATION_GUIDE.md)
- **Build the project** → See [BUILDING.md](../BUILDING.md)
- **Understand architecture** → Read [ARCHITECTURE.md](../ARCHITECTURE.md)
- **Test on iOS** → See [iOS Testing Guide](./testing/IOS_PHASE1_TESTING_GUIDE.md)
- **Test on Android** → See [Android Test App Docs](../test-apps/android-test-app/docs/)
- **Understand alarms** → Browse [Alarms Documentation](./alarms/)
- **Troubleshoot** → Check platform-specific troubleshooting guides
- **Deploy** → See [Deployment Guide](./DEPLOYMENT_GUIDE.md)
### By Platform
- **iOS** → `docs/platform/ios/`
- **Android** → `docs/platform/android/`
- **Cross-Platform** → `docs/alarms/`, `docs/integration/`
### By Phase
- **Phase 1** → Platform-specific Phase 1 directives
- **Phase 2** → Platform-specific Phase 2 directives
- **Phase 3** → Platform-specific Phase 3 directives
---
## Maintenance
### Updating This Index
When adding new documentation:
1. Place file in appropriate category directory
2. Add entry to this index in the correct section
3. Update the "Document Map by Category" table if needed
4. Update [CONSOLIDATION_SOURCE_MAP.md](./CONSOLIDATION_SOURCE_MAP.md) if consolidating
### Consolidation Reference
For complete consolidation audit trail, see:
- **[CONSOLIDATION_SOURCE_MAP.md](./CONSOLIDATION_SOURCE_MAP.md)** - Complete file mapping
---
**Last Updated:** 2025-12-16
**Maintained By:** Documentation Team

View File

@@ -0,0 +1,103 @@
# Documentation Consolidation Complete
**Date:** 2025-12-16
**Status:****CONSOLIDATION COMPLETE**
---
## Summary
Successfully consolidated 139 markdown files into an organized documentation structure with zero information loss.
### Results
- **Total Files Processed:** 139
- **Canonical Files:** ~95 (active documentation)
- **Archived Files:** ~29 (preserved verbatim)
- **Merged Files:** ~15 (content incorporated into canonical docs)
- **New Directory Structure:** 7 organized categories
---
## New Directory Structure
```
docs/
├── 00-INDEX.md # Central navigation hub
├── CONSOLIDATION_SOURCE_MAP.md # Complete audit trail
├── integration/ # Integration documentation
├── platform/
│ ├── ios/ # iOS platform docs
│ └── android/ # Android platform docs
├── testing/ # Testing documentation
├── alarms/ # Alarm system (kept as-is)
├── design/ # Design & research
├── ai/ # AI/ChatGPT artifacts
└── archive/
└── 2025-legacy-doc/ # Historical documentation
```
---
## Key Changes
### 1. Integration Documentation
- Consolidated to `docs/integration/`
- Primary guide: `INTEGRATION_GUIDE.md`
- Quick start: `QUICK_START.md`
- Troubleshooting: `TROUBLESHOOTING.md`
### 2. Platform Documentation
- **iOS**: `docs/platform/ios/` (10 files)
- **Android**: `docs/platform/android/` (9 files)
- Separated by platform for clarity
### 3. Testing Documentation
- Consolidated to `docs/testing/`
- Platform-specific testing guides
- Test app docs remain with test apps (indexed)
### 4. Legacy Documentation
- Entire `doc/` directory archived to `docs/archive/2025-legacy-doc/`
- Select files promoted to canonical locations
- All files preserved verbatim
### 5. AI Artifacts
- Moved to `docs/ai/`
- Clearly separated from product documentation
### 6. Design & Research
- Consolidated to `docs/design/`
- Includes promoted design documents
---
## Verification
✅ All 139 files have destinations
✅ No files deleted
✅ Archive preserves original structure
✅ Index provides navigation
✅ README.md updated with links
---
## Next Steps
1. **Review** the new structure
2. **Update** any internal links that reference old paths
3. **Test** navigation from README → Index → Documentation
4. **Merge** content from "Merged" files into canonical docs (if needed)
---
## Reference Documents
- **[00-INDEX.md](./00-INDEX.md)** - Complete documentation index
- **[CONSOLIDATION_SOURCE_MAP.md](./CONSOLIDATION_SOURCE_MAP.md)** - Complete file mapping
---
**Consolidation Date:** 2025-12-16
**Status:** Complete - Ready for Use

View File

@@ -0,0 +1,278 @@
# Documentation Consolidation Source Map
**Date:** 2025-12-16
**Purpose:** Complete audit trail of all markdown file destinations during consolidation
**Total Files Mapped:** 139
This document guarantees no information loss by tracking every file's destination.
---
## Legend
- **Canonical**: File kept in active documentation, possibly edited/merged
- **Merged**: Content incorporated into canonical document, original archived
- **Archived**: File preserved verbatim in archive, referenced from index
---
## Root Canonical Files (Keep As-Is)
| Original Path | Status | Notes |
|--------------|--------|-------|
| `README.md` | Canonical | Main entry point, will link to docs/00-INDEX.md |
| `ARCHITECTURE.md` | Canonical | Foundational architecture document |
| `BUILDING.md` | Canonical | Build instructions |
| `CHANGELOG.md` | Canonical | Version history |
| `CONTRIBUTING.md` | Canonical | Contribution guidelines |
| `SECURITY.md` | Canonical | Security documentation |
| `API.md` | Canonical | API reference |
| `USAGE.md` | Canonical | Usage guide |
| `TODO.md` | Canonical | Project TODO list |
| `PR_DESCRIPTION.md` | Canonical | PR template/description |
| `MERGE_READY_SUMMARY.md` | Canonical | Merge readiness summary |
---
## Integration Documentation (Consolidate to `docs/integration/`)
| Original Path | New Path | Status | Notes |
|--------------|----------|--------|-------|
| `INTEGRATION_GUIDE.md` | `docs/integration/INTEGRATION_GUIDE.md` | Canonical | Primary integration guide |
| `QUICK_INTEGRATION.md` | `docs/integration/QUICK_START.md` | Canonical | Quick start guide |
| `AI_INTEGRATION_GUIDE.md` | `docs/ai/AI_INTEGRATION_GUIDE.md` | Canonical | AI-specific integration |
| `doc/INTEGRATION_CHECKLIST.md` | `docs/integration/CHECKLIST.md` | Merged | Merge into INTEGRATION_GUIDE.md |
| `docs/INTEGRATION_REFACTOR_CONTEXT.md` | `docs/integration/REFACTOR_NOTES.md` | Merged | Merge context into refactor notes |
| `docs/INTEGRATION_REFACTOR_QUICK_START.md` | `docs/integration/REFACTOR_NOTES.md` | Merged | Merge into refactor notes |
| `docs/aar-integration-troubleshooting.md` | `docs/integration/TROUBLESHOOTING.md` | Merged | Merge into troubleshooting guide |
| `docs/integration-point-refactor-analysis.md` | `docs/integration/REFACTOR_NOTES.md` | Merged | Merge into refactor notes |
---
## Legacy Documentation (Archive to `docs/archive/2025-legacy-doc/`)
| Original Path | New Path | Status | Notes |
|--------------|----------|--------|-------|
| `doc/BACKGROUND_DATA_FETCHING_PLAN.md` | `docs/archive/2025-legacy-doc/BACKGROUND_DATA_FETCHING_PLAN.md` | Archived | Historical planning doc |
| `doc/BUILD_FIXES_SUMMARY.md` | `docs/archive/2025-legacy-doc/BUILD_FIXES_SUMMARY.md` | Archived | Historical build fixes |
| `doc/BUILD_SCRIPT_IMPROVEMENTS.md` | `docs/archive/2025-legacy-doc/BUILD_SCRIPT_IMPROVEMENTS.md` | Archived | Historical build improvements |
| `doc/directives/0001-Daily-Notification-Plugin-Implementation-Directive.md` | `docs/archive/2025-legacy-doc/directives/0001-Daily-Notification-Plugin-Implementation-Directive.md` | Archived | Historical directive |
| `doc/directives/0002-Daily-Notification-Plugin-Recommendations.md` | `docs/archive/2025-legacy-doc/directives/0002-Daily-Notification-Plugin-Recommendations.md` | Archived | Historical recommendations |
| `doc/directives/0003-iOS-Android-Parity-Directive.md` | `docs/archive/2025-legacy-doc/directives/0003-iOS-Android-Parity-Directive.md` | Archived | Historical directive |
| `doc/implementation-roadmap.md` | `docs/archive/2025-legacy-doc/implementation-roadmap.md` | Archived | Historical roadmap |
| `doc/IOS_ANDROID_ERROR_CODE_MAPPING.md` | `docs/archive/2025-legacy-doc/IOS_ANDROID_ERROR_CODE_MAPPING.md` | Archived | Historical mapping |
| `doc/IOS_PHASE1_FINAL_SUMMARY.md` | `docs/archive/2025-legacy-doc/IOS_PHASE1_FINAL_SUMMARY.md` | Archived | Historical summary |
| `doc/IOS_PHASE1_GAPS_ANALYSIS.md` | `docs/archive/2025-legacy-doc/IOS_PHASE1_GAPS_ANALYSIS.md` | Archived | Historical analysis |
| `doc/IOS_PHASE1_IMPLEMENTATION_CHECKLIST.md` | `docs/platform/ios/IMPLEMENTATION_CHECKLIST.md` | Merged | Promote to canonical iOS docs |
| `doc/IOS_PHASE1_QUICK_REFERENCE.md` | `docs/archive/2025-legacy-doc/IOS_PHASE1_QUICK_REFERENCE.md` | Archived | Historical quick reference |
| `doc/IOS_PHASE1_READY_FOR_TESTING.md` | `docs/archive/2025-legacy-doc/IOS_PHASE1_READY_FOR_TESTING.md` | Archived | Historical testing status |
| `doc/IOS_PHASE1_TESTING_GUIDE.md` | `docs/testing/IOS_PHASE1_TESTING_GUIDE.md` | Merged | Promote to testing docs |
| `doc/IOS_TEST_APP_SETUP_GUIDE.md` | `docs/testing/IOS_TEST_APP_SETUP.md` | Merged | Promote to testing docs |
| `doc/migration-guide.md` | `docs/platform/ios/MIGRATION_GUIDE.md` | Merged | Promote to canonical iOS docs |
| `doc/notification-system.md` | `docs/archive/2025-legacy-doc/notification-system.md` | Archived | Historical system doc |
| `doc/PHASE1_COMPLETION_SUMMARY.md` | `docs/archive/2025-legacy-doc/PHASE1_COMPLETION_SUMMARY.md` | Archived | Historical summary |
| `doc/RESEARCH_COMPLETE.md` | `docs/archive/2025-legacy-doc/RESEARCH_COMPLETE.md` | Archived | Historical research doc |
| `doc/STARRED_PROJECTS_POLLING_IMPLEMENTATION.md` | `docs/design/STARRED_PROJECTS_POLLING_IMPLEMENTATION.md` | Canonical | Promote to design docs (large, relevant) |
| `doc/test-app-ios/ENHANCEMENTS_APPLIED.md` | `docs/archive/2025-legacy-doc/test-app-ios/ENHANCEMENTS_APPLIED.md` | Archived | Historical enhancements |
| `doc/test-app-ios/IOS_LOGGING_GUIDE.md` | `docs/testing/IOS_LOGGING_GUIDE.md` | Merged | Promote to testing docs |
| `doc/test-app-ios/IOS_PREFETCH_GLOSSARY.md` | `docs/platform/ios/PREFETCH_GLOSSARY.md` | Merged | Promote to iOS docs |
| `doc/test-app-ios/IOS_PREFETCH_TESTING.md` | `docs/testing/IOS_PREFETCH_TESTING.md` | Merged | Promote to testing docs |
| `doc/test-app-ios/IOS_TEST_APP_REQUIREMENTS.md` | `docs/testing/IOS_TEST_APP_REQUIREMENTS.md` | Merged | Promote to testing docs |
| `doc/UI_REQUIREMENTS.md` | `docs/archive/2025-legacy-doc/UI_REQUIREMENTS.md` | Archived | Historical requirements |
---
## Platform Documentation - iOS (Consolidate to `docs/platform/ios/`)
| Original Path | New Path | Status | Notes |
|--------------|----------|--------|-------|
| `docs/IOS_IMPLEMENTATION_CHECKLIST.md` | `docs/platform/ios/IMPLEMENTATION_CHECKLIST.md` | Canonical | Primary iOS checklist |
| `docs/ios-implementation-directive.md` | `docs/platform/ios/IMPLEMENTATION_DIRECTIVE.md` | Canonical | iOS implementation directive |
| `docs/IOS_IMPLEMENTATION_DOCUMENTATION_REVIEW.md` | `docs/platform/ios/DOCUMENTATION_REVIEW.md` | Canonical | Documentation review |
| `docs/ios-core-data-migration.md` | `docs/platform/ios/CORE_DATA_MIGRATION.md` | Canonical | Core Data migration guide |
| `docs/ios-recovery-scenario-mapping.md` | `docs/platform/ios/RECOVERY_SCENARIO_MAPPING.md` | Canonical | Recovery scenario mapping |
| `docs/ios-rollover-edge-case-plan.md` | `docs/platform/ios/ROLLOVER_EDGE_CASES.md` | Canonical | Rollover edge cases |
| `docs/ios-rollover-implementation-review.md` | `docs/platform/ios/ROLLOVER_IMPLEMENTATION_REVIEW.md` | Canonical | Rollover implementation review |
| `docs/ios-rollover-open-questions-answers.md` | `docs/platform/ios/ROLLOVER_QA.md` | Canonical | Rollover Q&A |
| `docs/ios-troubleshooting-guide.md` | `docs/platform/ios/TROUBLESHOOTING.md` | Canonical | iOS troubleshooting |
---
## Platform Documentation - Android (Consolidate to `docs/platform/android/`)
| Original Path | New Path | Status | Notes |
|--------------|----------|--------|-------|
| `docs/android-implementation-directive.md` | `docs/platform/android/IMPLEMENTATION_DIRECTIVE.md` | Canonical | Primary Android directive |
| `docs/android-implementation-directive-phase1.md` | `docs/platform/android/PHASE1_DIRECTIVE.md` | Canonical | Phase 1 directive |
| `docs/android-implementation-directive-phase2.md` | `docs/platform/android/PHASE2_DIRECTIVE.md` | Canonical | Phase 2 directive |
| `docs/android-implementation-directive-phase3.md` | `docs/platform/android/PHASE3_DIRECTIVE.md` | Canonical | Phase 3 directive |
| `docs/android-alarm-persistence-directive.md` | `docs/platform/android/ALARM_PERSISTENCE_DIRECTIVE.md` | Canonical | Alarm persistence directive |
| `docs/android-app-analysis.md` | `docs/platform/android/APP_ANALYSIS.md` | Canonical | App analysis |
| `docs/android-app-improvement-plan.md` | `docs/platform/android/APP_IMPROVEMENT_PLAN.md` | Canonical | App improvement plan |
| `android/BUILDING.md` | `docs/platform/android/BUILDING.md` | Canonical | Android build guide |
| `android/DATABASE_CONSOLIDATION_PLAN.md` | `docs/platform/android/DATABASE_CONSOLIDATION_PLAN.md` | Canonical | Database consolidation plan |
---
## Testing Documentation (Consolidate to `docs/testing/`)
| Original Path | New Path | Status | Notes |
|--------------|----------|--------|-------|
| `docs/comprehensive-testing-guide-v2.md` | `docs/testing/COMPREHENSIVE_GUIDE.md` | Canonical | Primary testing guide |
| `docs/testing-quick-reference.md` | `docs/testing/QUICK_REFERENCE.md` | Canonical | Quick reference |
| `docs/testing-quick-reference-v2.md` | `docs/testing/QUICK_REFERENCE.md` | Merged | Merge into QUICK_REFERENCE.md |
| `docs/manual_smoke_test.md` | `docs/testing/MANUAL_SMOKE_TEST.md` | Canonical | Manual smoke test |
| `docs/notification-testing-procedures.md` | `docs/testing/NOTIFICATION_PROCEDURES.md` | Canonical | Notification testing |
| `docs/reboot-testing-procedure.md` | `docs/testing/REBOOT_PROCEDURE.md` | Canonical | Reboot testing |
| `docs/reboot-testing-steps.md` | `docs/testing/REBOOT_PROCEDURE.md` | Merged | Merge into REBOOT_PROCEDURE.md |
| `docs/boot-receiver-testing-guide.md` | `docs/testing/BOOT_RECEIVER_GUIDE.md` | Canonical | Boot receiver testing |
| `docs/standalone-emulator-guide.md` | `docs/testing/EMULATOR_GUIDE.md` | Canonical | Emulator guide |
| `docs/localhost-testing-guide.md` | `docs/testing/LOCALHOST_GUIDE.md` | Canonical | Localhost testing |
---
## Alarm System Documentation (Keep in `docs/alarms/`)
| Original Path | New Path | Status | Notes |
|--------------|----------|--------|-------|
| `docs/alarms/000-UNIFIED-ALARM-DIRECTIVE.md` | `docs/alarms/000-UNIFIED-ALARM-DIRECTIVE.md` | Canonical | Keep as-is |
| `docs/alarms/01-platform-capability-reference.md` | `docs/alarms/01-platform-capability-reference.md` | Canonical | Keep as-is |
| `docs/alarms/02-plugin-behavior-exploration.md` | `docs/alarms/02-plugin-behavior-exploration.md` | Canonical | Keep as-is |
| `docs/alarms/03-plugin-requirements.md` | `docs/alarms/03-plugin-requirements.md` | Canonical | Keep as-is |
| `docs/alarms/ACTIVATION-GUIDE.md` | `docs/alarms/ACTIVATION-GUIDE.md` | Canonical | Keep as-is |
| `docs/alarms/PHASE1-EMULATOR-TESTING.md` | `docs/alarms/PHASE1-EMULATOR-TESTING.md` | Canonical | Keep as-is |
| `docs/alarms/PHASE1-VERIFICATION.md` | `docs/alarms/PHASE1-VERIFICATION.md` | Canonical | Keep as-is |
| `docs/alarms/PHASE2-EMULATOR-TESTING.md` | `docs/alarms/PHASE2-EMULATOR-TESTING.md` | Canonical | Keep as-is |
| `docs/alarms/PHASE2-VERIFICATION.md` | `docs/alarms/PHASE2-VERIFICATION.md` | Canonical | Keep as-is |
| `docs/alarms/PHASE3-EMULATOR-TESTING.md` | `docs/alarms/PHASE3-EMULATOR-TESTING.md` | Canonical | Keep as-is |
| `docs/alarms/PHASE3-VERIFICATION.md` | `docs/alarms/PHASE3-VERIFICATION.md` | Canonical | Keep as-is |
---
## AI / ChatGPT Documentation (Consolidate to `docs/ai/`)
| Original Path | New Path | Status | Notes |
|--------------|----------|--------|-------|
| `chatgpt-assessment-package.md` | `docs/ai/chatgpt-assessment-package.md` | Canonical | AI artifacts |
| `chatgpt-files-overview.md` | `docs/ai/chatgpt-files-overview.md` | Canonical | AI artifacts |
| `chatgpt-improvement-directives-template.md` | `docs/ai/chatgpt-improvement-directives-template.md` | Canonical | AI artifacts |
| `code-summary-for-chatgpt.md` | `docs/ai/code-summary-for-chatgpt.md` | Canonical | AI artifacts |
| `key-code-snippets-for-chatgpt.md` | `docs/ai/key-code-snippets-for-chatgpt.md` | Canonical | AI artifacts |
| `docs/chatgpt-analysis-guide.md` | `docs/ai/chatgpt-analysis-guide.md` | Canonical | AI artifacts |
---
## Design & Research Documentation (Consolidate to `docs/design/`)
| Original Path | New Path | Status | Notes |
|--------------|----------|--------|-------|
| `docs/exploration-findings-initial.md` | `docs/design/exploration-findings-initial.md` | Canonical | Design research |
| `docs/explore-alarm-behavior-directive.md` | `docs/design/explore-alarm-behavior-directive.md` | Canonical | Design research |
| `docs/improve-alarm-directives.md` | `docs/design/improve-alarm-directives.md` | Canonical | Design research |
| `docs/plugin-behavior-exploration-template.md` | `docs/design/plugin-behavior-exploration-template.md` | Canonical | Design template |
---
## Deployment Documentation (Keep in `docs/`)
| Original Path | New Path | Status | Notes |
|--------------|----------|--------|-------|
| `DEPLOYMENT_CHECKLIST.md` | `docs/DEPLOYMENT_CHECKLIST.md` | Canonical | Move to docs/ |
| `DEPLOYMENT_SUMMARY.md` | `docs/DEPLOYMENT_SUMMARY.md` | Canonical | Move to docs/ |
| `docs/deployment-guide.md` | `docs/DEPLOYMENT_GUIDE.md` | Canonical | Primary deployment guide |
---
## Feature-Specific Documentation (Keep in `docs/`)
| Original Path | New Path | Status | Notes |
|--------------|----------|--------|-------|
| `docs/CROSS_PLATFORM_STORAGE_PATTERN.md` | `docs/CROSS_PLATFORM_STORAGE_PATTERN.md` | Canonical | Keep as-is |
| `docs/DATABASE_INTERFACES.md` | `docs/DATABASE_INTERFACES.md` | Canonical | Keep as-is |
| `docs/DATABASE_INTERFACES_IMPLEMENTATION.md` | `docs/DATABASE_INTERFACES_IMPLEMENTATION.md` | Canonical | Keep as-is |
| `docs/NATIVE_FETCHER_CONFIGURATION.md` | `docs/NATIVE_FETCHER_CONFIGURATION.md` | Canonical | Keep as-is |
| `docs/platform-capability-reference.md` | `docs/platform-capability-reference.md` | Canonical | Keep as-is |
| `docs/plugin-requirements-implementation.md` | `docs/plugin-requirements-implementation.md` | Canonical | Keep as-is |
| `docs/prefetch-scheduling-diagnosis.md` | `docs/prefetch-scheduling-diagnosis.md` | Canonical | Keep as-is |
| `docs/prefetch-scheduling-trace.md` | `docs/prefetch-scheduling-trace.md` | Canonical | Keep as-is |
| `docs/app-startup-recovery-solution.md` | `docs/app-startup-recovery-solution.md` | Canonical | Keep as-is |
| `docs/getting-valid-plan-ids.md` | `docs/getting-valid-plan-ids.md` | Canonical | Keep as-is |
| `docs/host-request-configuration.md` | `docs/host-request-configuration.md` | Canonical | Keep as-is |
| `docs/hydrate-plan-implementation-guide.md` | `docs/hydrate-plan-implementation-guide.md` | Canonical | Keep as-is |
| `docs/user-zero-stars-implementation.md` | `docs/user-zero-stars-implementation.md` | Canonical | Keep as-is |
| `docs/accessibility-localization.md` | `docs/accessibility-localization.md` | Canonical | Keep as-is |
| `docs/legal-store-compliance.md` | `docs/legal-store-compliance.md` | Canonical | Keep as-is |
| `docs/observability-dashboards.md` | `docs/observability-dashboards.md` | Canonical | Keep as-is |
| `docs/file-organization-summary.md` | `docs/file-organization-summary.md` | Canonical | Keep as-is |
| `docs/capacitor-platform-service-clean-changes.md` | `docs/capacitor-platform-service-clean-changes.md` | Canonical | Keep as-is |
---
## Test App Documentation (Keep with Test Apps, Index in `docs/testing/`)
| Original Path | New Path | Status | Notes |
|--------------|----------|--------|-------|
| `test-apps/BUILD_PROCESS.md` | `test-apps/BUILD_PROCESS.md` | Canonical | Keep with test apps |
| `test-apps/android-test-app/docs/PHASE1_TEST0_GOLDEN.md` | `test-apps/android-test-app/docs/PHASE1_TEST0_GOLDEN.md` | Canonical | Keep with test apps |
| `test-apps/android-test-app/docs/PHASE1_TEST1_GOLDEN.md` | `test-apps/android-test-app/docs/PHASE1_TEST1_GOLDEN.md` | Canonical | Keep with test apps |
| `test-apps/daily-notification-test/docs/BUILD_QUICK_REFERENCE.md` | `test-apps/daily-notification-test/docs/BUILD_QUICK_REFERENCE.md` | Canonical | Keep with test apps |
| `test-apps/daily-notification-test/docs/NOTIFICATION_STACK_IMPROVEMENT_PLAN.md` | `test-apps/daily-notification-test/docs/NOTIFICATION_STACK_IMPROVEMENT_PLAN.md` | Canonical | Keep with test apps |
| `test-apps/daily-notification-test/docs/PLUGIN_DETECTION_GUIDE.md` | `test-apps/daily-notification-test/docs/PLUGIN_DETECTION_GUIDE.md` | Canonical | Keep with test apps |
| `test-apps/daily-notification-test/docs/VUE3_NOTIFICATION_IMPLEMENTATION_GUIDE.md` | `test-apps/daily-notification-test/docs/VUE3_NOTIFICATION_IMPLEMENTATION_GUIDE.md` | Canonical | Keep with test apps |
| `test-apps/daily-notification-test/IMPLEMENTATION_COMPLETE.md` | `test-apps/daily-notification-test/IMPLEMENTATION_COMPLETE.md` | Canonical | Keep with test apps |
| `test-apps/daily-notification-test/INVESTIGATION_JWT_ALGORITHM.md` | `test-apps/daily-notification-test/INVESTIGATION_JWT_ALGORITHM.md` | Canonical | Keep with test apps |
| `test-apps/daily-notification-test/INVESTIGATION_JWT_ALGORITHM_RESULTS.md` | `test-apps/daily-notification-test/INVESTIGATION_JWT_ALGORITHM_RESULTS.md` | Canonical | Keep with test apps |
| `test-apps/daily-notification-test/README.md` | `test-apps/daily-notification-test/README.md` | Canonical | Keep with test apps |
| `test-apps/daily-notification-test/TODO_NATIVE_FETCHER.md` | `test-apps/daily-notification-test/TODO_NATIVE_FETCHER.md` | Canonical | Keep with test apps |
| `test-apps/ios-test-app/BUILD_NOTES.md` | `test-apps/ios-test-app/BUILD_NOTES.md` | Canonical | Keep with test apps |
| `test-apps/ios-test-app/BUILD_SUCCESS.md` | `test-apps/ios-test-app/BUILD_SUCCESS.md` | Canonical | Keep with test apps |
| `test-apps/ios-test-app/COMPILATION_FIXES.md` | `test-apps/ios-test-app/COMPILATION_FIXES.md` | Canonical | Keep with test apps |
| `test-apps/ios-test-app/COMPILATION_STATUS.md` | `test-apps/ios-test-app/COMPILATION_STATUS.md` | Canonical | Keep with test apps |
| `test-apps/ios-test-app/COMPILATION_SUMMARY.md` | `test-apps/ios-test-app/COMPILATION_SUMMARY.md` | Canonical | Keep with test apps |
| `test-apps/ios-test-app/README.md` | `test-apps/ios-test-app/README.md` | Canonical | Keep with test apps |
| `test-apps/ios-test-app/SETUP_COMPLETE.md` | `test-apps/ios-test-app/SETUP_COMPLETE.md` | Canonical | Keep with test apps |
| `test-apps/ios-test-app/SETUP_STATUS.md` | `test-apps/ios-test-app/SETUP_STATUS.md` | Canonical | Keep with test apps |
---
## Plugin-Specific Documentation (Keep in `ios/Plugin/`)
| Original Path | New Path | Status | Notes |
|--------------|----------|--------|-------|
| `ios/Plugin/README.md` | `ios/Plugin/README.md` | Canonical | Keep with plugin code |
---
## Cursor Rules Documentation (Keep in `.cursor/rules/`)
| Original Path | New Path | Status | Notes |
|--------------|----------|--------|-------|
| `.cursor/rules/README.md` | `.cursor/rules/README.md` | Canonical | Keep with cursor rules |
| `.cursor/rules/architecture/README.md` | `.cursor/rules/architecture/README.md` | Canonical | Keep with cursor rules |
| `.cursor/rules/meta_rule_architecture.md` | `.cursor/rules/meta_rule_architecture.md` | Canonical | Keep with cursor rules |
---
## Summary Statistics
- **Total Files:** 139
- **Canonical (Active):** ~95 files
- **Merged:** ~15 files
- **Archived:** ~29 files
---
## Verification Checklist
- [ ] All 139 files have a destination
- [ ] No file is marked for deletion
- [ ] All merged content is traceable
- [ ] Archive structure preserves original paths
- [ ] Index references all canonical files
- [ ] README.md links to docs/00-INDEX.md
---
**Last Updated:** 2025-12-16
**Status:** Complete - Ready for Implementation

View File

@@ -0,0 +1,697 @@
# iOS Core Data Migration Guide: Android Room → iOS Core Data
**Author**: Matthew Raymer
**Date**: 2025-12-08
**Status**: 🎯 **ACTIVE** - Database Migration Reference
**Version**: 1.0.0
**Last Synced With Plugin Version**: v1.1.0
## Purpose
This document provides a comprehensive mapping guide for migrating Android Room database entities to iOS Core Data entities, ensuring cross-platform data consistency and feature parity.
**Reference**:
- [Android Database Schema](../android/src/main/java/com/timesafari/dailynotification/DatabaseSchema.kt) - Android Room schema
- [iOS Core Data Model](../ios/Plugin/DailyNotificationModel.xcdatamodeld) - iOS Core Data model
- [Database Consolidation Plan](../android/DATABASE_CONSOLIDATION_PLAN.md) - Unified schema design
---
## 1. Entity Mapping Overview
### 1.1 Complete Entity Mapping
| Android Room Entity | iOS Core Data Entity | Status | Priority |
| ------------------- | -------------------- | ------ | -------- |
| `ContentCache` | `ContentCache` | ✅ Implemented | - |
| `Schedule` | `Schedule` | ✅ Implemented | - |
| `Callback` | `Callback` | ✅ Implemented | - |
| `History` | `History` | ✅ Implemented | - |
| `NotificationContentEntity` | `NotificationContent` | ❌ Missing | **High** |
| `NotificationDeliveryEntity` | `NotificationDelivery` | ❌ Missing | **High** |
| `NotificationConfigEntity` | `NotificationConfig` | ❌ Missing | **Medium** |
### 1.2 Current Implementation Status
**✅ Implemented (4 entities)**:
- `ContentCache` - Fetched content with TTL
- `Schedule` - Recurring schedule patterns
- `Callback` - Callback configurations
- `History` - Execution history
**❌ Missing (3 entities)**:
- `NotificationContent` - Specific notification instances
- `NotificationDelivery` - Delivery tracking/analytics
- `NotificationConfig` - Configuration management
---
## 2. Detailed Entity Mappings
### 2.1 ContentCache Entity
**Status**: ✅ **Implemented**
**Android Room**:
```kotlin
@Entity(tableName = "content_cache")
data class ContentCache(
@PrimaryKey val id: String,
val fetchedAt: Long, // epoch ms
val ttlSeconds: Int,
val payload: ByteArray, // BLOB
val meta: String? = null
)
```
**iOS Core Data**:
```swift
@objc(ContentCache)
public class ContentCache: NSManagedObject {
@NSManaged public var id: String?
@NSManaged public var fetchedAt: Date?
@NSManaged public var ttlSeconds: Int32
@NSManaged public var payload: Data?
@NSManaged public var meta: String?
}
```
**Mapping Notes**:
- ✅ All fields mapped correctly
-`Long` (epoch ms) → `Date` conversion handled
-`ByteArray``Data` conversion handled
- ✅ Optional fields properly marked
**Migration Status**: ✅ **Complete**
---
### 2.2 Schedule Entity
**Status**: ✅ **Implemented**
**Android Room**:
```kotlin
@Entity(tableName = "schedules")
data class Schedule(
@PrimaryKey val id: String,
val kind: String, // 'fetch' or 'notify'
val cron: String? = null,
val clockTime: String? = null, // HH:mm
val enabled: Boolean = true,
val lastRunAt: Long? = null, // epoch ms
val nextRunAt: Long? = null, // epoch ms
val jitterMs: Int = 0,
val backoffPolicy: String = "exp",
val stateJson: String? = null
)
```
**iOS Core Data**:
```swift
@objc(Schedule)
public class Schedule: NSManagedObject {
@NSManaged public var id: String?
@NSManaged public var kind: String?
@NSManaged public var cron: String?
@NSManaged public var clockTime: String?
@NSManaged public var enabled: Bool
@NSManaged public var lastRunAt: Date?
@NSManaged public var nextRunAt: Date?
@NSManaged public var jitterMs: Int32
@NSManaged public var backoffPolicy: String?
@NSManaged public var stateJson: String?
}
```
**Mapping Notes**:
- ✅ All fields mapped correctly
-`Long` (epoch ms) → `Date` conversion handled
- ✅ Default values preserved
- ✅ Optional fields properly marked
**Migration Status**: ✅ **Complete**
---
### 2.3 Callback Entity
**Status**: ✅ **Implemented**
**Android Room**:
```kotlin
@Entity(tableName = "callbacks")
data class Callback(
@PrimaryKey val id: String,
val kind: String, // 'http', 'local', 'queue'
val target: String,
val headersJson: String? = null,
val enabled: Boolean = true,
val createdAt: Long // epoch ms
)
```
**iOS Core Data**:
```swift
@objc(Callback)
public class Callback: NSManagedObject {
@NSManaged public var id: String?
@NSManaged public var kind: String?
@NSManaged public var target: String?
@NSManaged public var headersJson: String?
@NSManaged public var enabled: Bool
@NSManaged public var createdAt: Date?
}
```
**Mapping Notes**:
- ✅ All fields mapped correctly
-`Long` (epoch ms) → `Date` conversion handled
- ✅ Optional fields properly marked
**Migration Status**: ✅ **Complete**
---
### 2.4 History Entity
**Status**: ✅ **Implemented**
**Android Room**:
```kotlin
@Entity(tableName = "history")
data class History(
@PrimaryKey(autoGenerate = true) val id: Int = 0,
val refId: String,
val kind: String, // fetch/notify/callback
val occurredAt: Long, // epoch ms
val durationMs: Long? = null,
val outcome: String,
val diagJson: String? = null
)
```
**iOS Core Data**:
```swift
@objc(History)
public class History: NSManagedObject {
@NSManaged public var id: String?
@NSManaged public var refId: String?
@NSManaged public var kind: String?
@NSManaged public var occurredAt: Date?
@NSManaged public var durationMs: Int32
@NSManaged public var outcome: String?
@NSManaged public var diagJson: String?
}
```
**Mapping Notes**:
- ⚠️ `id` type differs: Android uses `Int` (auto-generated), iOS uses `String`
-`Long` (epoch ms) → `Date` conversion handled
- ✅ Optional fields properly marked
**Migration Consideration**: iOS uses `String` for `id` instead of auto-generated `Int`. This is acceptable as long as IDs are generated as UUIDs.
**Migration Status**: ✅ **Complete** (with note)
---
### 2.5 NotificationContent Entity
**Status**: ❌ **Missing - High Priority**
**Android Room**:
```kotlin
@Entity(tableName = "notification_content")
data class NotificationContentEntity(
@PrimaryKey val id: String,
val pluginVersion: String?,
val timesafariDid: String?,
val notificationType: String?,
val title: String?,
val body: String?,
val scheduledTime: Long, // epoch ms
val timezone: String?,
val priority: Int,
val vibrationEnabled: Boolean,
val soundEnabled: Boolean,
val mediaUrl: String?,
val encryptedContent: String?,
val encryptionKeyId: String?,
val createdAt: Long, // epoch ms
val updatedAt: Long, // epoch ms
val ttlSeconds: Long,
val deliveryStatus: String?,
val deliveryAttempts: Int,
val lastDeliveryAttempt: Long, // epoch ms
val userInteractionCount: Int,
val lastUserInteraction: Long, // epoch ms
val metadata: String?
)
```
**Required iOS Core Data Entity**:
```swift
@objc(NotificationContent)
public class NotificationContent: NSManagedObject {
@NSManaged public var id: String?
@NSManaged public var pluginVersion: String?
@NSManaged public var timesafariDid: String?
@NSManaged public var notificationType: String?
@NSManaged public var title: String?
@NSManaged public var body: String?
@NSManaged public var scheduledTime: Date?
@NSManaged public var timezone: String?
@NSManaged public var priority: Int32
@NSManaged public var vibrationEnabled: Bool
@NSManaged public var soundEnabled: Bool
@NSManaged public var mediaUrl: String?
@NSManaged public var encryptedContent: String?
@NSManaged public var encryptionKeyId: String?
@NSManaged public var createdAt: Date?
@NSManaged public var updatedAt: Date?
@NSManaged public var ttlSeconds: Int64
@NSManaged public var deliveryStatus: String?
@NSManaged public var deliveryAttempts: Int32
@NSManaged public var lastDeliveryAttempt: Date?
@NSManaged public var userInteractionCount: Int32
@NSManaged public var lastUserInteraction: Date?
@NSManaged public var metadata: String?
}
```
**Required Core Data Model XML**:
```xml
<entity name="NotificationContent" representedClassName="NotificationContent" syncable="YES" codeGenerationType="class">
<attribute name="id" optional="NO" attributeType="String"/>
<attribute name="pluginVersion" optional="YES" attributeType="String"/>
<attribute name="timesafariDid" optional="YES" attributeType="String"/>
<attribute name="notificationType" optional="YES" attributeType="String"/>
<attribute name="title" optional="YES" attributeType="String"/>
<attribute name="body" optional="YES" attributeType="String"/>
<attribute name="scheduledTime" optional="NO" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="timezone" optional="YES" attributeType="String"/>
<attribute name="priority" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="vibrationEnabled" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="soundEnabled" optional="YES" attributeType="Boolean" defaultValueString="YES" usesScalarValueType="YES"/>
<attribute name="mediaUrl" optional="YES" attributeType="String"/>
<attribute name="encryptedContent" optional="YES" attributeType="String"/>
<attribute name="encryptionKeyId" optional="YES" attributeType="String"/>
<attribute name="createdAt" optional="NO" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="updatedAt" optional="NO" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="ttlSeconds" optional="YES" attributeType="Integer 64" defaultValueString="604800" usesScalarValueType="YES"/>
<attribute name="deliveryStatus" optional="YES" attributeType="String"/>
<attribute name="deliveryAttempts" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="lastDeliveryAttempt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="userInteractionCount" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="lastUserInteraction" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="metadata" optional="YES" attributeType="String"/>
<index name="index_notification_content_timesafari_did">
<indexElement value="timesafariDid"/>
</index>
<index name="index_notification_content_notification_type">
<indexElement value="notificationType"/>
</index>
<index name="index_notification_content_scheduled_time">
<indexElement value="scheduledTime"/>
</index>
</entity>
```
**Mapping Notes**:
- `Long` (epoch ms) → `Date` conversion required
- `Int``Int32` conversion
- `Long` (ttlSeconds) → `Int64` conversion
- Indexes should be added for performance
**Migration Status**: ❌ **Not Implemented**
---
### 2.6 NotificationDelivery Entity
**Status**: ❌ **Missing - High Priority**
**Android Room**:
```kotlin
@Entity(
tableName = "notification_delivery",
foreignKeys = @ForeignKey(
entity = NotificationContentEntity::class,
parentColumns = ["id"],
childColumns = ["notification_id"],
onDelete = ForeignKey.CASCADE
)
)
data class NotificationDeliveryEntity(
@PrimaryKey val id: String,
val notificationId: String,
val timesafariDid: String?,
val deliveryTimestamp: Long, // epoch ms
val deliveryStatus: String?,
val deliveryMethod: String?,
val deliveryAttemptNumber: Int,
val deliveryDurationMs: Long,
val userInteractionType: String?,
val userInteractionTimestamp: Long, // epoch ms
val userInteractionDurationMs: Long,
val errorCode: String?,
val errorMessage: String?,
val deviceInfo: String?,
val networkInfo: String?,
val batteryLevel: Int,
val dozeModeActive: Boolean,
val exactAlarmPermission: Boolean,
val notificationPermission: Boolean,
val metadata: String?
)
```
**Required iOS Core Data Entity**:
```swift
@objc(NotificationDelivery)
public class NotificationDelivery: NSManagedObject {
@NSManaged public var id: String?
@NSManaged public var notificationId: String?
@NSManaged public var notificationContent: NotificationContent? // Relationship
@NSManaged public var timesafariDid: String?
@NSManaged public var deliveryTimestamp: Date?
@NSManaged public var deliveryStatus: String?
@NSManaged public var deliveryMethod: String?
@NSManaged public var deliveryAttemptNumber: Int32
@NSManaged public var deliveryDurationMs: Int64
@NSManaged public var userInteractionType: String?
@NSManaged public var userInteractionTimestamp: Date?
@NSManaged public var userInteractionDurationMs: Int64
@NSManaged public var errorCode: String?
@NSManaged public var errorMessage: String?
@NSManaged public var deviceInfo: String?
@NSManaged public var networkInfo: String?
@NSManaged public var batteryLevel: Int32
@NSManaged public var dozeModeActive: Bool
@NSManaged public var exactAlarmPermission: Bool
@NSManaged public var notificationPermission: Bool
@NSManaged public var metadata: String?
}
```
**Required Core Data Model XML**:
```xml
<entity name="NotificationDelivery" representedClassName="NotificationDelivery" syncable="YES" codeGenerationType="class">
<attribute name="id" optional="NO" attributeType="String"/>
<attribute name="notificationId" optional="YES" attributeType="String"/>
<attribute name="timesafariDid" optional="YES" attributeType="String"/>
<attribute name="deliveryTimestamp" optional="NO" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="deliveryStatus" optional="YES" attributeType="String"/>
<attribute name="deliveryMethod" optional="YES" attributeType="String"/>
<attribute name="deliveryAttemptNumber" optional="YES" attributeType="Integer 32" defaultValueString="1" usesScalarValueType="YES"/>
<attribute name="deliveryDurationMs" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="userInteractionType" optional="YES" attributeType="String"/>
<attribute name="userInteractionTimestamp" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="userInteractionDurationMs" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="errorCode" optional="YES" attributeType="String"/>
<attribute name="errorMessage" optional="YES" attributeType="String"/>
<attribute name="deviceInfo" optional="YES" attributeType="String"/>
<attribute name="networkInfo" optional="YES" attributeType="String"/>
<attribute name="batteryLevel" optional="YES" attributeType="Integer 32" defaultValueString="-1" usesScalarValueType="YES"/>
<attribute name="dozeModeActive" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="exactAlarmPermission" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="notificationPermission" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="metadata" optional="YES" attributeType="String"/>
<relationship name="notificationContent" optional="YES" maxCount="1" deletionRule="Cascade" destinationEntity="NotificationContent" inverseName="deliveries" inverseEntity="NotificationContent"/>
<index name="index_notification_delivery_notification_id">
<indexElement value="notificationId"/>
</index>
<index name="index_notification_delivery_delivery_timestamp">
<indexElement value="deliveryTimestamp"/>
</index>
</entity>
```
**Mapping Notes**:
- Foreign key relationship should be modeled as Core Data relationship
- `Long` (epoch ms) → `Date` conversion required
- `Int``Int32` conversion
- `Long` (duration) → `Int64` conversion
- Cascade delete should be configured
**Migration Status**: ❌ **Not Implemented**
---
### 2.7 NotificationConfig Entity
**Status**: ❌ **Missing - Medium Priority**
**Android Room**:
```kotlin
@Entity(tableName = "notification_config")
data class NotificationConfigEntity(
@PrimaryKey val id: String,
val timesafariDid: String?,
val configType: String?,
val configKey: String?,
val configValue: String?,
val configDataType: String?,
val isEncrypted: Boolean,
val encryptionKeyId: String?,
val createdAt: Long, // epoch ms
val updatedAt: Long, // epoch ms
val ttlSeconds: Long,
val isActive: Boolean,
val metadata: String?
)
```
**Required iOS Core Data Entity**:
```swift
@objc(NotificationConfig)
public class NotificationConfig: NSManagedObject {
@NSManaged public var id: String?
@NSManaged public var timesafariDid: String?
@NSManaged public var configType: String?
@NSManaged public var configKey: String?
@NSManaged public var configValue: String?
@NSManaged public var configDataType: String?
@NSManaged public var isEncrypted: Bool
@NSManaged public var encryptionKeyId: String?
@NSManaged public var createdAt: Date?
@NSManaged public var updatedAt: Date?
@NSManaged public var ttlSeconds: Int64
@NSManaged public var isActive: Bool
@NSManaged public var metadata: String?
}
```
**Mapping Notes**:
- `Long` (epoch ms) → `Date` conversion required
- `Long` (ttlSeconds) → `Int64` conversion
- Indexes should be added for performance
**Migration Status**: ❌ **Not Implemented**
---
## 3. Data Type Conversions
### 3.1 Time Conversions
**Android → iOS**:
- `Long` (epoch milliseconds) → `Date`
- Conversion: `Date(timeIntervalSince1970: Double(milliseconds) / 1000.0)`
**iOS → Android**:
- `Date``Long` (epoch milliseconds)
- Conversion: `Int64(date.timeIntervalSince1970 * 1000)`
### 3.2 Numeric Conversions
| Android Type | iOS Type | Notes |
| ------------ | -------- | ----- |
| `Int` | `Int32` | Direct mapping |
| `Long` | `Int64` | For large values |
| `Boolean` | `Bool` | Direct mapping |
| `ByteArray` | `Data` | Binary data |
### 3.3 String Conversions
- `String?``String?` (direct mapping)
- JSON fields: `String?``String?` (parse as needed)
---
## 4. Index Mapping
### 4.1 Required Indexes
**NotificationContent**:
- `timesafariDid` (for user queries)
- `notificationType` (for type filtering)
- `scheduledTime` (for time-based queries)
- `createdAt` (for chronological queries)
**NotificationDelivery**:
- `notificationId` (for foreign key lookups)
- `deliveryTimestamp` (for time-based queries)
- `deliveryStatus` (for status filtering)
- `timesafariDid` (for user queries)
**NotificationConfig**:
- `timesafariDid` (for user queries)
- `configType` (for type filtering)
- `updatedAt` (for chronological queries)
---
## 5. Relationship Mapping
### 5.1 Foreign Key Relationships
**Android Room**:
```kotlin
@ForeignKey(
entity = NotificationContentEntity::class,
parentColumns = ["id"],
childColumns = ["notification_id"],
onDelete = ForeignKey.CASCADE
)
```
**iOS Core Data**:
```xml
<relationship
name="notificationContent"
optional="YES"
maxCount="1"
deletionRule="Cascade"
destinationEntity="NotificationContent"
inverseName="deliveries"
inverseEntity="NotificationContent"/>
```
**Inverse Relationship** (NotificationContent → NotificationDelivery):
```xml
<relationship
name="deliveries"
optional="YES"
toMany="YES"
deletionRule="Nullify"
destinationEntity="NotificationDelivery"
inverseName="notificationContent"
inverseEntity="NotificationDelivery"/>
```
---
## 6. Implementation Checklist
### 6.1 High Priority (Required for Feature Parity)
- [ ] Add `NotificationContent` entity to Core Data model
- [ ] Add `NotificationDelivery` entity to Core Data model
- [ ] Configure foreign key relationship between `NotificationContent` and `NotificationDelivery`
- [ ] Add required indexes for performance
- [ ] Implement Swift extensions for entity classes
- [ ] Add data conversion helpers (Date ↔ Long)
- [ ] Test entity creation and relationships
- [ ] Test cascade delete behavior
### 6.2 Medium Priority (Configuration Management)
- [ ] Add `NotificationConfig` entity to Core Data model
- [ ] Add required indexes
- [ ] Implement Swift extensions
- [ ] Test configuration CRUD operations
### 6.3 Low Priority (Optimization)
- [ ] Add migration policies for schema changes
- [ ] Add data validation rules
- [ ] Optimize fetch requests with predicates
- [ ] Add batch operations support
---
## 7. Migration Steps
### 7.1 Step 1: Update Core Data Model
1. Open `DailyNotificationModel.xcdatamodeld` in Xcode
2. Add `NotificationContent` entity with all attributes
3. Add `NotificationDelivery` entity with all attributes
4. Add `NotificationConfig` entity with all attributes
5. Configure relationships between entities
6. Add indexes for performance
7. Set code generation to "Class Definition"
### 7.2 Step 2: Create Swift Extensions
1. Create `NotificationContent+CoreDataClass.swift`
2. Create `NotificationContent+CoreDataProperties.swift`
3. Create `NotificationDelivery+CoreDataClass.swift`
4. Create `NotificationDelivery+CoreDataProperties.swift`
5. Create `NotificationConfig+CoreDataClass.swift`
6. Create `NotificationConfig+CoreDataProperties.swift`
### 7.3 Step 3: Implement Data Access Layer
1. Create DAO classes for each entity
2. Implement CRUD operations
3. Add data conversion helpers
4. Add query methods with predicates
### 7.4 Step 4: Update Persistence Controller
1. Update `PersistenceController` to handle new entities
2. Add migration policies if needed
3. Test database initialization
### 7.5 Step 5: Testing
1. Test entity creation
2. Test relationships
3. Test cascade delete
4. Test data conversion (Date ↔ Long)
5. Test query performance with indexes
---
## 8. Data Migration (If Needed)
### 8.1 Migration from SQLite to Core Data
If migrating existing SQLite data to Core Data:
1. Read data from SQLite database
2. Convert data types (Long → Date, etc.)
3. Create Core Data entities
4. Save to Core Data store
5. Verify data integrity
### 8.2 Migration Script Example
```swift
func migrateSQLiteToCoreData(sqlitePath: String, coreDataStack: NSPersistentContainer) {
// 1. Open SQLite database
// 2. Query all tables
// 3. Convert each row to Core Data entity
// 4. Save to Core Data store
// 5. Verify migration success
}
```
---
## 9. References
- [Android Database Schema](../android/src/main/java/com/timesafari/dailynotification/DatabaseSchema.kt)
- [iOS Core Data Model](../ios/Plugin/DailyNotificationModel.xcdatamodeld)
- [Database Consolidation Plan](../android/DATABASE_CONSOLIDATION_PLAN.md)
- [Core Data Programming Guide](https://developer.apple.com/documentation/coredata)
---
**Document Version**: 1.0.0
**Last Updated**: 2025-12-08
**Next Review**: After missing entities are implemented

View File

@@ -1,7 +1,7 @@
# iOS Implementation Documentation Review
**Author**: Matthew Raymer
**Date**: December 2024
**Date**: 2025-12-08
**Status**: 🎯 **ACTIVE** - Documentation Review for iOS Implementation
**Purpose**: Ensure Android plugin and test app documentation contains sufficient detail for iOS implementation to mirror all features
@@ -653,6 +653,6 @@ The Android plugin and test app documentation is **comprehensive and well-struct
---
**Document Version**: 1.0.0
**Last Updated**: December 2024
**Last Updated**: 2025-12-08
**Next Review**: After iOS implementation begins

View File

@@ -0,0 +1,395 @@
# iOS Implementation Directive: App Launch Recovery & Missed Notification Detection
**Author**: Matthew Raymer
**Date**: 2025-12-08
**Status**: Active Implementation Directive - iOS Only
**Version**: 1.0.0
**Last Synced With Plugin Version**: v1.1.0
## Purpose
This directive provides **descriptive overview and integration guidance** for iOS-specific recovery and missed notification detection:
1. App Launch Recovery (cold/warm/terminated)
2. Missed Notification Detection
3. App Termination Detection
4. Background Task Registration for Boot Recovery
**⚠️ CRITICAL**: This document is **descriptive and integrative**. The **normative implementation instructions** are in the Phase 13 directives below. **If any code or behavior in this file conflicts with a Phase directive, the Phase directive wins.**
**Reference**: See [Plugin Requirements](./alarms/03-plugin-requirements.md) for requirements that Phase directives implement.
**Reference**: See [Platform Capability Reference](./alarms/01-platform-capability-reference.md) for iOS OS-level facts.
**⚠️ IMPORTANT**: For implementation, use the phase-specific directives (these are the canonical source of truth):
- **[Phase 1: Cold Start Recovery](./ios-implementation-directive-phase1.md)** - Minimal viable recovery
- Implements: [Plugin Requirements §3.1.2](./alarms/03-plugin-requirements.md#312-app-cold-start) (iOS equivalent)
- Explicit acceptance criteria, rollback safety, data integrity checks
- **Start here** for fastest implementation
- **[Phase 2: App Termination Detection & Recovery](./ios-implementation-directive-phase2.md)** - Comprehensive termination handling
- Implements: iOS-specific app termination scenarios
- Prerequisite: Phase 1 complete
- **[Phase 3: Background Task Registration & Boot Recovery](./ios-implementation-directive-phase3.md)** - Background task enhancement
- Implements: BGTaskScheduler registration for boot recovery
- Prerequisites: Phase 1 and Phase 2 complete
**See Also**: [Unified Alarm Directive](./alarms/000-UNIFIED-ALARM-DIRECTIVE.md) for master coordination document.
---
## 1. Implementation Overview
### 1.1 What Needs to Be Implemented
| Feature | Status | Priority | Location |
| ------- | ------ | -------- | -------- |
| App Launch Recovery | ❌ Missing | **High** | `DailyNotificationPlugin.swift` - `load()` method |
| Missed Notification Detection | ⚠️ Partial | **High** | `DailyNotificationPlugin.swift` - new method |
| App Termination Detection | ❌ Missing | **High** | `DailyNotificationPlugin.swift` - recovery logic |
| Background Task Registration | ⚠️ Partial | **Medium** | `AppDelegate.swift` - BGTaskScheduler registration |
### 1.2 Implementation Strategy
**Phase 1** Cold start recovery only
- Missed notification detection + future notification verification
- No termination detection, no boot handling
- **See [Phase 1 directive](./ios-implementation-directive-phase1.md) for implementation**
**Phase 2** App termination detection & full recovery
- Termination detection via UNUserNotificationCenter state comparison
- Comprehensive recovery of all schedules (notify + fetch)
- Past notifications marked as missed, future notifications rescheduled
- **See [Phase 2 directive](./ios-implementation-directive-phase2.md) for implementation**
**Phase 3** Background task registration & boot recovery
- BGTaskScheduler registration for boot recovery
- Next occurrence rescheduled for repeating schedules
- **See [Phase 3 directive](./ios-implementation-directive-phase3.md) for implementation**
---
## 2. iOS-Specific Considerations
### 2.1 Key Differences from Android
**iOS Advantages**:
- ✅ Notifications persist across app termination (OS-guaranteed)
- ✅ Notifications persist across device reboot (OS-guaranteed)
- ✅ No force stop equivalent (iOS doesn't have user-facing force stop)
**iOS Challenges**:
- ❌ App code does NOT run when notification fires (only if user taps)
- ❌ Background execution severely limited (BGTaskScheduler only)
- ❌ Cannot rely on background execution for recovery
- ❌ Must detect missed notifications on app launch
**Platform Reference**: See [Platform Capability Reference §3](./alarms/01-platform-capability-reference.md#3-ios-notification-capability-matrix) for complete iOS behavior matrix.
### 2.2 Recovery Scenario Mapping
**Android → iOS Mapping**:
| Android Scenario | iOS Equivalent | Detection Method |
| ---------------- | -------------- | --------------- |
| `COLD_START` | App Launch After Termination | Check if notifications exist vs DB state |
| `FORCE_STOP` | App Terminated by System | Check if notifications missing vs DB state |
| `BOOT` | Device Reboot | BGTaskScheduler registration (Phase 3) |
| `WARM_START` | App Resume (Foreground) | Check app state on resume |
**Note**: iOS doesn't have a user-facing "force stop" equivalent. System termination is detected by comparing UNUserNotificationCenter state with database state.
### 2.3 iOS APIs Used
**Notification Management**:
- `UNUserNotificationCenter.current()` - Notification center
- `UNUserNotificationCenter.getPendingNotificationRequests()` - Check scheduled notifications
- `UNUserNotificationCenter.add()` - Schedule notifications
**Background Tasks**:
- `BGTaskScheduler.shared` - Background task scheduler
- `BGTaskScheduler.register()` - Register background task handlers
- `BGAppRefreshTaskRequest` - Background fetch requests
**App Lifecycle**:
- `applicationWillTerminate` - App termination notification
- `applicationDidBecomeActive` - App foreground notification
- `applicationDidEnterBackground` - App background notification
---
## 3. Implementation: ReactivationManager (iOS)
**⚠️ Illustrative only** See Phase 1 and Phase 2 directives for canonical implementation.
**ReactivationManager Responsibilities by Phase**:
| Phase | Responsibilities |
| ----- | ---------------- |
| 1 | Cold start only (missed detection + verify/reschedule future) |
| 2 | Adds termination detection & recovery |
| 3 | Background task registration & boot recovery |
**For implementation details, see**:
- [Phase 1: ReactivationManager creation](./ios-implementation-directive-phase1.md#2-implementation-reactivationmanager)
- [Phase 2: Termination detection](./ios-implementation-directive-phase2.md#2-implementation-termination-detection)
### 3.1 Create New File
**File**: `ios/Plugin/DailyNotificationReactivationManager.swift`
**Purpose**: Centralized recovery logic for app launch scenarios
### 3.2 Class Structure
**⚠️ Illustrative only** See Phase 1 for canonical implementation.
```swift
import Foundation
import UserNotifications
/**
* Manages recovery of notifications on app launch
* Handles cold start, warm start, and termination recovery scenarios
*
* @author Matthew Raymer
* @version 1.0.0
*/
class DailyNotificationReactivationManager {
private static let TAG = "DNP-REACTIVATION"
private let notificationCenter = UNUserNotificationCenter.current()
private let database: DailyNotificationDatabase
private let storage: DailyNotificationStorage
init(database: DailyNotificationDatabase, storage: DailyNotificationStorage) {
self.database = database
self.storage = storage
}
/**
* Perform recovery on app launch
* Detects scenario (cold/warm/termination) and handles accordingly
*/
func performRecovery() async {
do {
NSLog("\(Self.TAG): Starting app launch recovery")
// Step 1: Detect scenario
let scenario = try await detectScenario()
NSLog("\(Self.TAG): Detected scenario: \(scenario)")
// Step 2: Handle based on scenario
switch scenario {
case .termination:
try await handleTerminationRecovery()
case .coldStart:
try await handleColdStartRecovery()
case .warmStart:
try await handleWarmStartRecovery()
case .none:
NSLog("\(Self.TAG): No recovery needed")
}
NSLog("\(Self.TAG): App launch recovery completed")
} catch {
NSLog("\(Self.TAG): Error during app launch recovery: \(error)")
}
}
// ... implementation methods below ...
}
```
---
## 4. Recovery Scenario Detection
### 4.1 Scenario Detection Algorithm
**Platform Reference**: [iOS §3.1.1](./alarms/01-platform-capability-reference.md#311-notifications-survive-app-termination) - Notifications survive app termination
**Detection Logic**:
```swift
enum RecoveryScenario {
case none // No recovery needed (first launch or warm resume)
case coldStart // App launched after termination, notifications may exist
case termination // App terminated, notifications missing vs DB
case warmStart // App resumed from background (optimization only)
}
func detectScenario() async throws -> RecoveryScenario {
// Step 1: Check if database has schedules
let schedules = try database.getEnabledSchedules()
if schedules.isEmpty {
return .none // First launch
}
// Step 2: Get pending notifications from UNUserNotificationCenter
let pendingNotifications = try await notificationCenter.pendingNotificationRequests()
// Step 3: Compare DB state with notification center state
let dbNotificationIds = Set(schedules.flatMap { $0.getScheduledNotificationIds() })
let pendingIds = Set(pendingNotifications.map { $0.identifier })
// Step 4: Determine scenario
if pendingIds.isEmpty && !dbNotificationIds.isEmpty {
// DB has schedules but no notifications scheduled
return .termination
} else if !pendingIds.isEmpty && !dbNotificationIds.isEmpty {
// Both have data - check if they match
if dbNotificationIds != pendingIds {
return .coldStart // Mismatch indicates recovery needed
} else {
return .warmStart // Match indicates warm resume
}
}
return .none
}
```
**For complete implementation, see**: [Phase 1 directive](./ios-implementation-directive-phase1.md#3-scenario-detection)
---
## 5. Missed Notification Detection
### 5.1 Detection Logic
**Platform Reference**: [iOS §3.2.1](./alarms/01-platform-capability-reference.md#321-app-code-does-not-run-when-notification-fires) - App code does not run when notification fires
**iOS Behavior**: When a notification fires, the app code does NOT execute. The notification is displayed, but the app must detect missed notifications on the next app launch.
**Detection Steps**:
1. Query database for notifications with `scheduled_time < currentTime`
2. Filter for notifications with `delivery_status != 'delivered'`
3. Mark as `'missed'` in database
4. Record in history table
**For complete implementation, see**: [Phase 1 directive](./ios-implementation-directive-phase1.md#4-missed-notification-detection)
---
## 6. Background Task Registration
### 6.1 BGTaskScheduler Registration
**Platform Reference**: [iOS §3.1.3](./alarms/01-platform-capability-reference.md#313-background-tasks-for-prefetching) - Background tasks for prefetching
**iOS Limitation**: BGTaskScheduler cannot be used for critical scheduling. It's system-controlled and not guaranteed.
**Use Case**: BGTaskScheduler is used for:
- Prefetching content (not critical timing)
- Boot recovery (system may defer)
- Background maintenance (best effort)
**Registration Location**: `AppDelegate.swift` or `SceneDelegate.swift`
**For complete implementation, see**: [Phase 3 directive](./ios-implementation-directive-phase3.md#2-background-task-registration)
---
## 7. Testing Strategy
### 7.1 iOS Testing Tools
**Simulator Testing**:
- `xcrun simctl` - Simulator control
- Xcode Instruments - Performance profiling
- Console.app - System log viewing
**Device Testing**:
- Xcode Device Console - Real device logs
- Settings → Developer → Background App Refresh - Control background execution
### 7.2 Test Scenarios
**Phase 1 Tests**:
- Cold start recovery
- Missed notification detection
- Future notification verification
**Phase 2 Tests**:
- App termination detection
- Comprehensive recovery
- Multiple schedules recovery
**Phase 3 Tests**:
- Background task registration
- Boot recovery (simulated)
- Background task execution
**For complete test procedures, see**: [iOS Test Scripts](../test-apps/ios-test-app/test-phase1.sh)
---
## 8. Platform-Specific Notes
### 8.1 Notification Persistence
**iOS Advantage**: Notifications persist automatically across:
- App termination
- Device reboot (for calendar/time triggers)
**App Responsibility**: Must still:
- Detect missed notifications on app launch
- Reschedule future notifications if needed
- Track delivery status in database
### 8.2 Background Execution Limits
**iOS Limitation**: Background execution is severely limited:
- BGTaskScheduler is system-controlled
- Cannot rely on background execution for recovery
- Must handle recovery on app launch
**Workaround**: Use BGTaskScheduler for prefetching only, not for critical scheduling.
### 8.3 Timing Tolerance
**iOS Limitation**: Calendar-based notifications have ±180 second tolerance.
**Impact**: Notifications may fire up to 3 minutes early or late.
**Mitigation**: Account for tolerance in missed notification detection logic.
---
## 9. Next Steps
1. **Start with Phase 1**: Implement cold start recovery
- See [Phase 1 directive](./ios-implementation-directive-phase1.md)
- Focus on missed notification detection
- Verify future notifications are scheduled
2. **Proceed to Phase 2**: Add termination detection
- See [Phase 2 directive](./ios-implementation-directive-phase2.md)
- Implement comprehensive recovery
- Handle multiple schedules
3. **Complete Phase 3**: Background task registration
- See [Phase 3 directive](./ios-implementation-directive-phase3.md)
- Register BGTaskScheduler handlers
- Implement boot recovery
---
## 10. References
- [Platform Capability Reference](./alarms/01-platform-capability-reference.md) - iOS OS-level facts
- [Plugin Requirements](./alarms/03-plugin-requirements.md) - Requirements this directive implements
- [Android Implementation Directive](./android-implementation-directive.md) - Android equivalent for comparison
- [iOS Recovery Scenario Mapping](./ios-recovery-scenario-mapping.md) - Detailed scenario mapping
- [iOS Core Data Migration Guide](./ios-core-data-migration.md) - Database migration guide
---
**Document Version**: 1.0.0
**Last Updated**: 2025-12-08
**Next Review**: After Phase 1 implementation

View File

@@ -0,0 +1,487 @@
# iOS Implementation Checklist
**Author**: Matthew Raymer
**Date**: 2025-12-08
**Status**: 🎯 **ACTIVE** - Implementation Tracking
**Version**: 1.0.0
## Purpose
Complete checklist of iOS code that needs to be implemented for feature parity with Android. This checklist tracks all implementation tasks with checkboxes.
**Reference**:
- [iOS Implementation Directive](./ios-implementation-directive.md) - Implementation guide
- [iOS Recovery Scenario Mapping](./ios-recovery-scenario-mapping.md) - Scenario details
- [iOS Core Data Migration Guide](./ios-core-data-migration.md) - Database entities
---
## Phase 1: Cold Start Recovery (High Priority)
### 1.1 Create ReactivationManager
- [x] Create new file: `ios/Plugin/DailyNotificationReactivationManager.swift`
- [x] Implement class structure with properties:
- [x] `notificationCenter: UNUserNotificationCenter`
- [x] `database: DailyNotificationDatabase`
- [x] `storage: DailyNotificationStorage`
- [x] `scheduler: DailyNotificationScheduler`
- [x] `TAG: String = "DNP-REACTIVATION"`
- [x] Implement `init(database:storage:scheduler:)` initializer
- [x] Implement `performRecovery()` async method
- [x] Add timeout protection (2 seconds max)
- [x] Add error handling (non-fatal, log only)
### 1.2 Scenario Detection
- [x] Create `RecoveryScenario` enum:
- [x] `.none` - No recovery needed
- [x] `.coldStart` - App launched after termination
- [x] `.termination` - App terminated, notifications missing
- [x] `.warmStart` - App resumed (optimization)
- [x] Implement `detectScenario() async throws -> RecoveryScenario`:
- [x] Check if database has notifications (empty → `.none`)
- [x] Get pending notifications from `UNUserNotificationCenter`
- [x] Compare DB state with notification center state
- [x] Return appropriate scenario
### 1.3 Cold Start Recovery Logic
- [x] Implement `performColdStartRecovery() async throws -> RecoveryResult`:
- [x] Detect missed notifications (scheduled_time < now, not delivered)
- [x] Mark missed notifications in database (Phase 1: basic marking, Phase 2: add delivery_status)
- [x] Update `last_delivery_attempt` timestamp (Phase 2: add property)
- [x] Record in history table (Phase 1: logging only, Phase 2: database recording)
- [x] Verify future notifications are scheduled
- [x] Reschedule missing future notifications
- [x] Return `RecoveryResult` with counts
### 1.4 Missed Notification Detection
- [x] Implement `detectMissedNotifications() async throws -> [NotificationContent]`:
- [x] Query storage for notifications with `scheduled_time < currentTime`
- [x] Filter for missed notifications (Phase 1: time-based only, Phase 2: add delivery_status check)
- [x] Return list of missed notifications
- [x] Implement `markMissedNotification(_:) async throws`:
- [x] Mark notification as missed (Phase 1: basic, Phase 2: add delivery_status property)
- [x] Update notification in storage
- [x] Record status change (Phase 1: logging, Phase 2: history table)
### 1.5 Future Notification Verification
- [x] Implement `verifyFutureNotifications() async throws -> VerificationResult`:
- [x] Get all future notifications from storage
- [x] Get pending notifications from `UNUserNotificationCenter`
- [x] Compare notification IDs
- [x] Identify missing notifications
- [x] Return verification result
- [x] Implement `rescheduleMissingNotification(id:) async throws`:
- [x] For each missing notification, reschedule using `DailyNotificationScheduler`
- [x] Verify no duplicates created (scheduler handles this)
- [x] Log rescheduling activity
### 1.6 Recovery Result Types
- [x] Create `RecoveryResult` struct:
- [x] `missedCount: Int`
- [x] `rescheduledCount: Int`
- [x] `verifiedCount: Int`
- [x] `errors: Int`
- [x] Create `VerificationResult` struct:
- [x] `totalSchedules: Int`
- [x] `notificationsFound: Int`
- [x] `notificationsMissing: Int`
- [x] `missingIds: [String]`
### 1.7 Integration with Plugin
- [x] Add `reactivationManager` property to `DailyNotificationPlugin`
- [x] Initialize `ReactivationManager` in `load()` method
- [x] Call `performRecovery()` in `load()` method (async, non-blocking)
- [x] Add logging with `DNP-REACTIVATION` tag
- [x] Ensure recovery doesn't block app startup (Task-based async execution)
### 1.8 History Recording
- [x] Implement `recordRecoveryHistory(_:scenario:)` method:
- [x] Record recovery execution (Phase 1: logging with JSON, Phase 2: database table)
- [x] Include scenario, counts, outcome
- [x] Add diagnostic JSON with details
- [x] Implement `recordRecoveryFailure(_:)` method:
- [x] Record recovery errors (Phase 1: logging, Phase 2: database table)
- [x] Include error message and error type
### 1.9 Testing
- [x] Unit tests for scenario detection
- [x] Unit tests for missed notification detection
- [x] Unit tests for future notification verification
- [x] Unit tests for boot detection
- [x] Unit tests for recovery result types
- [ ] Integration test for full recovery flow
- [ ] Manual test with test scripts (`test-phase1.sh`)
---
## Phase 2: App Termination Detection (High Priority)
### 2.1 Termination Detection Logic
- [x] Enhance `detectScenario()` to detect termination:
- [x] Check if DB has notifications but no pending notifications
- [x] Return `.termination` scenario
- [x] Implement `handleTerminationRecovery() async throws`:
- [x] Detect all missed notifications
- [x] Mark all as missed
- [x] Reschedule all future notifications
- [x] Reschedule all fetch schedules (if applicable)
### 2.2 Comprehensive Recovery
- [x] Implement `performFullRecovery() async throws -> RecoveryResult`:
- [x] Handle all notifications (missed and future)
- [x] Reschedule all missing notifications
- [x] Batch operations for efficiency
- [x] Return comprehensive result
### 2.3 Multiple Schedules Recovery
- [x] Implement recovery for multiple schedules:
- [x] Handle multiple notifications (batch processing)
- [x] Batch operations for efficiency (single pending request query)
- [x] Handle partial failures gracefully (continue on error)
- [x] Separate missed vs future notifications for batch processing
### 2.4 Testing
- [ ] Test termination detection accuracy
- [ ] Test full recovery with multiple schedules
- [ ] Test partial failure scenarios
- [ ] Manual test with test scripts (`test-phase2.sh`)
---
## Phase 3: Background Task Registration & Boot Recovery (Medium Priority)
### 3.1 BGTaskScheduler Registration
- [x] Verify `BGTaskScheduler` registration in `DailyNotificationPlugin.setupBackgroundTasks()`:
- [x] Check `fetchTaskIdentifier` registration (already implemented)
- [x] Check `notifyTaskIdentifier` registration (already implemented)
- [x] Add verification method `verifyBGTaskRegistration()` in ReactivationManager
- [x] Implement boot detection:
- [x] Check system uptime on app launch
- [x] Compare with last launch time (stored in UserDefaults)
- [x] Detect if boot occurred recently (< 60 seconds threshold)
### 3.2 Boot Recovery Logic
- [x] Implement `performBootRecovery() async throws`:
- [x] Detect all missed notifications (past scheduled times)
- [x] Mark all as missed
- [x] Reschedule all future notifications
- [x] Record boot recovery in history
### 3.3 Background Task Handlers
- [x] Enhance `handleBackgroundFetch` in `DailyNotificationPlugin.swift`:
- [x] Add recovery logic if needed (verification of scheduled notifications)
- [x] Schedule next background task (using getNextScheduledNotificationTime)
- [x] Handle expiration gracefully (enhanced expiration handler with cleanup)
- [x] Enhance `handleBackgroundNotify`:
- [x] Add recovery logic if needed (verification of scheduled notifications)
- [x] Schedule next background task (helper method added)
### 3.4 Testing
- [ ] Test BGTaskScheduler registration
- [ ] Test boot detection (simulate or manual)
- [ ] Test boot recovery logic
- [ ] Manual test with test scripts (`test-phase3.sh`)
---
## Core Data Entities (High Priority)
### 4.1 NotificationContent Entity
- [x] Update `DailyNotificationModel.xcdatamodeld`:
- [x] Add `NotificationContent` entity
- [x] Add all 23 attributes (id, pluginVersion, timesafariDid, etc.)
- [x] Set correct attribute types (String, Date, Int32, Int64, Bool)
- [x] Add default values where specified
- [x] Mark required vs optional attributes
- [x] Add indexes:
- [x] `timesafariDid` index
- [x] `notificationType` index
- [x] `scheduledTime` index
- [x] Note: Core Data auto-generates class files with `codeGenerationType="class"`
- [ ] Implement data conversion helpers (if needed):
- [ ] `Date``Long` (epoch milliseconds) conversion helpers
- [ ] `Int64``Long` conversion helpers
### 4.2 NotificationDelivery Entity
- [x] Update `DailyNotificationModel.xcdatamodeld`:
- [x] Add `NotificationDelivery` entity
- [x] Add all 20 attributes
- [x] Set correct attribute types
- [x] Add default values
- [x] Configure relationship:
- [x] Add `notificationContent` relationship (to-one)
- [x] Set deletion rule to `Nullify` (Core Data handles cascade via inverse)
- [x] Add inverse relationship `deliveries` (to-many) on `NotificationContent`
- [x] Add indexes:
- [x] `notificationId` index
- [x] `deliveryTimestamp` index
- [x] Note: Core Data auto-generates class files
### 4.3 NotificationConfig Entity
- [x] Update `DailyNotificationModel.xcdatamodeld`:
- [x] Add `NotificationConfig` entity
- [x] Add all 13 attributes
- [x] Set correct attribute types
- [x] Add default values
- [x] Add indexes:
- [x] `configKey` index
- [x] `configType` index
- [x] `timesafariDid` index
- [x] Note: Core Data auto-generates class files
### 4.4 Data Access Layer
- [x] Create DAO classes or extensions:
- [x] `NotificationContentDAO` or extension methods
- [x] `NotificationDeliveryDAO` or extension methods
- [x] `NotificationConfigDAO` or extension methods
- [x] Implement CRUD operations:
- [x] Create/Insert methods
- [x] Read/Query methods with predicates
- [x] Update methods
- [x] Delete methods
- [x] Implement query helpers:
- [x] Query by timesafariDid
- [x] Query by notificationType
- [x] Query by scheduledTime range
- [x] Query by deliveryStatus
### 4.5 Persistence Controller Updates
- [x] Update `PersistenceController` (if exists) or create:
- [x] Handle new entities in initialization
- [x] Add migration policies if needed
- [x] Test database initialization (unit tests verify Core Data stack)
- [x] Test Core Data stack:
- [x] Entity creation (tested in DAO unit tests)
- [x] Relationships (tested in NotificationDeliveryDAOTests)
- [x] Cascade delete (tested in NotificationDeliveryDAOTests)
- [x] Data conversion (tested in DailyNotificationDataConversionsTests)
---
## API Methods (Medium Priority)
### 5.1 Notification Permission Methods
- [x] Implement `getNotificationPermissionStatus()`:
- [x] Query `UNUserNotificationCenter.current().getNotificationSettings()`
- [x] Map to `NotificationPermissionStatus` type
- [x] Return authorization status
- [x] Implement `requestNotificationPermission()`:
- [x] Request authorization via `UNUserNotificationCenter`
- [x] Handle user response
- [x] Return `{ granted: boolean }`
- [x] Implement `openNotificationSettings()`:
- [x] Open iOS Settings app to notification settings
- [x] Use `UIApplication.shared.open()` with settings URL
### 5.2 Background Task Methods
- [x] Implement `getBackgroundTaskStatus()`:
- [x] Check BGTaskScheduler registration
- [x] Check Background App Refresh status (cannot check programmatically, return null)
- [x] Return `BackgroundTaskStatus` object
- [x] Implement `openBackgroundAppRefreshSettings()`:
- [x] Open iOS Settings app to Background App Refresh
- [x] Use `UIApplication.shared.open()` with settings URL
### 5.3 Pending Notifications Method
- [x] Implement `getPendingNotifications()`:
- [x] Query `UNUserNotificationCenter.current().getPendingNotificationRequests()`
- [x] Map to `PendingNotification[]` array
- [x] Return count and notification details
- [x] Add to `pluginMethods` array in `DailyNotificationPlugin`
### 5.4 Register Methods in Plugin
- [x] Add methods to `pluginMethods` array:
- [x] `getNotificationPermissionStatus`
- [x] `requestNotificationPermission`
- [x] `getPendingNotifications`
- [x] `getBackgroundTaskStatus`
- [x] `openNotificationSettings`
- [x] `openBackgroundAppRefreshSettings`
---
## Data Type Conversions (High Priority)
### 6.1 Time Conversions
- [x] Create helper functions:
- [x] `dateFromEpochMillis(_: Int64) -> Date`
- [x] `epochMillisFromDate(_: Date) -> Int64`
- [x] Use in all Core Data operations:
- [x] When reading from database (Long → Date)
- [x] When writing to database (Date → Long)
### 6.2 Numeric Conversions
- [x] Ensure correct type mappings:
- [x] `Int``Int32` for small integers
- [x] `Long``Int64` for large integers
- [x] `Boolean``Bool` (direct)
### 6.3 String Conversions
- [x] Handle optional strings correctly:
- [x] `String?` in Swift maps to optional in Core Data
- [x] JSON fields stored as `String?`
---
## Logging & Observability (Medium Priority)
### 7.1 Recovery Logging
- [x] Add comprehensive logging:
- [x] `DNP-REACTIVATION: Starting app launch recovery`
- [x] `DNP-REACTIVATION: Detected scenario: [scenario]`
- [x] `DNP-REACTIVATION: Missed notifications detected: [count]`
- [x] `DNP-REACTIVATION: Future notifications verified: [count]`
- [x] `DNP-REACTIVATION: Recovery completed: [result]`
- [x] Add error logging:
- [x] `DNP-REACTIVATION: Recovery failed (non-fatal): [error]`
- [x] Include error details and stack trace (NSError domain, code, userInfo)
### 7.2 Metrics Recording
- [x] Record recovery metrics in history table:
- [x] Recovery execution time (tracked with startTime/endTime)
- [x] Missed notification count
- [x] Rescheduled notification count
- [x] Error count
- [x] Add diagnostic JSON to history entries (via HistoryDAO.recordRecovery)
---
## Error Handling (High Priority)
### 8.1 Recovery Error Handling
- [x] Ensure all recovery methods catch errors:
- [x] Database errors (non-fatal) - handled in detectScenario, detectMissedNotifications, verifyFutureNotifications
- [x] Notification center errors (non-fatal) - handled in detectScenario, verifyFutureNotifications
- [x] Scheduling errors (non-fatal) - handled in rescheduleMissingNotification
- [x] Log errors but don't crash app - all errors logged with NSLog, app continues
- [x] Return partial results if some operations fail - RecoveryResult includes error count
### 8.2 Error Types
- [x] Define iOS-specific error codes:
- [x] `NOTIFICATION_PERMISSION_DENIED`
- [x] `BACKGROUND_REFRESH_DISABLED`
- [x] `PENDING_NOTIFICATION_LIMIT_EXCEEDED`
- [x] `BG_TASK_NOT_REGISTERED`
- [x] `BG_TASK_EXECUTION_FAILED`
- [x] Map to error responses in plugin methods - getNotificationPermissionStatus uses NOTIFICATION_PERMISSION_DENIED
---
## Testing (High Priority)
### 9.1 Unit Tests
- [x] Test `ReactivationManager` initialization (DailyNotificationReactivationManagerTests)
- [x] Test scenario detection logic:
- [x] Test `.none` scenario (empty database)
- [x] Test `.coldStart` scenario
- [x] Test `.termination` scenario
- [x] Test `.warmStart` scenario
- [x] Test missed notification detection
- [x] Test future notification verification
- [x] Test recovery result creation
- [x] Test data conversions (DailyNotificationDataConversionsTests)
- [x] Test NotificationContentDAO (NotificationContentDAOTests)
- [x] Test NotificationDeliveryDAO (NotificationDeliveryDAOTests)
- [x] Test NotificationConfigDAO (NotificationConfigDAOTests)
### 9.2 Integration Tests
- [x] Test full recovery flow:
- [x] Schedule notification
- [x] Terminate app (simulated by clearing notifications)
- [x] Launch app (simulated by calling performRecovery)
- [x] Verify recovery executed
- [x] Verify notifications rescheduled (DailyNotificationRecoveryIntegrationTests)
- [x] Test error handling:
- [x] Test database errors (testErrorHandling_DatabaseError)
- [x] Test notification center errors (testErrorHandling_NotificationCenterError)
- [x] Verify app doesn't crash (all stability tests)
### 9.3 Manual Testing
- [ ] Run `test-phase1.sh` script
- [ ] Run `test-phase2.sh` script
- [ ] Run `test-phase3.sh` script
- [ ] Test on physical device (not just simulator)
- [ ] Test with Background App Refresh enabled/disabled
- [ ] Test with notification permission granted/denied
---
## Documentation Updates (Low Priority)
### 10.1 Code Documentation
- [x] Add file-level documentation to `DailyNotificationReactivationManager.swift`
- [x] Add method-level documentation to all public methods
- [x] Add parameter documentation (@param tags)
- [x] Add return value documentation (@return tags)
- [x] Add error documentation (@throws tags and error handling notes)
### 10.2 Implementation Status
- [x] Update `ios/Plugin/README.md` with implementation status
- [x] Mark completed features as ✅
- [x] Update version numbers (1.1.0)
- [x] Update "Last Updated" dates (2025-12-08)
---
## Summary
**Total Tasks**: ~150+ implementation tasks
**Priority Breakdown**:
- **High Priority**: ~80 tasks (Phase 1, Core Data, API methods, Error handling)
- **Medium Priority**: ~50 tasks (Phase 2, Phase 3, Logging)
- **Low Priority**: ~20 tasks (Documentation)
**Estimated Implementation Time**:
- Phase 1: 2-3 days
- Phase 2: 1-2 days
- Phase 3: 1 day
- Core Data: 2-3 days
- API Methods: 1 day
- Testing: 2-3 days
- **Total**: ~10-15 days
---
**Document Version**: 1.0.0
**Last Updated**: 2025-12-08
**Next Review**: After Phase 1 implementation

View File

@@ -0,0 +1,423 @@
# iOS Recovery Scenario Mapping: Android → iOS Equivalents
**Author**: Matthew Raymer
**Date**: 2025-12-08
**Status**: 🎯 **ACTIVE** - Recovery Scenario Mapping Reference
**Version**: 1.0.0
**Last Synced With Plugin Version**: v1.1.0
## Purpose
This document maps Android recovery scenarios to their iOS equivalents, providing a clear translation guide for implementing iOS recovery logic based on Android patterns.
**Reference**:
- [Android Implementation Directive](./android-implementation-directive.md) - Android scenarios
- [iOS Implementation Directive](./ios-implementation-directive.md) - iOS scenarios
- [Platform Capability Reference](./alarms/01-platform-capability-reference.md) - OS-level facts
---
## 1. Scenario Mapping Overview
### 1.1 Direct Mappings
| Android Scenario | iOS Equivalent | Detection Method | Recovery Action |
| ---------------- | -------------- | ---------------- | --------------- |
| `COLD_START` | App Launch After Termination | Compare UNUserNotificationCenter vs DB | Detect missed, verify future |
| `FORCE_STOP` | App Terminated by System | DB has schedules, no notifications | Full recovery of all schedules |
| `BOOT` | Device Reboot | BGTaskScheduler registration | Reschedule all notifications |
| `WARM_START` | App Resume (Foreground) | Notifications match DB state | No recovery needed (optimization) |
| `NONE` | First Launch / No Recovery | Empty database | No action needed |
### 1.2 Key Differences
**iOS Advantages**:
- ✅ Notifications persist across termination (OS-guaranteed)
- ✅ Notifications persist across reboot (OS-guaranteed)
- ❌ No user-facing "force stop" equivalent
**iOS Challenges**:
- ❌ App code does NOT run when notification fires
- ❌ Must detect missed notifications on app launch
- ❌ Background execution severely limited
---
## 2. Detailed Scenario Mappings
### 2.1 COLD_START → App Launch After Termination
**Android Definition**:
- Process killed, alarms may or may not exist
- Database still populated
- Alarms may have been cleared by OS
**iOS Equivalent**:
- App terminated by system or user
- Notifications may still exist (OS-guaranteed persistence)
- Database still populated
- Need to verify notification state matches database
**Detection Logic**:
**Android**:
```kotlin
// Check if alarms exist in AlarmManager
val alarmsExist = alarmManager.hasAlarm(pendingIntent)
if (alarmsExist && dbHasSchedules) {
return COLD_START
}
```
**iOS**:
```swift
// Check if notifications exist in UNUserNotificationCenter
let pendingNotifications = try await notificationCenter.pendingNotificationRequests()
let dbSchedules = try database.getEnabledSchedules()
if !pendingNotifications.isEmpty && !dbSchedules.isEmpty {
// Compare notification IDs with DB state
let dbIds = Set(dbSchedules.flatMap { $0.getScheduledNotificationIds() })
let pendingIds = Set(pendingNotifications.map { $0.identifier })
if dbIds != pendingIds {
return .coldStart // Mismatch indicates recovery needed
}
}
```
**Recovery Actions**:
1. Detect missed notifications (scheduled_time < now, not delivered)
2. Mark missed notifications in database
3. Verify future notifications are scheduled
4. Reschedule missing future notifications
**Platform Reference**: [iOS §3.1.1](./alarms/01-platform-capability-reference.md#311-notifications-survive-app-termination)
---
### 2.2 FORCE_STOP → App Terminated by System
**Android Definition**:
- User force-stopped app via Settings
- All alarms cleared
- Database still populated
- Boot receiver blocked until user launches app
**iOS Equivalent**:
- App terminated by system (low memory, etc.)
- Notifications may be missing (system cleared them)
- Database still populated
- No user-facing force stop equivalent
**Key Difference**: iOS doesn't have a user-facing "force stop" option. System termination is the closest equivalent.
**Detection Logic**:
**Android**:
```kotlin
// Check if alarms exist
val alarmsExist = alarmManager.hasAlarm(pendingIntent)
if (!alarmsExist && dbHasSchedules && !isBootRecent) {
return FORCE_STOP
}
```
**iOS**:
```swift
// Check if notifications exist
let pendingNotifications = try await notificationCenter.pendingNotificationRequests()
let dbSchedules = try database.getEnabledSchedules()
if pendingNotifications.isEmpty && !dbSchedules.isEmpty {
// DB has schedules but no notifications scheduled
return .termination
}
```
**Recovery Actions**:
1. Detect all missed notifications
2. Mark all missed notifications in database
3. Reschedule all future notifications
4. Reschedule all fetch schedules (if applicable)
**Platform Reference**: [iOS §3.2.1](./alarms/01-platform-capability-reference.md#321-app-code-does-not-run-when-notification-fires)
---
### 2.3 BOOT → Device Reboot
**Android Definition**:
- Device rebooted
- All alarms wiped (OS behavior)
- Database still populated
- Boot receiver executes after boot completes
**iOS Equivalent**:
- Device rebooted
- Notifications persist automatically (OS-guaranteed)
- Database still populated
- BGTaskScheduler may execute (system-controlled)
**Key Difference**: iOS automatically persists notifications across reboot. Android requires manual rescheduling.
**Detection Logic**:
**Android**:
```kotlin
// Check boot flag (set by BootReceiver)
val bootFlag = sharedPreferences.getLong("last_boot_time", 0)
val currentTime = System.currentTimeMillis()
if (bootFlag > 0 && (currentTime - bootFlag) < 60000) {
return BOOT
}
```
**iOS**:
```swift
// BGTaskScheduler registration handles boot
// Check if this is a boot-triggered background task
if isBootBackgroundTask {
return .boot
}
// Or detect on app launch after reboot
let lastLaunchTime = UserDefaults.standard.double(forKey: "last_launch_time")
let bootTime = ProcessInfo.processInfo.systemUptime
if lastLaunchTime > 0 && bootTime < 60 {
return .boot
}
```
**Recovery Actions**:
1. Verify notifications still exist (iOS usually handles this)
2. Detect any missed notifications during reboot window
3. Reschedule any missing notifications
4. Update next run times for repeating schedules
**Platform Reference**: [iOS §3.1.2](./alarms/01-platform-capability-reference.md#312-notifications-persist-across-device-reboot)
---
### 2.4 WARM_START → App Resume (Foreground)
**Android Definition**:
- App resumed from background
- Alarms still exist
- Database matches alarm state
- No recovery needed (optimization)
**iOS Equivalent**:
- App resumed from background
- Notifications still exist
- Database matches notification state
- No recovery needed (optimization)
**Detection Logic**:
**Android**:
```kotlin
// Check if alarms exist and match DB
val alarmsExist = alarmManager.hasAlarm(pendingIntent)
if (alarmsExist && dbMatchesAlarms) {
return WARM_START
}
```
**iOS**:
```swift
// Check if notifications exist and match DB
let pendingNotifications = try await notificationCenter.pendingNotificationRequests()
let dbSchedules = try database.getEnabledSchedules()
let dbIds = Set(dbSchedules.flatMap { $0.getScheduledNotificationIds() })
let pendingIds = Set(pendingNotifications.map { $0.identifier })
if dbIds == pendingIds {
return .warmStart // Match indicates warm resume
}
```
**Recovery Actions**:
- None (optimization only)
- May perform lightweight verification
- May update metrics
---
### 2.5 NONE → First Launch / No Recovery
**Android Definition**:
- First app launch
- Empty database
- No schedules configured
- No recovery needed
**iOS Equivalent**:
- First app launch
- Empty database
- No schedules configured
- No recovery needed
**Detection Logic**:
**Android**:
```kotlin
// Check if database is empty
val schedules = database.scheduleDao().getEnabled()
if (schedules.isEmpty()) {
return NONE
}
```
**iOS**:
```swift
// Check if database is empty
let schedules = try database.getEnabledSchedules()
if schedules.isEmpty {
return .none
}
```
**Recovery Actions**:
- None
---
## 3. Recovery Action Mapping
### 3.1 Missed Notification Detection
**Android**:
- Query AlarmManager for past alarms
- Check database for undelivered notifications
- Mark as missed in database
**iOS**:
- Query database for past scheduled notifications
- Check delivery status
- Mark as missed in database
**Key Difference**: iOS cannot query past notifications from UNUserNotificationCenter. Must rely on database state.
### 3.2 Future Notification Verification
**Android**:
- Query AlarmManager for future alarms
- Compare with database schedules
- Reschedule missing alarms
**iOS**:
- Query UNUserNotificationCenter for pending notifications
- Compare with database schedules
- Reschedule missing notifications
**Key Difference**: iOS uses UNUserNotificationCenter instead of AlarmManager.
### 3.3 Full Recovery
**Android**:
- Reschedule all notify schedules
- Reschedule all fetch schedules (WorkManager)
- Mark past notifications as missed
**iOS**:
- Reschedule all notify schedules
- Reschedule all fetch schedules (BGTaskScheduler)
- Mark past notifications as missed
**Key Difference**: iOS uses BGTaskScheduler instead of WorkManager.
---
## 4. Implementation Checklist
### 4.1 Phase 1: Cold Start Recovery
- [ ] Implement scenario detection (cold start)
- [ ] Implement missed notification detection
- [ ] Implement future notification verification
- [ ] Test cold start recovery
### 4.2 Phase 2: Termination Detection
- [ ] Implement termination detection
- [ ] Implement full recovery logic
- [ ] Test termination recovery
### 4.3 Phase 3: Boot Recovery
- [ ] Implement BGTaskScheduler registration
- [ ] Implement boot detection
- [ ] Test boot recovery
---
## 5. Platform-Specific Notes
### 5.1 iOS Advantages
1. **Notification Persistence**: iOS automatically persists notifications across termination and reboot
2. **No Force Stop**: iOS doesn't have user-facing force stop, reducing complexity
3. **Simplified Recovery**: Less recovery needed due to OS persistence
### 5.2 iOS Challenges
1. **No Code Execution on Fire**: App code doesn't run when notification fires
2. **Background Limits**: Severely limited background execution
3. **Timing Tolerance**: ±180 second tolerance for calendar triggers
### 5.3 Android Advantages
1. **Code Execution on Fire**: PendingIntent can execute code when alarm fires
2. **WorkManager**: More reliable background execution
3. **Exact Timing**: Can achieve exact timing with permission
### 5.4 Android Challenges
1. **No Persistence**: Alarms don't persist across reboot
2. **Force Stop**: Hard kill that cannot be bypassed
3. **Boot Recovery**: Must implement boot receiver
---
## 6. Testing Strategy
### 6.1 Scenario Testing
**Cold Start**:
1. Terminate app (swipe away)
2. Wait for notification time to pass
3. Launch app
4. Verify missed notification detection
5. Verify future notifications rescheduled
**Termination**:
1. Schedule notifications
2. Terminate app
3. Clear notifications (simulate system clearing)
4. Launch app
5. Verify full recovery
**Boot**:
1. Schedule notifications
2. Reboot device (or simulate)
3. Launch app
4. Verify notifications still exist
5. Verify any missed notifications detected
---
## 7. References
- [Android Implementation Directive](./android-implementation-directive.md) - Android scenarios
- [iOS Implementation Directive](./ios-implementation-directive.md) - iOS scenarios
- [Platform Capability Reference](./alarms/01-platform-capability-reference.md) - OS-level facts
- [Plugin Requirements](./alarms/03-plugin-requirements.md) - Requirements
---
**Document Version**: 1.0.0
**Last Updated**: 2025-12-08
**Next Review**: After Phase 1 implementation

View File

@@ -0,0 +1,649 @@
# iOS Rollover Implementation — Edge Case Handling Plan
**Status**: Planning Phase
**Priority**: Reliability-First
**Author**: AI Assistant
**Date**: 2025-01-27
## Objective
Implement Android-like automatic rollover for iOS notifications with comprehensive edge case handling to ensure reliability across all scenarios, including time changes, timezone changes, DST transitions, and race conditions.
---
## Edge Case Categories
### 1. **Time Changes**
- Manual clock adjustments (user changes device time)
- System clock corrections (NTP sync)
- Clock drift corrections
- Time jumps (forward/backward)
### 2. **Timezone Changes**
- User changes device timezone
- Automatic timezone detection changes
- Travel across timezones
- Timezone database updates
### 3. **DST Transitions**
- Spring forward (lose 1 hour)
- Fall back (gain 1 hour)
- DST rule changes
- Regions that don't observe DST
### 4. **Race Conditions**
- Multiple rollover attempts for same notification
- Concurrent scheduling operations
- App state transitions during rollover
- Background task conflicts
### 5. **System Events**
- Device reboots
- App termination
- Low memory conditions
- Background execution limits
### 6. **Notification System Edge Cases**
- Notification limit reached (64 pending)
- Notification delivery failures
- System notification queue issues
- Permission changes
---
## Detection Mechanisms
### A. Time Change Detection
**iOS Limitation**: iOS doesn't provide direct time change notifications like Android's `ACTION_TIME_CHANGED` broadcast.
**Solution**: Multi-layered detection:
1. **App Launch Detection**
- Store last known system time on app exit
- Compare on app launch
- Detect significant time jumps (>5 minutes)
2. **Background Task Detection**
- Store timestamp when scheduling notification
- Compare with current time when background task runs
- Detect time discrepancies
3. **Notification Delivery Detection**
- Compare scheduled time with actual delivery time
- Flag if delivery time is significantly different
4. **Periodic Validation**
- Background task validates scheduled notifications
- Checks if notification times are still valid
- Adjusts if time change detected
### B. Timezone Change Detection
**iOS Limitation**: No direct timezone change notification.
**Solution**:
1. **Store Timezone on Schedule**
- Save timezone identifier when scheduling
- Store as part of notification metadata
2. **Compare on Access**
- Check current timezone vs stored timezone
- Detect changes on app launch, background tasks, rollover
3. **Recalculate on Change**
- If timezone changed, recalculate all scheduled times
- Maintain same local time (e.g., 9:00 AM stays 9:00 AM)
### C. DST Transition Detection
**Solution**: Use Calendar API for DST-aware calculations:
1. **Calendar-Based Calculation**
- Use `Calendar.date(byAdding: .hour, value: 24, to:)`
- Automatically handles DST transitions
- No manual DST detection needed
2. **Validation After Calculation**
- Verify calculated time is exactly 24 hours later in local time
- Log DST transitions for debugging
- Handle edge cases (e.g., 2:00 AM → 3:00 AM spring forward)
### D. Duplicate Prevention
**Solution**: Multi-level idempotence checks:
1. **Database-Level Check**
- Store rollover state per notification ID
- Track last processed rollover time
- Prevent duplicate rollover attempts
2. **Storage-Level Check**
- Check for existing notifications at same scheduled time
- Use tolerance window (1 minute) for DST shifts
- Compare notification IDs and scheduled times
3. **System-Level Check**
- Query `UNUserNotificationCenter` for pending notifications
- Check if notification already scheduled
- Cancel and reschedule if needed
4. **Request-Level Check**
- Use unique notification IDs
- Include timestamp in ID generation
- Prevent ID collisions
---
## Handling Strategies
### Strategy 1: Time Change Handling
**When Detected**:
1. **Validate All Scheduled Notifications**
- Check if scheduled times are still valid
- Recalculate if time change was significant
- Cancel invalid notifications
2. **Recalculate Rollover Times**
- If time changed, recalculate next notification time
- Use DST-safe calculation
- Maintain same local time (e.g., 9:00 AM)
3. **Reschedule Affected Notifications**
- Cancel old notifications
- Schedule with corrected times
- Update storage with new times
4. **Log Time Change Event**
- Record time change in history
- Log old time, new time, delta
- Track which notifications were affected
**Implementation**:
```swift
func handleTimeChange(
lastKnownTime: Int64,
currentTime: Int64,
scheduledNotifications: [NotificationContent]
) async {
let timeDelta = abs(currentTime - lastKnownTime)
// Only handle significant time changes (>5 minutes)
guard timeDelta > (5 * 60 * 1000) else {
return // Ignore small clock adjustments
}
// Recalculate all scheduled notifications
for notification in scheduledNotifications {
// Recalculate using original scheduled time
let originalScheduledTime = notification.scheduledTime
let newScheduledTime = recalculateScheduledTime(
originalTime: originalScheduledTime,
timeDelta: timeDelta
)
// Cancel old notification
await scheduler.cancelNotification(id: notification.id)
// Reschedule with corrected time
let updatedNotification = NotificationContent(
id: notification.id,
title: notification.title,
body: notification.body,
scheduledTime: newScheduledTime,
fetchedAt: notification.fetchedAt,
url: notification.url,
payload: notification.payload,
etag: notification.etag
)
await scheduler.scheduleNotification(updatedNotification)
}
// Record time change in history
await recordTimeChangeEvent(
oldTime: lastKnownTime,
newTime: currentTime,
delta: timeDelta
)
}
```
### Strategy 2: Timezone Change Handling
**When Detected**:
1. **Detect Timezone Change**
- Compare current timezone with stored timezone
- Detect on app launch, background tasks, rollover
2. **Recalculate All Scheduled Times**
- Maintain same local time (e.g., 9:00 AM)
- Convert to new timezone
- Update scheduled times
3. **Reschedule All Notifications**
- Cancel existing notifications
- Schedule with new times
- Update storage
**Implementation**:
```swift
func handleTimezoneChange(
oldTimezone: TimeZone,
newTimezone: TimeZone,
scheduledNotifications: [NotificationContent]
) async {
// Extract local time from each notification
for notification in scheduledNotifications {
// Get local time components (hour, minute)
let scheduledDate = notification.getScheduledTimeAsDate()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: scheduledDate)
let minute = calendar.component(.minute, from: scheduledDate)
// Recalculate in new timezone
let newScheduledTime = calculateNextOccurrence(
hour: hour,
minute: minute,
timezone: newTimezone
)
// Cancel old notification
await scheduler.cancelNotification(id: notification.id)
// Reschedule with new time
let updatedNotification = NotificationContent(
id: notification.id,
title: notification.title,
body: notification.body,
scheduledTime: newScheduledTime,
fetchedAt: notification.fetchedAt,
url: notification.url,
payload: notification.payload,
etag: notification.etag
)
await scheduler.scheduleNotification(updatedNotification)
}
// Update stored timezone
await storage.saveTimezone(newTimezone.identifier)
}
```
### Strategy 3: DST Transition Handling
**When Detected**:
1. **Use Calendar API**
- `Calendar.date(byAdding: .hour, value: 24, to:)` handles DST automatically
- No manual DST detection needed
2. **Validate Calculation**
- Verify 24-hour addition results in correct local time
- Log DST transitions for debugging
- Handle edge cases (2:00 AM → 3:00 AM)
3. **Handle Edge Cases**
- Spring forward: Notification might be scheduled for 2:00 AM (doesn't exist)
- Fall back: Notification might be scheduled for 2:00 AM (occurs twice)
- Use system's automatic handling
**Implementation**:
```swift
func calculateNextScheduledTime(_ currentScheduledTime: Int64) -> Int64 {
let calendar = Calendar.current
let currentDate = Date(timeIntervalSince1970: Double(currentScheduledTime) / 1000.0)
// Add 24 hours (handles DST automatically)
guard let nextDate = calendar.date(byAdding: .hour, value: 24, to: currentDate) else {
// Fallback to simple addition
return currentScheduledTime + (24 * 60 * 60 * 1000)
}
// Validate: Ensure it's exactly 24 hours later in local time
let currentHour = calendar.component(.hour, from: currentDate)
let currentMinute = calendar.component(.minute, from: currentDate)
let nextHour = calendar.component(.hour, from: nextDate)
let nextMinute = calendar.component(.minute, from: nextDate)
// Log DST transitions
if currentHour != nextHour || currentMinute != nextMinute {
print("\(Self.TAG): DST transition detected: \(currentHour):\(currentMinute) -> \(nextHour):\(nextMinute)")
}
return Int64(nextDate.timeIntervalSince1970 * 1000)
}
```
### Strategy 4: Duplicate Prevention
**Multi-Level Checks**:
1. **Rollover State Tracking**
- Store rollover state in database
- Track last processed notification ID
- Prevent duplicate rollover attempts
2. **Time-Based Deduplication**
- Check for existing notifications at same scheduled time
- Use tolerance window (1 minute) for DST shifts
- Compare notification IDs
3. **System-Level Verification**
- Query `UNUserNotificationCenter` for pending notifications
- Check if notification already scheduled
- Cancel and reschedule if needed
**Implementation**:
```swift
func scheduleNextNotification(
_ content: NotificationContent,
storage: DailyNotificationStorage?,
fetcher: DailyNotificationFetcher? = nil
) async -> Bool {
// Check 1: Rollover state tracking
if let storage = storage {
let lastRolloverTime = await storage.getLastRolloverTime(for: content.id)
let currentTime = Int64(Date().timeIntervalSince1970 * 1000)
// If rollover was processed recently (< 1 hour ago), skip
if let lastTime = lastRolloverTime,
(currentTime - lastTime) < (60 * 60 * 1000) {
print("\(Self.TAG): RESCHEDULE_SKIP id=\(content.id) already_processed")
return false
}
}
// Calculate next time
let nextScheduledTime = calculateNextScheduledTime(content.scheduledTime)
// Check 2: Storage-level duplicate check
if let storage = storage {
let existingNotifications = storage.getAllNotifications()
let toleranceMs: Int64 = 60 * 1000 // 1 minute tolerance
for existing in existingNotifications {
if abs(existing.scheduledTime - nextScheduledTime) <= toleranceMs {
print("\(Self.TAG): RESCHEDULE_DUPLICATE id=\(content.id) existing_id=\(existing.id)")
return false
}
}
}
// Check 3: System-level duplicate check
let pendingNotifications = await notificationCenter.pendingNotificationRequests()
for pending in pendingNotifications {
if let trigger = pending.trigger as? UNCalendarNotificationTrigger,
let nextDate = trigger.nextTriggerDate() {
let pendingTime = Int64(nextDate.timeIntervalSince1970 * 1000)
let toleranceMs: Int64 = 60 * 1000
if abs(pendingTime - nextScheduledTime) <= toleranceMs {
print("\(Self.TAG): RESCHEDULE_DUPLICATE id=\(content.id) system_pending_id=\(pending.identifier)")
return false
}
}
}
// All checks passed, proceed with scheduling
// ... (rest of scheduling logic)
// Mark rollover as processed
await storage?.saveLastRolloverTime(for: content.id, time: Int64(Date().timeIntervalSince1970 * 1000))
return true
}
```
### Strategy 5: Race Condition Prevention
**Solution**: Use serial queue + state tracking
1. **Serial Queue for Rollover**
- Use dedicated serial queue for rollover operations
- Prevent concurrent rollover attempts
- Ensure atomic operations
2. **State Machine**
- Track rollover state (pending, processing, completed)
- Prevent duplicate processing
- Handle failures gracefully
3. **Locking Mechanism**
- Use actor or serial queue for thread safety
- Prevent race conditions
- Ensure atomic updates
**Implementation**:
```swift
actor RolloverCoordinator {
private var processingNotifications: Set<String> = []
private let scheduler: DailyNotificationScheduler
private let storage: DailyNotificationStorage
func processRollover(for notificationId: String) async -> Bool {
// Check if already processing
if processingNotifications.contains(notificationId) {
print("RolloverCoordinator: Already processing \(notificationId)")
return false
}
// Mark as processing
processingNotifications.insert(notificationId)
defer {
processingNotifications.remove(notificationId)
}
// Perform rollover
// ... (rollover logic)
return true
}
}
```
---
## Implementation Architecture
### Component 1: TimeChangeDetector
**Purpose**: Detect time changes and trigger recovery
**Responsibilities**:
- Store last known system time
- Compare on app launch/background tasks
- Detect significant time jumps
- Trigger time change recovery
**Location**: `ios/Plugin/DailyNotificationTimeChangeDetector.swift`
### Component 2: TimezoneChangeDetector
**Purpose**: Detect timezone changes and trigger recalculation
**Responsibilities**:
- Store current timezone
- Compare on access
- Detect timezone changes
- Trigger timezone change recovery
**Location**: `ios/Plugin/DailyNotificationTimezoneChangeDetector.swift`
### Component 3: RolloverCoordinator
**Purpose**: Coordinate rollover operations with duplicate prevention
**Responsibilities**:
- Manage rollover state
- Prevent duplicate rollovers
- Coordinate multiple detection mechanisms
- Handle race conditions
**Location**: `ios/Plugin/DailyNotificationRolloverCoordinator.swift`
### Component 4: Enhanced Recovery Manager
**Purpose**: Extend existing recovery manager with time/timezone change handling
**Responsibilities**:
- Integrate time change detection
- Integrate timezone change detection
- Coordinate with rollover coordinator
- Handle all edge cases
**Location**: `ios/Plugin/DailyNotificationReactivationManager.swift` (enhance existing)
---
## Testing Strategy
### Test Category 1: Time Changes
1. **Manual Clock Adjustment**
- Set device time forward 1 hour
- Verify notifications rescheduled correctly
- Verify rollover still works
2. **Clock Jump Forward**
- Set device time forward 24 hours
- Verify all notifications recalculated
- Verify no duplicates created
3. **Clock Jump Backward**
- Set device time backward 1 hour
- Verify notifications still valid
- Verify rollover works correctly
### Test Category 2: Timezone Changes
1. **Timezone Change**
- Change device timezone
- Verify notifications rescheduled to same local time
- Verify rollover maintains local time
2. **Travel Simulation**
- Change timezone multiple times
- Verify notifications always at correct local time
- Verify no duplicates
### Test Category 3: DST Transitions
1. **Spring Forward**
- Test on DST spring forward day
- Verify 24-hour calculation handles correctly
- Verify notification fires at correct time
2. **Fall Back**
- Test on DST fall back day
- Verify 24-hour calculation handles correctly
- Verify no duplicate notifications
### Test Category 4: Race Conditions
1. **Concurrent Rollover**
- Trigger multiple rollover attempts simultaneously
- Verify only one succeeds
- Verify no duplicates
2. **App State Transitions**
- Trigger rollover during app state changes
- Verify rollover completes correctly
- Verify no data corruption
### Test Category 5: Edge Cases
1. **Notification Limit**
- Schedule 64 notifications
- Verify rollover still works
- Verify proper error handling
2. **Permission Changes**
- Revoke notification permission
- Verify graceful failure
- Verify recovery when permission restored
---
## Implementation Phases
### Phase 1: Core Rollover (Week 1)
- ✅ DST-safe time calculation
- ✅ Basic rollover scheduling
- ✅ Duplicate prevention (storage + system level)
- ✅ AppDelegate integration
### Phase 2: Edge Case Detection (Week 2)
- ✅ Time change detection
- ✅ Timezone change detection
- ✅ Rollover state tracking
- ✅ Race condition prevention
### Phase 3: Recovery Integration (Week 3)
- ✅ Time change recovery
- ✅ Timezone change recovery
- ✅ Enhanced recovery manager
- ✅ Background task integration
### Phase 4: Testing & Validation (Week 4)
- ✅ Comprehensive edge case testing
- ✅ Real device testing
- ✅ DST transition testing
- ✅ Performance optimization
---
## Success Criteria
1. **Reliability**: 99%+ rollover success rate across all edge cases
2. **No Duplicates**: Zero duplicate notifications in any scenario
3. **Time Accuracy**: Notifications fire within 1 minute of scheduled time
4. **Recovery**: All edge cases handled gracefully with recovery
5. **Performance**: Rollover completes in <1 second
6. **Logging**: Comprehensive logging for debugging
---
## Risk Mitigation
### Risk 1: iOS Background Execution Limits
**Mitigation**: Multiple detection mechanisms (delegate + background + recovery)
### Risk 2: Time Change Detection Reliability
**Mitigation**: Store timestamps, compare on every access, validate scheduled times
### Risk 3: Race Conditions
**Mitigation**: Serial queue, state machine, actor-based coordination
### Risk 4: DST Edge Cases
**Mitigation**: Use Calendar API, validate calculations, comprehensive testing
### Risk 5: Notification System Limits
**Mitigation**: Check pending count, handle gracefully, provide user feedback
---
## Next Steps
1. **Review & Approve Plan** (This document)
2. **Create Implementation Tasks** (Break down into specific tasks)
3. **Implement Phase 1** (Core rollover functionality)
4. **Test Phase 1** (Basic functionality)
5. **Implement Phase 2** (Edge case detection)
6. **Test Phase 2** (Edge case scenarios)
7. **Implement Phase 3** (Recovery integration)
8. **Test Phase 3** (Recovery scenarios)
9. **Final Testing** (Comprehensive validation)
10. **Documentation** (Update docs with edge case handling)
---
## References
- Android Implementation: `DailyNotificationWorker.java` (scheduleNextNotification)
- Android Time Change Handling: `DailyNotificationRebootRecoveryManager.java`
- iOS Calendar API: `Calendar.date(byAdding:to:)` documentation
- iOS Background Tasks: `BGTaskScheduler` documentation
- iOS Notifications: `UNUserNotificationCenter` documentation

View File

@@ -0,0 +1,633 @@
# iOS Rollover Implementation — Comprehensive Review
**Status**: Pre-Implementation Review
**Date**: 2025-01-27
**Priority**: Reliability-First
---
## Table of Contents
1. [Plan Overview](#plan-overview)
2. [File Changes Summary](#file-changes-summary)
3. [Detailed File Modifications](#detailed-file-modifications)
4. [Integration Points](#integration-points)
5. [Dependencies & Order](#dependencies--order)
6. [Testing Strategy](#testing-strategy)
7. [Open Questions](#open-questions)
---
## Plan Overview
### Objective
Implement Android-like automatic rollover for iOS notifications with comprehensive edge case handling.
### Key Features
- ✅ Automatic rollover when notification fires (24 hours later)
- ✅ DST-safe time calculations
- ✅ Multi-level duplicate prevention
- ✅ Time/timezone change detection and recovery
- ✅ Race condition prevention
- ✅ Comprehensive edge case handling
### Architecture Components
1. **TimeChangeDetector** — Detects time changes
2. **TimezoneChangeDetector** — Detects timezone changes
3. **RolloverCoordinator** — Coordinates rollover operations
4. **Enhanced Recovery Manager** — Integrates all edge case handling
---
## File Changes Summary
| File | Change Type | Lines Added | Purpose |
|------|-------------|-------------|---------|
| `DailyNotificationScheduler.swift` | Add methods | ~150 | DST-safe calculation + rollover scheduling |
| `DailyNotificationPlugin.swift` | Add method | ~50 | Rollover handler entry point |
| `AppDelegate.swift` | Modify method | ~20 | Detect notification delivery (foreground) |
| `DailyNotificationReactivationManager.swift` | Enhance | ~100 | Rollover on app launch recovery |
| `DailyNotificationStorage.swift` | Add methods | ~30 | Rollover state tracking |
| `DailyNotificationTimeChangeDetector.swift` | New file | ~200 | Time change detection |
| `DailyNotificationTimezoneChangeDetector.swift` | New file | ~150 | Timezone change detection |
| `DailyNotificationRolloverCoordinator.swift` | New file | ~250 | Rollover coordination |
**Total**: ~950 lines of new/modified code
---
## Detailed File Modifications
### 1. DailyNotificationScheduler.swift
**Location**: `ios/Plugin/DailyNotificationScheduler.swift`
#### Change 1.1: Add DST-Safe Next Time Calculation
**Insert after line 307** (after `calculateNextOccurrence` method):
```swift
/**
* Calculate next scheduled time from current scheduled time (24 hours later, DST-safe)
*
* Matches Android calculateNextScheduledTime() functionality
* Handles DST transitions automatically using Calendar
*
* @param currentScheduledTime Current scheduled time in milliseconds
* @return Next scheduled time in milliseconds (24 hours later)
*/
func calculateNextScheduledTime(_ currentScheduledTime: Int64) -> Int64 {
let calendar = Calendar.current
let currentDate = Date(timeIntervalSince1970: Double(currentScheduledTime) / 1000.0)
// Add 24 hours (handles DST transitions automatically)
guard let nextDate = calendar.date(byAdding: .hour, value: 24, to: currentDate) else {
// Fallback to simple 24-hour addition if calendar calculation fails
print("\(Self.TAG): DST calculation failed, using fallback")
return currentScheduledTime + (24 * 60 * 60 * 1000)
}
// Validate: Log DST transitions for debugging
let currentHour = calendar.component(.hour, from: currentDate)
let currentMinute = calendar.component(.minute, from: currentDate)
let nextHour = calendar.component(.hour, from: nextDate)
let nextMinute = calendar.component(.minute, from: nextDate)
if currentHour != nextHour || currentMinute != nextMinute {
print("\(Self.TAG): DST transition detected: \(currentHour):\(String(format: "%02d", currentMinute)) -> \(nextHour):\(String(format: "%02d", nextMinute))")
}
return Int64(nextDate.timeIntervalSince1970 * 1000)
}
```
#### Change 1.2: Add Rollover Scheduling Method
**Insert after line 202** (after `scheduleNotification` method):
```swift
/**
* Schedule next notification after current one fires (rollover)
*
* Matches Android scheduleNextNotification() functionality
* Implements multi-level duplicate prevention
*
* @param content Current notification content that just fired
* @param storage Storage instance for duplicate checking
* @param fetcher Optional fetcher for scheduling prefetch
* @return true if next notification was scheduled successfully
*/
func scheduleNextNotification(
_ content: NotificationContent,
storage: DailyNotificationStorage?,
fetcher: DailyNotificationFetcher? = nil
) async -> Bool {
print("\(Self.TAG): RESCHEDULE_START id=\(content.id)")
// Check 1: Rollover state tracking (prevent duplicate rollover attempts)
if let storage = storage {
let lastRolloverTime = await storage.getLastRolloverTime(for: content.id)
let currentTime = Int64(Date().timeIntervalSince1970 * 1000)
// If rollover was processed recently (< 1 hour ago), skip
if let lastTime = lastRolloverTime,
(currentTime - lastTime) < (60 * 60 * 1000) {
print("\(Self.TAG): RESCHEDULE_SKIP id=\(content.id) already_processed")
return false
}
}
// Calculate next occurrence using DST-safe calculation
let nextScheduledTime = calculateNextScheduledTime(content.scheduledTime)
// Check 2: Storage-level duplicate check (prevent duplicate notifications)
if let storage = storage {
let existingNotifications = storage.getAllNotifications()
let toleranceMs: Int64 = 60 * 1000 // 1 minute tolerance for DST shifts
for existing in existingNotifications {
if abs(existing.scheduledTime - nextScheduledTime) <= toleranceMs {
print("\(Self.TAG): RESCHEDULE_DUPLICATE id=\(content.id) existing_id=\(existing.id) time_diff_ms=\(abs(existing.scheduledTime - nextScheduledTime))")
return false // Skip rescheduling to prevent duplicate
}
}
}
// Check 3: System-level duplicate check (query UNUserNotificationCenter)
let pendingNotifications = await notificationCenter.pendingNotificationRequests()
for pending in pendingNotifications {
if let trigger = pending.trigger as? UNCalendarNotificationTrigger,
let nextDate = trigger.nextTriggerDate() {
let pendingTime = Int64(nextDate.timeIntervalSince1970 * 1000)
let toleranceMs: Int64 = 60 * 1000
if abs(pendingTime - nextScheduledTime) <= toleranceMs {
print("\(Self.TAG): RESCHEDULE_DUPLICATE id=\(content.id) system_pending_id=\(pending.identifier)")
return false
}
}
}
// Extract hour:minute from current scheduled time for logging
let calendar = Calendar.current
let scheduledDate = content.getScheduledTimeAsDate()
let hour = calendar.component(.hour, from: scheduledDate)
let minute = calendar.component(.minute, from: scheduledDate)
// Create new notification content for next occurrence
// Note: Content will be refreshed by prefetch, but we need placeholder
let nextId = "daily_rollover_\(Int64(Date().timeIntervalSince1970 * 1000))"
let nextContent = NotificationContent(
id: nextId,
title: content.title, // Will be updated by prefetch
body: content.body, // Will be updated by prefetch
scheduledTime: nextScheduledTime,
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: content.url,
payload: content.payload,
etag: content.etag
)
// Schedule the next notification
let scheduled = await scheduleNotification(nextContent)
if scheduled {
let nextTimeStr = formatTime(nextScheduledTime)
print("\(Self.TAG): RESCHEDULE_OK id=\(content.id) next=\(nextTimeStr) nextId=\(nextId)")
// Schedule background fetch for next notification (5 minutes before scheduled time)
// Note: DailyNotificationFetcher integration deferred to Phase 2
if let fetcher = fetcher {
let fetchTime = nextScheduledTime - (5 * 60 * 1000) // 5 minutes before
let currentTime = Int64(Date().timeIntervalSince1970 * 1000)
if fetchTime > currentTime {
// TODO: Phase 2 - Implement fetcher.scheduleFetch(fetchTime)
print("\(Self.TAG): RESCHEDULE_PREFETCH_SCHEDULED id=\(content.id) next_fetch=\(fetchTime) next_notification=\(nextScheduledTime)")
} else {
// TODO: Phase 2 - Implement fetcher.scheduleImmediateFetch()
print("\(Self.TAG): RESCHEDULE_PREFETCH_PAST id=\(content.id) fetch_time=\(fetchTime) current=\(currentTime)")
}
} else {
print("\(Self.TAG): RESCHEDULE_PREFETCH_SKIP id=\(content.id) fetcher_not_available")
}
// Mark rollover as processed
await storage?.saveLastRolloverTime(for: content.id, time: Int64(Date().timeIntervalSince1970 * 1000))
return true
} else {
print("\(Self.TAG): RESCHEDULE_ERR id=\(content.id) scheduling_failed")
return false
}
}
```
**Note**: The `formatTime` method already exists (line 273), so no change needed there.
---
### 2. DailyNotificationPlugin.swift
**Location**: `ios/Plugin/DailyNotificationPlugin.swift`
#### Change 2.1: Add Rollover Handler Method + Notification Observer
**Insert after line 77** (in `load()` method, after recovery manager initialization):
```swift
// Register for notification delivery events (Notification Center pattern)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleNotificationDelivery(_:)),
name: NSNotification.Name("DailyNotificationDelivered"),
object: nil
)
```
**Insert after line 1242** (after `getNotificationStatus` method):
```swift
/**
* Handle notification delivery event (from Notification Center)
*
* This is called when AppDelegate posts notification delivery event
* Matches Android's scheduleNextNotification() behavior
*
* @param notification NSNotification with userInfo containing notification_id and scheduled_time
*/
@objc private func handleNotificationDelivery(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let notificationId = userInfo["notification_id"] as? String,
let scheduledTime = userInfo["scheduled_time"] as? Int64 else {
print("DNP-ROLLOVER: Invalid notification data")
return
}
Task {
await processRollover(notificationId: notificationId, scheduledTime: scheduledTime)
}
}
/**
* Process rollover for delivered notification
*
* @param notificationId ID of notification that was delivered
* @param scheduledTime Scheduled time of delivered notification
*/
private func processRollover(notificationId: String, scheduledTime: Int64) async {
guard let scheduler = scheduler, let storage = storage else {
print("DNP-ROLLOVER: Plugin not initialized")
return
}
// Get the notification content that was delivered
guard let content = storage.getNotificationContent(id: notificationId) else {
print("DNP-ROLLOVER: Could not find notification content for id=\(notificationId)")
return
}
// Schedule next notification
// Note: DailyNotificationFetcher integration deferred to Phase 2
let scheduled = await scheduler.scheduleNextNotification(
content,
storage: storage,
fetcher: nil // TODO: Phase 2 - Add fetcher instance
)
if scheduled {
print("DNP-ROLLOVER: Successfully scheduled next notification for id=\(notificationId)")
// Log success (non-fatal, background operation)
} else {
print("DNP-ROLLOVER: Failed to schedule next notification for id=\(notificationId)")
// Log failure but continue (recovery will handle on next launch)
}
}
```
#### Change 2.2: Update getNotificationStatus to Include Rollover Info
**Modify line 1229-1236** (in `getNotificationStatus` method):
```swift
// Calculate next notification time
let nextNotificationTime = await scheduler.getNextNotificationTime() ?? 0
// Get rollover status
let lastRolloverTime = await storage?.getLastRolloverTime() ?? 0
var result: [String: Any] = [
"isEnabled": isEnabled,
"isScheduled": pendingCount > 0,
"lastNotificationTime": lastNotification?.scheduledTime ?? 0,
"nextNotificationTime": nextNotificationTime,
"pending": pendingCount,
"rolloverEnabled": true, // Indicate rollover is active
"lastRolloverTime": lastRolloverTime, // When last rollover occurred
"settings": settings
]
```
---
### 3. AppDelegate.swift
**Location**: `test-apps/ios-test-app/ios/App/App/AppDelegate.swift`
#### Change 3.1: Modify willPresent to Trigger Rollover
**Replace lines 136-152** with:
```swift
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
NSLog("DNP-DEBUG: ✅ userNotificationCenter willPresent called!")
NSLog("DNP-DEBUG: Notification received in foreground: %@", notification.request.identifier)
NSLog("DNP-DEBUG: Notification title: %@", notification.request.content.title)
NSLog("DNP-DEBUG: Notification body: %@", notification.request.content.body)
// Extract notification info from userInfo for rollover
let userInfo = notification.request.content.userInfo
if let notificationId = userInfo["notification_id"] as? String,
let scheduledTime = userInfo["scheduled_time"] as? Int64 {
// Trigger rollover scheduling (async, non-blocking)
Task {
await handleNotificationRollover(notificationId: notificationId, scheduledTime: scheduledTime)
}
}
// Show notification with banner, sound, and badge
// Use .banner for iOS 14+, fallback to .alert for iOS 13
if #available(iOS 14.0, *) {
completionHandler([.banner, .sound, .badge])
} else {
completionHandler([.alert, .sound, .badge])
}
NSLog("DNP-DEBUG: ✅ Completion handler called with presentation options")
}
```
#### Change 3.2: Post Notification for Rollover (Notification Center Pattern)
**Insert after line 152** (after `willPresent` completion handler):
```swift
// Post notification to trigger rollover (decoupled pattern)
NotificationCenter.default.post(
name: NSNotification.Name("DailyNotificationDelivered"),
object: nil,
userInfo: [
"notification_id": notificationId,
"scheduled_time": scheduledTime
]
)
```
**Note**: This uses Notification Center pattern for decoupling. Plugin will observe this notification.
---
### 4. DailyNotificationStorage.swift
**Location**: `ios/Plugin/DailyNotificationStorage.swift`
#### Change 4.1: Add Rollover State Tracking Methods
**Insert after line 148** (after `getAllNotifications` method):
```swift
/**
* Get last rollover time for a notification ID
*
* @param notificationId Notification ID
* @return Last rollover time in milliseconds, or nil if never rolled over
*/
func getLastRolloverTime(for notificationId: String) async -> Int64? {
let key = "rollover_\(notificationId)"
let lastTime = userDefaults.object(forKey: key) as? Int64
return lastTime
}
/**
* Save last rollover time for a notification ID
*
* @param notificationId Notification ID
* @param time Rollover time in milliseconds
*/
func saveLastRolloverTime(for notificationId: String, time: Int64) async {
let key = "rollover_\(notificationId)"
userDefaults.set(time, forKey: key)
userDefaults.synchronize()
}
/**
* Get last rollover time (any notification)
*
* @return Last rollover time in milliseconds, or 0 if never rolled over
*/
func getLastRolloverTime() -> Int64 {
let key = "rollover_last"
return Int64(userDefaults.integer(forKey: key))
}
/**
* Save last rollover time (any notification)
*
* @param time Rollover time in milliseconds
*/
func saveLastRolloverTime(_ time: Int64) {
let key = "rollover_last"
userDefaults.set(time, forKey: key)
userDefaults.synchronize()
}
```
---
### 5. DailyNotificationReactivationManager.swift
**Location**: `ios/Plugin/DailyNotificationReactivationManager.swift`
#### Change 5.1: Add Rollover Check to Recovery
**Insert after line 338** (in `performColdStartRecovery` method, after detecting missed notifications):
```swift
// Step 4.5: Check for delivered notifications and trigger rollover
// This handles notifications that were delivered while app was not running
await checkAndProcessDeliveredNotifications()
```
#### Change 5.2: Add Delivered Notifications Check Method
**Insert at end of class** (before closing brace):
```swift
/**
* Check for delivered notifications and trigger rollover
*
* This ensures rollover happens on app launch if notifications were delivered
* while the app was not running
*/
private func checkAndProcessDeliveredNotifications() async {
print("\(Self.TAG): Checking for delivered notifications to trigger rollover")
// Get delivered notifications from system
let deliveredNotifications = await notificationCenter.deliveredNotifications()
// Get last processed rollover time from storage
let lastProcessedTime = storage.getLastRolloverTime()
for notification in deliveredNotifications {
let userInfo = notification.request.content.userInfo
guard let notificationId = userInfo["notification_id"] as? String,
let scheduledTime = userInfo["scheduled_time"] as? Int64 else {
continue
}
// Only process if this notification hasn't been processed yet
if scheduledTime > lastProcessedTime {
print("\(Self.TAG): Found delivered notification id=\(notificationId) scheduledTime=\(scheduledTime)")
// Get notification content
guard let content = storage.getNotificationContent(id: notificationId) else {
print("\(Self.TAG): Could not find content for delivered notification id=\(notificationId)")
continue
}
// Trigger rollover
let scheduled = await scheduler.scheduleNextNotification(
content,
storage: storage,
fetcher: nil // TODO: Add fetcher in Phase 2
)
if scheduled {
print("\(Self.TAG): Successfully rolled over delivered notification id=\(notificationId)")
// Update last processed time
storage.saveLastRolloverTime(scheduledTime)
} else {
print("\(Self.TAG): Failed to roll over delivered notification id=\(notificationId)")
}
}
}
}
```
---
## Integration Points
### 1. AppDelegate → Plugin (Notification Center Pattern)
- **Flow**: AppDelegate detects notification → posts Notification Center event → plugin observes and handles
- **Challenge**: Decoupling AppDelegate from plugin
- **Solution**: Use Notification Center for decoupled communication
### 2. Plugin → Scheduler
- **Flow**: Plugin receives rollover request → calls scheduler method
- **Challenge**: Passing storage and fetcher instances
- **Solution**: Plugin maintains references, passes to scheduler
### 3. Scheduler → Storage
- **Flow**: Scheduler checks duplicates → queries storage
- **Challenge**: Thread safety
- **Solution**: Storage methods are already thread-safe (UserDefaults)
### 4. Recovery Manager → Scheduler
- **Flow**: Recovery detects delivered notifications → triggers rollover
- **Challenge**: Ensuring rollover happens on app launch
- **Solution**: Integrate into existing recovery flow
---
## Dependencies & Order
### Implementation Order
1. **Phase 1: Core Infrastructure**
- ✅ Add `calculateNextScheduledTime` to Scheduler
- ✅ Add `scheduleNextNotification` to Scheduler
- ✅ Add rollover state tracking to Storage
- ✅ Add `handleNotificationRollover` to Plugin
2. **Phase 2: Detection Mechanisms**
- ✅ Modify AppDelegate `willPresent` method
- ✅ Add rollover check to Recovery Manager
- ✅ Test foreground delivery
3. **Phase 3: Edge Case Handling** (Future)
- Add TimeChangeDetector
- Add TimezoneChangeDetector
- Add RolloverCoordinator
4. **Phase 4: Integration** (Future)
- Integrate fetcher for prefetch scheduling
- Add comprehensive logging
- Performance optimization
---
## Testing Strategy
### Test 1: Foreground Delivery
- **Setup**: App running, notification fires
- **Expected**: Rollover triggers via AppDelegate → Notification Center → Plugin
- **Verify**: Next notification scheduled, logs show rollover success
### Test 2: Background Delivery
- **Setup**: App not running, notification fires
- **Expected**: Rollover triggers on app launch via Recovery Manager
- **Verify**: Next notification scheduled, recovery logs show rollover
### Test 3: Duplicate Prevention
- **Setup**: Trigger rollover multiple times (rapid fire)
- **Expected**: Only one notification scheduled
- **Verify**: No duplicates in system, logs show duplicate prevention
### Test 4: DST Transition
- **Setup**: Schedule notification on DST transition day
- **Expected**: 24-hour calculation handles DST correctly
- **Verify**: Notification fires at correct time, logs show DST transition
### Test 5: Error Handling
- **Setup**: Simulate failure (e.g., invalid notification ID)
- **Expected**: Error logged, app continues, no crash
- **Verify**: Logs show error, recovery handles on next launch
---
## Open Questions — RESOLVED
**See**: `docs/ios-rollover-open-questions-answers.md` for detailed answers
### Summary of Decisions:
1. **Fetcher Integration**: ✅ Defer to Phase 2, use optional parameter pattern
2. **AppDelegate Access**: ✅ Use Notification Center pattern (decoupling, flexibility)
3. **Background Task**: ✅ Rely on existing recovery + AppDelegate (no dedicated task)
4. **Error Handling**: ✅ Log + Continue (non-fatal), no retry, no user notification
5. **Performance**: ✅ Process individually (low volume, simplicity)
6. **Testing**: ✅ Manual testing for Phase 1, automated tests for Phase 2
---
## Next Steps
1. **Review this document** ✅ (Current step)
2. **Address open questions**
3. **Create implementation tasks**
4. **Implement Phase 1** (Core rollover)
5. **Test Phase 1**
6. **Implement Phase 2** (Edge case detection)
7. **Final testing and validation**
---
## References
- Edge Case Plan: `docs/ios-rollover-edge-case-plan.md`
- Android Implementation: `android/src/main/java/com/timesafari/dailynotification/DailyNotificationWorker.java`
- iOS Scheduler: `ios/Plugin/DailyNotificationScheduler.swift`
- iOS Plugin: `ios/Plugin/DailyNotificationPlugin.swift`

View File

@@ -0,0 +1,343 @@
# iOS Rollover Implementation — Open Questions & Answers
**Date**: 2025-01-27
**Status**: Pre-Implementation Decisions
---
## Question 1: Fetcher Integration
**Question**: How should we integrate DailyNotificationFetcher for prefetch scheduling? (Phase 2)
### Current State Analysis
- **Android**: Uses `DailyNotificationFetcher.scheduleFetch(fetchTime)` and `scheduleImmediateFetch()`
- **iOS**: Has `DailyNotificationBackgroundTaskManager` with `scheduleBackgroundTask()` method
- **iOS Pattern**: Uses `BGTaskScheduler` with `BGAppRefreshTaskRequest`
### Recommendation: **Defer to Phase 2, Use Placeholder Pattern**
**Rationale**:
1. **Phase 1 Focus**: Core rollover functionality (scheduling next notification)
2. **Prefetch is Separate**: Prefetch scheduling is independent of rollover
3. **Existing Infrastructure**: iOS already has background task infrastructure
4. **Android Pattern**: Android also separates rollover from prefetch (optional parameter)
### Implementation Approach
**Phase 1 (Current)**:
- Make `fetcher` parameter optional in `scheduleNextNotification()`
- Add TODO comments for Phase 2 integration
- Log prefetch scheduling intent (even if not executed)
**Phase 2 (Future)**:
- Create `DailyNotificationFetcher` class (iOS equivalent)
- Integrate with `DailyNotificationBackgroundTaskManager`
- Use `BGTaskScheduler` for prefetch scheduling
- Calculate fetch time: `nextScheduledTime - (5 * 60 * 1000)` (5 minutes before)
### Code Pattern
```swift
// Phase 1: Optional fetcher, log intent
if let fetcher = fetcher {
let fetchTime = nextScheduledTime - (5 * 60 * 1000)
// TODO: Phase 2 - Implement fetcher.scheduleFetch(fetchTime)
print("\(Self.TAG): RESCHEDULE_PREFETCH_SCHEDULED id=\(content.id) next_fetch=\(fetchTime)")
} else {
print("\(Self.TAG): RESCHEDULE_PREFETCH_SKIP id=\(content.id) fetcher_not_available")
}
```
**Decision**: ✅ **Defer to Phase 2, use optional parameter pattern**
---
## Question 2: AppDelegate Access
**Question**: Is there a better way to access the plugin from AppDelegate without using Capacitor bridge?
### Current State Analysis
- **Capacitor Pattern**: Uses `CAPBridgeViewController` to access plugins
- **Test App**: Already uses this pattern for other operations
- **Production Apps**: May have different AppDelegate structures
### Recommendation: **Use Notification Center Pattern**
**Rationale**:
1. **Decoupling**: AppDelegate doesn't need direct plugin reference
2. **Flexibility**: Works across different app architectures
3. **Reliability**: Notification center is always available
4. **Testability**: Easier to test without Capacitor dependency
### Implementation Approach
**Option A: Notification Center (Recommended)**
- Plugin registers for notification delivery events
- AppDelegate posts notification when delivery detected
- Plugin handles rollover in response to notification
**Option B: Capacitor Bridge (Fallback)**
- Use existing bridge pattern
- Works but creates tight coupling
- Use as fallback if notification center doesn't work
### Code Pattern
```swift
// In DailyNotificationPlugin.load():
NotificationCenter.default.addObserver(
self,
selector: #selector(handleNotificationDelivery(_:)),
name: NSNotification.Name("DailyNotificationDelivered"),
object: nil
)
// In AppDelegate.willPresent:
NotificationCenter.default.post(
name: NSNotification.Name("DailyNotificationDelivered"),
object: nil,
userInfo: [
"notification_id": notificationId,
"scheduled_time": scheduledTime
]
)
```
**Decision**: ✅ **Use Notification Center pattern, with Capacitor bridge as fallback**
---
## Question 3: Background Task
**Question**: Should we add a dedicated background task for rollover detection, or rely on existing recovery mechanisms?
### Current State Analysis
- **Existing Recovery**: `DailyNotificationReactivationManager` already runs on app launch
- **Background Tasks**: iOS has strict limits on background execution
- **Reliability**: Multiple detection mechanisms increase reliability
### Recommendation: **Rely on Existing Recovery + AppDelegate**
**Rationale**:
1. **iOS Limitations**: Background tasks are unreliable (system-controlled)
2. **Existing Infrastructure**: Recovery manager already handles app launch scenarios
3. **Coverage**: AppDelegate (foreground) + Recovery (background) covers all cases
4. **Simplicity**: Fewer moving parts = fewer failure points
### Implementation Approach
**Two Detection Mechanisms**:
1. **Foreground**: AppDelegate `willPresent` → immediate rollover
2. **Background**: Recovery Manager → rollover on app launch
**No Dedicated Background Task**:
- Background tasks are unreliable (system decides when to run)
- Recovery manager already covers app launch scenarios
- Adding another mechanism adds complexity without significant benefit
### Code Pattern
```swift
// Detection Mechanism 1: Foreground (AppDelegate)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, ...) {
// Trigger rollover immediately
await handleNotificationRollover(...)
}
// Detection Mechanism 2: Background (Recovery Manager)
func performColdStartRecovery() async {
// Check for delivered notifications
await checkAndProcessDeliveredNotifications()
}
```
**Decision**: ✅ **Rely on existing recovery + AppDelegate, no dedicated background task**
---
## Question 4: Error Handling
**Question**: How should we handle rollover failures? Retry? Log? User notification?
### Current State Analysis
- **Android Pattern**: Logs errors, continues execution (non-fatal)
- **iOS Recovery Manager**: Catches all errors, logs, continues (non-fatal)
- **User Experience**: Failures should be silent (background operation)
### Recommendation: **Log + Continue (Non-Fatal)**
**Rationale**:
1. **Background Operation**: Rollover is background, shouldn't interrupt user
2. **Recovery Available**: Recovery manager will catch missed rollovers on next app launch
3. **Consistency**: Matches Android and existing iOS recovery patterns
4. **User Experience**: Silent failures, recovery on next launch
### Implementation Approach
**Error Handling Strategy**:
1. **Log Errors**: Comprehensive logging for debugging
2. **Continue Execution**: Don't crash or interrupt app
3. **No Retry**: Let recovery manager handle on next launch
4. **No User Notification**: Background operation, silent failure
5. **History Recording**: Record failures in history (if history implemented)
### Code Pattern
```swift
func scheduleNextNotification(...) async -> Bool {
do {
// Rollover logic
return true
} catch {
print("\(Self.TAG): RESCHEDULE_ERR id=\(content.id) err=\(error.localizedDescription)")
// Log error but don't throw - let recovery handle on next launch
return false
}
}
// In recovery manager:
if !scheduled {
print("\(Self.TAG): Failed to roll over delivered notification id=\(notificationId)")
// Recovery will retry on next app launch
}
```
**Decision**: ✅ **Log + Continue (non-fatal), no retry, no user notification**
---
## Question 5: Performance
**Question**: Should we batch rollover operations or process individually?
### Current State Analysis
- **Android Pattern**: Processes individually (one notification at a time)
- **iOS Recovery**: Processes notifications individually
- **Volume**: Typically 1-2 notifications per day (low volume)
### Recommendation: **Process Individually**
**Rationale**:
1. **Low Volume**: Typically 1 notification per day, batching unnecessary
2. **Simplicity**: Individual processing is simpler and easier to debug
3. **Error Isolation**: Individual processing isolates failures
4. **Consistency**: Matches Android and existing iOS patterns
### Implementation Approach
**Individual Processing**:
- Process each notification rollover separately
- Each rollover is independent operation
- Failures in one don't affect others
- Easier to log and debug
**Future Optimization** (if needed):
- If volume increases, consider batching
- Current volume doesn't justify batching complexity
### Code Pattern
```swift
// Process individually (current approach)
for notification in deliveredNotifications {
await scheduler.scheduleNextNotification(notification, ...)
}
// Batching would look like:
// await scheduler.scheduleNextNotificationsBatch(notifications, ...)
// But not needed for current volume
```
**Decision**: ✅ **Process individually (current volume doesn't justify batching)**
---
## Question 6: Testing
**Question**: Do we need automated tests for rollover, or is manual testing sufficient for Phase 1?
### Current State Analysis
- **Existing Tests**: iOS has unit tests for recovery manager
- **Test Coverage**: Some components have tests, others don't
- **Phase 1 Scope**: Core rollover functionality
### Recommendation: **Manual Testing for Phase 1, Automated Tests for Phase 2**
**Rationale**:
1. **Phase 1 Focus**: Core functionality, manual testing sufficient
2. **Complexity**: Rollover involves system notifications (hard to test automatically)
3. **Time Investment**: Automated tests take time, manual testing faster for Phase 1
4. **Phase 2**: Add automated tests when edge cases are implemented
### Implementation Approach
**Phase 1 Testing**:
- Manual testing checklist
- Test scenarios: foreground delivery, background delivery, duplicates
- Real device testing (simulator may not handle notifications correctly)
**Phase 2 Testing**:
- Unit tests for time calculations (DST, timezone)
- Integration tests for rollover flow
- Edge case tests (time changes, timezone changes)
### Test Checklist (Phase 1)
1.**Foreground Delivery**: App running, notification fires → rollover triggers
2.**Background Delivery**: App not running, notification fires → rollover on launch
3.**Duplicate Prevention**: Multiple rollover attempts → only one scheduled
4.**DST Transition**: Schedule on DST day → correct time calculation
5.**Error Handling**: Simulate failure → graceful degradation
**Decision**: ✅ **Manual testing for Phase 1, automated tests for Phase 2**
---
## Summary of Decisions
| Question | Decision | Rationale |
|----------|----------|-----------|
| **Fetcher Integration** | Defer to Phase 2, optional parameter | Prefetch is separate concern, Phase 1 focuses on core rollover |
| **AppDelegate Access** | Notification Center pattern | Decoupling, flexibility, reliability |
| **Background Task** | Rely on existing recovery | iOS limitations, existing infrastructure sufficient |
| **Error Handling** | Log + Continue (non-fatal) | Background operation, recovery handles failures |
| **Performance** | Process individually | Low volume, simplicity, consistency |
| **Testing** | Manual for Phase 1, automated for Phase 2 | Phase 1 scope, complexity, time investment |
---
## Implementation Impact
### Changes to Review Document
Based on these decisions, the review document should be updated:
1. **Fetcher Parameter**: Make optional, add Phase 2 TODOs
2. **AppDelegate Pattern**: Use Notification Center instead of Capacitor bridge
3. **Background Task**: Remove dedicated background task, rely on recovery
4. **Error Handling**: Add comprehensive logging, non-fatal errors
5. **Performance**: Individual processing (no batching)
6. **Testing**: Manual testing checklist for Phase 1
### Next Steps
1.**Decisions Made** (This document)
2. **Update Review Document** with decisions
3. **Update Implementation Plan** with specific patterns
4. **Begin Phase 1 Implementation**
---
## References
- Review Document: `docs/ios-rollover-implementation-review.md`
- Edge Case Plan: `docs/ios-rollover-edge-case-plan.md`
- Android Implementation: `android/src/main/java/com/timesafari/dailynotification/DailyNotificationWorker.java`
- iOS Recovery Manager: `ios/Plugin/DailyNotificationReactivationManager.swift`
- iOS Background Tasks: `ios/Plugin/DailyNotificationBackgroundTaskManager.swift`

View File

@@ -0,0 +1,574 @@
# iOS Troubleshooting Guide
**Author**: Matthew Raymer
**Date**: 2025-12-08
**Status**: 🎯 **ACTIVE** - iOS Troubleshooting Reference
**Version**: 1.0.0
## Purpose
This guide provides solutions to common iOS-specific issues when using the Daily Notification Plugin. It covers debugging techniques, common problems, and their solutions.
**Reference**:
- [iOS Implementation Directive](./ios-implementation-directive.md) - Implementation details
- [Platform Capability Reference](./alarms/01-platform-capability-reference.md) - iOS OS behaviors
- [iOS Logging Guide](../doc/test-app-ios/IOS_LOGGING_GUIDE.md) - How to view logs
---
## 1. Common Issues
### 1.1 Notifications Not Firing
**Symptoms:**
- Notifications scheduled but don't appear
- No notification at scheduled time
- Notifications work in simulator but not on device
**Diagnosis Steps:**
1. **Check Notification Permission:**
```swift
// In app code
UNUserNotificationCenter.current().getNotificationSettings { settings in
print("Authorization status: \(settings.authorizationStatus)")
}
```
Or check in Xcode Console:
```
DNP-PLUGIN: Notification permission status: authorized
```
2. **Check Pending Notifications:**
```swift
UNUserNotificationCenter.current().getPendingNotificationRequests { requests in
print("Pending notifications: \(requests.count)")
}
```
3. **Check Background App Refresh:**
- Settings → [Your App] → Background App Refresh
- Must be enabled for background tasks
4. **Check Notification Limit:**
- iOS limits to 64 pending notifications
- Check if limit is exceeded
**Solutions:**
- **Permission Denied:**
- Request permission: `DailyNotification.requestNotificationPermission()`
- Guide user to Settings → [Your App] → Notifications
- **Background App Refresh Disabled:**
- Guide user to enable: Settings → [Your App] → Background App Refresh
- Or use: `DailyNotification.openBackgroundAppRefreshSettings()`
- **Notification Limit Exceeded:**
- Reduce number of scheduled notifications
- Implement notification cleanup logic
- Check rolling window implementation
- **Simulator vs Device:**
- Simulator may not fire notifications reliably
- Test on physical device for accurate behavior
---
### 1.2 Background Tasks Not Executing
**Symptoms:**
- Prefetch tasks don't run
- Background fetch never executes
- BGTaskScheduler tasks not firing
**Diagnosis Steps:**
1. **Check BGTaskScheduler Registration:**
```swift
// Verify registration in AppDelegate
BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.timesafari.dailynotification.fetch", using: nil) { task in
// Handler should be registered
}
```
2. **Check Info.plist:**
```xml
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.timesafari.dailynotification.fetch</string>
</array>
```
3. **Check Background App Refresh:**
- Must be enabled in Settings
- System-controlled timing (not guaranteed)
4. **Check Logs:**
```
DNP-FETCH-START: Background fetch task started
```
**Solutions:**
- **Not Registered:**
- Verify registration in `AppDelegate.application(_:didFinishLaunchingWithOptions:)`
- Check Info.plist has correct identifiers
- **Background App Refresh Disabled:**
- User must enable in Settings
- Cannot be programmatically enabled
- **System Not Executing:**
- BGTaskScheduler is system-controlled
- Execution timing is not guaranteed
- System may defer or skip tasks
- Use for prefetching only, not critical scheduling
- **Simulator Limitations:**
- Background tasks may not execute in simulator
- Test on physical device
---
### 1.3 Notifications Disappear After App Termination
**Symptoms:**
- Notifications scheduled but disappear when app is terminated
- Notifications don't persist across app restarts
**Diagnosis:**
**This should NOT happen on iOS** - notifications persist automatically (OS-guaranteed).
**If it happens, check:**
1. **Notification Trigger Type:**
- Calendar/time triggers persist ✅
- Location triggers do NOT persist ❌
2. **Notification Content:**
- Ensure notification has valid content
- Check for invalid trigger dates
3. **System Storage:**
- iOS may clear notifications if device storage is full
- Check available storage
**Solutions:**
- **Use Calendar Triggers:**
```swift
// ✅ Persists across reboot
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
// ❌ Does NOT persist
let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
```
- **Check Device Storage:**
- Free up storage if device is full
- iOS may clear notifications when storage is critical
---
### 1.4 Recovery Not Working
**Symptoms:**
- Missed notifications not detected on app launch
- Future notifications not verified/rescheduled
- No recovery activity in logs
**Diagnosis:**
**Recovery features are NOT yet implemented** (as of 2025-12-08).
**Expected Behavior (Once Implemented):**
1. **Check Logs for Recovery:**
```
DNP-REACTIVATION: Starting app launch recovery
DNP-REACTIVATION: Detected scenario: coldStart
DNP-REACTIVATION: Missed notifications detected: 2
DNP-REACTIVATION: Future notifications verified: 1
```
2. **Verify Recovery Logic:**
- Should run in `DailyNotificationPlugin.load()`
- Should detect missed notifications
- Should verify future notifications
**Solutions:**
- **Implementation Pending:**
- See [iOS Implementation Directive Phase 1](./ios-implementation-directive-phase1.md)
- Recovery features need to be implemented
- **Manual Workaround:**
- Check pending notifications manually
- Reschedule if needed
---
### 1.5 Database/Storage Issues
**Symptoms:**
- Database errors in logs
- Data not persisting
- Core Data errors
**Diagnosis Steps:**
1. **Check Database Path:**
```swift
let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let dbPath = documentsPath.appendingPathComponent("daily_notifications.db")
print("Database path: \(dbPath.path)")
```
2. **Check Core Data Stack:**
- Verify `NSPersistentContainer` initialization
- Check for migration errors
3. **Check Logs:**
```
DNP-STORAGE: Database opened successfully
DNP-STORAGE: Error opening database: [error]
```
**Solutions:**
- **Database Path Issues:**
- Verify app has write permissions
- Check Documents directory is accessible
- Ensure path is correct
- **Core Data Errors:**
- Check Core Data model version
- Verify migration policies
- Check for schema mismatches
- **Storage Full:**
- Free up device storage
- iOS may clear app data if storage is critical
---
### 1.6 Permission Issues
**Symptoms:**
- Permission requests fail
- Permission status incorrect
- Cannot schedule notifications
**Diagnosis:**
1. **Check Current Status:**
```swift
UNUserNotificationCenter.current().getNotificationSettings { settings in
switch settings.authorizationStatus {
case .authorized: // ✅ Can schedule
case .denied: // ❌ User denied
case .notDetermined: // ⚠️ Not requested yet
case .provisional: // ⚠️ Provisional (iOS 12+)
@unknown default: break
}
}
```
2. **Check Logs:**
```
DNP-PLUGIN: Notification permission status: denied
```
**Solutions:**
- **Permission Denied:**
- Cannot request again programmatically
- Guide user to Settings → [Your App] → Notifications
- Use: `DailyNotification.openNotificationSettings()`
- **Not Determined:**
- Request permission: `DailyNotification.requestNotificationPermission()`
- Show explanation before requesting
- **Provisional:**
- iOS 12+ feature
- Notifications delivered quietly
- User can upgrade to full permission
---
## 2. Debugging Techniques
### 2.1 Viewing Logs
**Xcode Console (Recommended):**
1. Run app in Xcode (Cmd+R)
2. Open Debug Area (Cmd+Shift+Y)
3. Filter by: `DNP-` or `DailyNotification`
**Console.app:**
1. Open Console.app
2. Select device/simulator
3. Filter by process: `ios-test-app`
**Command Line:**
```bash
# Simulator logs
xcrun simctl spawn <device-id> log stream --level=debug --predicate 'processImagePath contains "ios-test-app"'
# Device logs (requires device connected)
xcrun devicectl device process monitor --device <device-id> --filter "ios-test-app"
```
### 2.2 Checking Pending Notifications
**Via Plugin Method:**
```typescript
const result = await DailyNotification.getPendingNotifications();
console.log(`Pending: ${result.count}`);
```
**Via Swift Code:**
```swift
UNUserNotificationCenter.current().getPendingNotificationRequests { requests in
print("Pending notifications: \(requests.count)")
for request in requests {
print(" - \(request.identifier): \(request.content.title)")
}
}
```
### 2.3 Checking Background Task Status
**Via Plugin Method:**
```typescript
const status = await DailyNotification.getBackgroundTaskStatus();
console.log(`Fetch task registered: ${status.fetchTaskRegistered}`);
console.log(`Background refresh enabled: ${status.backgroundRefreshEnabled}`);
```
**Via Swift Code:**
```swift
// Check registration
let registered = BGTaskScheduler.shared.registeredTaskIdentifiers
print("Registered tasks: \(registered)")
// Check Background App Refresh (requires entitlement)
// Cannot check programmatically - must guide user to Settings
```
### 2.4 Simulating Background Tasks (Simulator Only)
**LLDB Command in Xcode:**
```lldb
e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.timesafari.dailynotification.fetch"]
```
**Note:** This only works in simulator, not on physical devices.
---
## 3. Platform-Specific Considerations
### 3.1 Simulator vs Device
**Simulator Limitations:**
- Background tasks may not execute reliably
- Notifications may not fire at exact time
- Some features require physical device
**Device Testing:**
- More accurate behavior
- Background tasks execute (system-controlled)
- Notifications fire reliably
**Recommendation:** Test critical features on physical device.
### 3.2 iOS Version Differences
**iOS 12+:**
- Provisional notification authorization
- Background task improvements
**iOS 13+:**
- State actor support (concurrency)
- Improved background execution
**iOS 14+:**
- Notification interruption levels
- Focus modes (may affect notifications)
**iOS 15+:**
- Notification summary
- Focus mode integration
### 3.3 Background Execution Limits
**iOS Constraints:**
- BGTaskScheduler is system-controlled
- Execution timing not guaranteed
- Minimum intervals between tasks (hours)
- Tasks may be deferred or skipped
**Workaround:**
- Use BGTaskScheduler for prefetching only
- Don't rely on it for critical scheduling
- Use UNUserNotificationCenter for notifications (more reliable)
---
## 4. Error Codes
### 4.1 Common Error Codes
| Error Code | Description | Solution |
| ---------- | ----------- | -------- |
| `NOTIFICATION_PERMISSION_DENIED` | User denied notification permission | Guide user to Settings |
| `BACKGROUND_REFRESH_DISABLED` | Background App Refresh disabled | Guide user to enable in Settings |
| `PENDING_NOTIFICATION_LIMIT_EXCEEDED` | Exceeded 64 notification limit | Reduce scheduled notifications |
| `BG_TASK_NOT_REGISTERED` | Background task not registered | Check Info.plist and AppDelegate |
| `BG_TASK_EXECUTION_FAILED` | Background task execution failed | Check logs for specific error |
### 4.2 Checking Error Details
**Via Logs:**
```
DNP-ERROR: [Error Code] [Error Message]
DNP-ERROR: NOTIFICATION_PERMISSION_DENIED: User denied notification permission
```
**Via Plugin:**
```typescript
try {
await DailyNotification.scheduleDailyNotification({...});
} catch (error) {
console.error('Error code:', error.code);
console.error('Error message:', error.message);
}
```
---
## 5. Performance Issues
### 5.1 Slow Notification Scheduling
**Symptoms:**
- Scheduling takes too long
- App freezes during scheduling
**Solutions:**
- Schedule notifications asynchronously
- Batch operations when possible
- Use background queue for heavy operations
### 5.2 High Memory Usage
**Symptoms:**
- App memory usage high
- Memory warnings in logs
**Solutions:**
- Implement notification cleanup
- Limit cached notifications
- Use efficient data structures
### 5.3 Battery Drain
**Symptoms:**
- Battery drains quickly
- Background activity high
**Solutions:**
- Limit background task frequency
- Optimize prefetch operations
- Use efficient scheduling algorithms
---
## 6. Getting Help
### 6.1 Log Collection
**Collect Logs:**
1. Reproduce the issue
2. Collect logs from Xcode Console or Console.app
3. Filter by `DNP-` prefix
4. Include relevant error messages
**Log Format:**
```
DNP-PLUGIN: [Message]
DNP-ERROR: [Error Code] [Error Message]
DNP-REACTIVATION: [Recovery Activity]
```
### 6.2 Issue Reporting
**Include:**
- iOS version
- Device model (or simulator)
- Plugin version
- Steps to reproduce
- Relevant logs
- Expected vs actual behavior
### 6.3 Documentation References
- [iOS Implementation Directive](./ios-implementation-directive.md)
- [Platform Capability Reference](./alarms/01-platform-capability-reference.md)
- [API Reference](../API.md)
- [iOS Logging Guide](../doc/test-app-ios/IOS_LOGGING_GUIDE.md)
---
## 7. Quick Reference
### 7.1 Common Commands
**Check Pending Notifications:**
```bash
# Via plugin method (recommended)
# Or check logs for scheduling activity
```
**View Logs:**
```bash
# Xcode Console (Cmd+Shift+Y)
# Filter: DNP-
# Console.app
# Filter: ios-test-app
```
**Check Permissions:**
```typescript
const status = await DailyNotification.getNotificationPermissionStatus();
```
**Open Settings:**
```typescript
await DailyNotification.openNotificationSettings();
await DailyNotification.openBackgroundAppRefreshSettings();
```
### 7.2 Checklist
**Before Reporting Issue:**
- [ ] Checked notification permissions
- [ ] Verified Background App Refresh is enabled
- [ ] Checked pending notification count (< 64)
- [ ] Reviewed logs for errors
- [ ] Tested on physical device (not just simulator)
- [ ] Verified iOS version compatibility
- [ ] Checked device storage availability
---
**Document Version**: 1.0.0
**Last Updated**: 2025-12-08
**Next Review**: After Phase 1 implementation

View File

@@ -259,7 +259,7 @@ class DailyNotificationBackgroundTaskTestHarness {
/// - ETag validation
/// - Content caching
/// - Error handling
class PrefetchOperation: Operation {
class PrefetchOperation: Operation, @unchecked Sendable {
var isFailed = false
private static let fetchLog = OSLog(subsystem: "com.timesafari.dailynotification", category: "fetch")

View File

@@ -0,0 +1,194 @@
/**
* DailyNotificationDataConversions.swift
*
* Data type conversion helpers for Core Data operations
* Handles conversions between Swift types and Core Data types,
* especially for time (Date Long/Int64) and numeric types.
*
* @author Matthew Raymer
* @version 1.0.0
* @created 2025-12-08
*/
import Foundation
import CoreData
/**
* Data conversion utilities for Core Data operations
*
* This module provides helper functions for converting between:
* - Date Int64 (epoch milliseconds)
* - Int Int32
* - Long Int64
* - Optional string handling
*/
class DailyNotificationDataConversions {
// MARK: - Constants
private static let TAG = "DNP-DATA-CONVERSIONS"
// MARK: - Time Conversions (Section 6.1)
/**
* Convert epoch milliseconds (Int64) to Date
*
* @param epochMillis Milliseconds since epoch (1970-01-01 00:00:00 UTC)
* @return Date object
*/
static func dateFromEpochMillis(_ epochMillis: Int64) -> Date {
return Date(timeIntervalSince1970: Double(epochMillis) / 1000.0)
}
/**
* Convert Date to epoch milliseconds (Int64)
*
* @param date Date object
* @return Milliseconds since epoch (1970-01-01 00:00:00 UTC)
*/
static func epochMillisFromDate(_ date: Date) -> Int64 {
return Int64(date.timeIntervalSince1970 * 1000.0)
}
/**
* Convert optional epoch milliseconds to optional Date
*
* @param epochMillis Optional milliseconds since epoch
* @return Optional Date object
*/
static func dateFromEpochMillis(_ epochMillis: Int64?) -> Date? {
guard let millis = epochMillis else { return nil }
return dateFromEpochMillis(millis)
}
/**
* Convert optional Date to optional epoch milliseconds
*
* @param date Optional Date object
* @return Optional milliseconds since epoch
*/
static func epochMillisFromDate(_ date: Date?) -> Int64? {
guard let dateValue = date else { return nil }
return epochMillisFromDate(dateValue)
}
// MARK: - Numeric Conversions (Section 6.2)
/**
* Convert Int to Int32 (for Core Data Integer 32)
*
* @param value Int value
* @return Int32 value
*/
static func int32FromInt(_ value: Int) -> Int32 {
return Int32(value)
}
/**
* Convert Int32 to Int
*
* @param value Int32 value
* @return Int value
*/
static func intFromInt32(_ value: Int32) -> Int {
return Int(value)
}
/**
* Convert Int64 to Int32 (with clamping if needed)
*
* @param value Int64 value
* @return Int32 value (clamped if out of range)
*/
static func int32FromInt64(_ value: Int64) -> Int32 {
if value > Int64(Int32.max) {
return Int32.max
} else if value < Int64(Int32.min) {
return Int32.min
}
return Int32(value)
}
/**
* Convert Int32 to Int64
*
* @param value Int32 value
* @return Int64 value
*/
static func int64FromInt32(_ value: Int32) -> Int64 {
return Int64(value)
}
/**
* Convert Long (Int64) to Int64 (no-op, but explicit)
*
* @param value Int64 value
* @return Int64 value
*/
static func int64FromLong(_ value: Int64) -> Int64 {
return value
}
/**
* Convert Boolean to Bool (direct, but explicit)
*
* @param value Boolean value
* @return Bool value
*/
static func boolFromBoolean(_ value: Bool) -> Bool {
return value
}
// MARK: - String Conversions (Section 6.3)
/**
* Safely convert optional String to String
*
* @param value Optional String
* @return String (empty string if nil)
*/
static func stringFromOptional(_ value: String?) -> String {
return value ?? ""
}
/**
* Safely convert String to optional String
*
* @param value String value
* @return Optional String (nil if empty)
*/
static func optionalStringFromString(_ value: String) -> String? {
return value.isEmpty ? nil : value
}
/**
* Convert JSON dictionary to JSON string
*
* @param dict Dictionary to encode
* @return JSON string or nil if encoding fails
*/
static func jsonStringFromDictionary(_ dict: [String: Any]?) -> String? {
guard let dict = dict else { return nil }
guard let data = try? JSONSerialization.data(withJSONObject: dict),
let jsonString = String(data: data, encoding: .utf8) else {
return nil
}
return jsonString
}
/**
* Convert JSON string to dictionary
*
* @param jsonString JSON string to decode
* @return Dictionary or nil if decoding fails
*/
static func dictionaryFromJsonString(_ jsonString: String?) -> [String: Any]? {
guard let jsonString = jsonString,
let data = jsonString.data(using: .utf8),
let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
return nil
}
return dict
}
}

View File

@@ -26,6 +26,13 @@ struct DailyNotificationErrorCodes {
static let BACKGROUND_REFRESH_DISABLED = "background_refresh_disabled"
static let PERMISSION_DENIED = "permission_denied"
// MARK: - iOS-Specific Error Codes
static let NOTIFICATION_PERMISSION_DENIED = "notification_permission_denied"
static let PENDING_NOTIFICATION_LIMIT_EXCEEDED = "pending_notification_limit_exceeded"
static let BG_TASK_NOT_REGISTERED = "bg_task_not_registered"
static let BG_TASK_EXECUTION_FAILED = "bg_task_execution_failed"
// MARK: - Configuration Errors
static let INVALID_TIME_FORMAT = "invalid_time_format"
@@ -108,5 +115,67 @@ struct DailyNotificationErrorCodes {
message: "Notification permissions denied"
)
}
// MARK: - iOS-Specific Error Helpers
/**
* Create error response for notification permission denied
*
* @return Error response dictionary
*/
static func notificationPermissionDenied() -> [String: Any] {
return createErrorResponse(
code: NOTIFICATION_PERMISSION_DENIED,
message: "Notification permission denied. User must grant permission in Settings."
)
}
/**
* Create error response for pending notification limit exceeded
*
* @return Error response dictionary
*/
static func pendingNotificationLimitExceeded() -> [String: Any] {
return createErrorResponse(
code: PENDING_NOTIFICATION_LIMIT_EXCEEDED,
message: "Pending notification limit exceeded. iOS allows maximum 64 pending notifications."
)
}
/**
* Create error response for background task not registered
*
* @return Error response dictionary
*/
static func bgTaskNotRegistered() -> [String: Any] {
return createErrorResponse(
code: BG_TASK_NOT_REGISTERED,
message: "Background task not registered. Ensure BGTaskScheduler is properly configured."
)
}
/**
* Create error response for background task execution failed
*
* @return Error response dictionary
*/
static func bgTaskExecutionFailed() -> [String: Any] {
return createErrorResponse(
code: BG_TASK_EXECUTION_FAILED,
message: "Background task execution failed. Check Background App Refresh settings."
)
}
/**
* Create error response for background refresh disabled
*
* @return Error response dictionary
*/
static func backgroundRefreshDisabled() -> [String: Any] {
return createErrorResponse(
code: BACKGROUND_REFRESH_DISABLED,
message: "Background App Refresh is disabled. Enable it in Settings > General > Background App Refresh."
)
}
}

View File

@@ -114,11 +114,120 @@ extension History: Identifiable {
}
// MARK: - NotificationContent Entity
@objc(NotificationContentEntity)
public class NotificationContentEntity: NSManagedObject {
}
extension NotificationContentEntity {
@nonobjc public class func fetchRequest() -> NSFetchRequest<NotificationContentEntity> {
return NSFetchRequest<NotificationContentEntity>(entityName: "NotificationContent")
}
@NSManaged public var id: String?
@NSManaged public var pluginVersion: String?
@NSManaged public var timesafariDid: String?
@NSManaged public var notificationType: String?
@NSManaged public var title: String?
@NSManaged public var body: String?
@NSManaged public var scheduledTime: Date?
@NSManaged public var timezone: String?
@NSManaged public var priority: Int32
@NSManaged public var vibrationEnabled: Bool
@NSManaged public var soundEnabled: Bool
@NSManaged public var mediaUrl: String?
@NSManaged public var encryptedContent: String?
@NSManaged public var encryptionKeyId: String?
@NSManaged public var createdAt: Date?
@NSManaged public var updatedAt: Date?
@NSManaged public var ttlSeconds: Int64
@NSManaged public var deliveryStatus: String?
@NSManaged public var deliveryAttempts: Int32
@NSManaged public var lastDeliveryAttempt: Date?
@NSManaged public var userInteractionCount: Int32
@NSManaged public var lastUserInteraction: Date?
@NSManaged public var metadata: String?
}
extension NotificationContentEntity: Identifiable {
}
// MARK: - NotificationDelivery Entity
@objc(NotificationDelivery)
public class NotificationDelivery: NSManagedObject {
}
extension NotificationDelivery {
@nonobjc public class func fetchRequest() -> NSFetchRequest<NotificationDelivery> {
return NSFetchRequest<NotificationDelivery>(entityName: "NotificationDelivery")
}
@NSManaged public var id: String?
@NSManaged public var notificationId: String?
@NSManaged public var notificationContent: NotificationContentEntity?
@NSManaged public var timesafariDid: String?
@NSManaged public var deliveryTimestamp: Date?
@NSManaged public var deliveryStatus: String?
@NSManaged public var deliveryMethod: String?
@NSManaged public var deliveryAttemptNumber: Int32
@NSManaged public var deliveryDurationMs: Int64
@NSManaged public var userInteractionType: String?
@NSManaged public var userInteractionTimestamp: Date?
@NSManaged public var userInteractionDurationMs: Int64
@NSManaged public var errorCode: String?
@NSManaged public var errorMessage: String?
@NSManaged public var deviceInfo: String?
@NSManaged public var networkInfo: String?
@NSManaged public var batteryLevel: Int32
@NSManaged public var dozeModeActive: Bool
@NSManaged public var exactAlarmPermission: Bool
@NSManaged public var notificationPermission: Bool
@NSManaged public var metadata: String?
}
extension NotificationDelivery: Identifiable {
}
// MARK: - NotificationConfig Entity
@objc(NotificationConfig)
public class NotificationConfig: NSManagedObject {
}
extension NotificationConfig {
@nonobjc public class func fetchRequest() -> NSFetchRequest<NotificationConfig> {
return NSFetchRequest<NotificationConfig>(entityName: "NotificationConfig")
}
@NSManaged public var id: String?
@NSManaged public var timesafariDid: String?
@NSManaged public var configType: String?
@NSManaged public var configKey: String?
@NSManaged public var configValue: String?
@NSManaged public var configDataType: String?
@NSManaged public var isEncrypted: Bool
@NSManaged public var encryptionKeyId: String?
@NSManaged public var createdAt: Date?
@NSManaged public var updatedAt: Date?
@NSManaged public var ttlSeconds: Int64
@NSManaged public var isActive: Bool
@NSManaged public var metadata: String?
}
extension NotificationConfig: Identifiable {
}
// MARK: - Persistence Controller
// Phase 2: CoreData integration for advanced features
// Phase 1: Stubbed out - CoreData model not yet created
// All entities now available: ContentCache, Schedule, Callback, History,
// NotificationContent, NotificationDelivery, NotificationConfig
class PersistenceController {
// Lazy initialization to prevent Phase 1 errors
// Lazy initialization
private static var _shared: PersistenceController?
static var shared: PersistenceController {
if _shared == nil {
@@ -131,8 +240,6 @@ class PersistenceController {
private var initializationError: Error?
init(inMemory: Bool = false) {
// Phase 1: CoreData model doesn't exist yet, so we'll handle gracefully
// Phase 2: Will create DailyNotificationModel.xcdatamodeld
var tempContainer: NSPersistentContainer? = nil
do {
@@ -142,12 +249,23 @@ class PersistenceController {
tempContainer?.persistentStoreDescriptions.first?.url = URL(fileURLWithPath: "/dev/null")
}
// Configure persistent store options
let description = tempContainer?.persistentStoreDescriptions.first
description?.shouldMigrateStoreAutomatically = true
description?.shouldInferMappingModelAutomatically = true
var loadError: Error? = nil
tempContainer?.loadPersistentStores { _, error in
tempContainer?.loadPersistentStores { description, error in
if let error = error as NSError? {
loadError = error
print("DNP-PLUGIN: CoreData model not found (Phase 1 - expected). Error: \(error.localizedDescription)")
print("DNP-PLUGIN: CoreData features will be available in Phase 2")
print("DNP-PLUGIN: CoreData store load error: \(error.localizedDescription)")
print("DNP-PLUGIN: Error domain: \(error.domain), code: \(error.code)")
if let failureReason = error.userInfo[NSLocalizedFailureReasonErrorKey] as? String {
print("DNP-PLUGIN: Failure reason: \(failureReason)")
}
} else {
print("DNP-PLUGIN: CoreData store loaded successfully")
print("DNP-PLUGIN: Store URL: \(description.url?.absoluteString ?? "unknown")")
}
}
@@ -155,8 +273,17 @@ class PersistenceController {
self.initializationError = error
self.container = nil
} else {
tempContainer?.viewContext.automaticallyMergesChangesFromParent = true
// Configure view context
if let context = tempContainer?.viewContext {
context.automaticallyMergesChangesFromParent = true
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
}
self.container = tempContainer
// Verify all entities are available (after container is initialized)
if let context = tempContainer?.viewContext {
verifyEntities(in: context)
}
}
} catch {
print("DNP-PLUGIN: Failed to initialize CoreData container: \(error.localizedDescription)")
@@ -166,10 +293,88 @@ class PersistenceController {
}
/**
* Check if CoreData is available (Phase 2+)
* Check if CoreData is available
*/
var isAvailable: Bool {
return container != nil && initializationError == nil
}
/**
* Get the main view context
*
* @return NSManagedObjectContext or nil if not available
*/
var viewContext: NSManagedObjectContext? {
return container?.viewContext
}
/**
* Create a new background context for async operations
*
* @return NSManagedObjectContext or nil if not available
*/
func newBackgroundContext() -> NSManagedObjectContext? {
return container?.newBackgroundContext()
}
/**
* Save the view context
*
* @return true if saved successfully, false otherwise
*/
func save() -> Bool {
guard let context = viewContext else {
print("DNP-PLUGIN: Cannot save - CoreData not available")
return false
}
if context.hasChanges {
do {
try context.save()
print("DNP-PLUGIN: CoreData context saved successfully")
return true
} catch {
print("DNP-PLUGIN: Error saving CoreData context: \(error.localizedDescription)")
context.rollback()
return false
}
}
return true
}
/**
* Verify all entities are available in the model
*
* @param context Managed object context
*/
private func verifyEntities(in context: NSManagedObjectContext) {
guard let model = context.persistentStoreCoordinator?.managedObjectModel else {
print("DNP-PLUGIN: Cannot verify entities - no managed object model")
return
}
let entityNames = [
"ContentCache",
"Schedule",
"Callback",
"History",
"NotificationContent",
"NotificationDelivery",
"NotificationConfig"
]
var missingEntities: [String] = []
for entityName in entityNames {
if model.entitiesByName[entityName] == nil {
missingEntities.append(entityName)
}
}
if missingEntities.isEmpty {
print("DNP-PLUGIN: All \(entityNames.count) entities verified in CoreData model")
} else {
print("DNP-PLUGIN: WARNING - Missing entities: \(missingEntities.joined(separator: ", "))")
}
}
}

View File

@@ -36,4 +36,92 @@
<attribute name="nextRunAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="stateJson" optional="YES" attributeType="String"/>
</entity>
<entity name="NotificationContent" representedClassName="NotificationContentEntity" syncable="YES" codeGenerationType="class">
<attribute name="id" optional="NO" attributeType="String"/>
<attribute name="pluginVersion" optional="YES" attributeType="String"/>
<attribute name="timesafariDid" optional="YES" attributeType="String"/>
<attribute name="notificationType" optional="YES" attributeType="String"/>
<attribute name="title" optional="YES" attributeType="String"/>
<attribute name="body" optional="YES" attributeType="String"/>
<attribute name="scheduledTime" optional="NO" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="timezone" optional="YES" attributeType="String"/>
<attribute name="priority" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="vibrationEnabled" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="soundEnabled" optional="YES" attributeType="Boolean" defaultValueString="YES" usesScalarValueType="YES"/>
<attribute name="mediaUrl" optional="YES" attributeType="String"/>
<attribute name="encryptedContent" optional="YES" attributeType="String"/>
<attribute name="encryptionKeyId" optional="YES" attributeType="String"/>
<attribute name="createdAt" optional="NO" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="updatedAt" optional="NO" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="ttlSeconds" optional="YES" attributeType="Integer 64" defaultValueString="604800" usesScalarValueType="YES"/>
<attribute name="deliveryStatus" optional="YES" attributeType="String"/>
<attribute name="deliveryAttempts" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="lastDeliveryAttempt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="userInteractionCount" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="lastUserInteraction" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="metadata" optional="YES" attributeType="String"/>
<relationship name="deliveries" optional="YES" toMany="YES" deletionRule="Cascade" destinationEntity="NotificationDelivery" inverseName="notificationContent" inverseEntity="NotificationDelivery"/>
<index name="index_notification_content_timesafari_did">
<indexElement value="timesafariDid"/>
</index>
<index name="index_notification_content_notification_type">
<indexElement value="notificationType"/>
</index>
<index name="index_notification_content_scheduled_time">
<indexElement value="scheduledTime"/>
</index>
</entity>
<entity name="NotificationDelivery" representedClassName="NotificationDelivery" syncable="YES" codeGenerationType="class">
<attribute name="id" optional="NO" attributeType="String"/>
<attribute name="notificationId" optional="YES" attributeType="String"/>
<attribute name="timesafariDid" optional="YES" attributeType="String"/>
<attribute name="deliveryTimestamp" optional="NO" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="deliveryStatus" optional="YES" attributeType="String"/>
<attribute name="deliveryMethod" optional="YES" attributeType="String"/>
<attribute name="deliveryAttemptNumber" optional="YES" attributeType="Integer 32" defaultValueString="1" usesScalarValueType="YES"/>
<attribute name="deliveryDurationMs" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="userInteractionType" optional="YES" attributeType="String"/>
<attribute name="userInteractionTimestamp" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="userInteractionDurationMs" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="errorCode" optional="YES" attributeType="String"/>
<attribute name="errorMessage" optional="YES" attributeType="String"/>
<attribute name="deviceInfo" optional="YES" attributeType="String"/>
<attribute name="networkInfo" optional="YES" attributeType="String"/>
<attribute name="batteryLevel" optional="YES" attributeType="Integer 32" defaultValueString="-1" usesScalarValueType="YES"/>
<attribute name="dozeModeActive" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="exactAlarmPermission" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="notificationPermission" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="metadata" optional="YES" attributeType="String"/>
<relationship name="notificationContent" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="NotificationContent" inverseName="deliveries" inverseEntity="NotificationContent"/>
<index name="index_notification_delivery_notification_id">
<indexElement value="notificationId"/>
</index>
<index name="index_notification_delivery_delivery_timestamp">
<indexElement value="deliveryTimestamp"/>
</index>
</entity>
<entity name="NotificationConfig" representedClassName="NotificationConfig" syncable="YES" codeGenerationType="class">
<attribute name="id" optional="NO" attributeType="String"/>
<attribute name="timesafariDid" optional="YES" attributeType="String"/>
<attribute name="configType" optional="YES" attributeType="String"/>
<attribute name="configKey" optional="YES" attributeType="String"/>
<attribute name="configValue" optional="YES" attributeType="String"/>
<attribute name="configDataType" optional="YES" attributeType="String"/>
<attribute name="isEncrypted" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="encryptionKeyId" optional="YES" attributeType="String"/>
<attribute name="createdAt" optional="NO" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="updatedAt" optional="NO" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="ttlSeconds" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="isActive" optional="YES" attributeType="Boolean" defaultValueString="YES" usesScalarValueType="YES"/>
<attribute name="metadata" optional="YES" attributeType="String"/>
<index name="index_notification_config_config_key">
<indexElement value="configKey"/>
</index>
<index name="index_notification_config_config_type">
<indexElement value="configType"/>
</index>
<index name="index_notification_config_timesafari_did">
<indexElement value="timesafariDid"/>
</index>
</entity>
</model>

View File

@@ -35,6 +35,9 @@ public class DailyNotificationPlugin: CAPPlugin {
var storage: DailyNotificationStorage?
var scheduler: DailyNotificationScheduler?
// Phase 1: Reactivation manager for recovery
var reactivationManager: DailyNotificationReactivationManager?
// Phase 1: Concurrency actor for thread-safe state access
@available(iOS 13.0, *)
var stateActor: DailyNotificationStateActor?
@@ -51,6 +54,13 @@ public class DailyNotificationPlugin: CAPPlugin {
storage = DailyNotificationStorage(databasePath: database.getPath())
scheduler = DailyNotificationScheduler()
// Initialize reactivation manager for recovery
reactivationManager = DailyNotificationReactivationManager(
database: database,
storage: storage!,
scheduler: scheduler!
)
// Initialize state actor for thread-safe access
if #available(iOS 13.0, *) {
stateActor = DailyNotificationStateActor(
@@ -59,6 +69,17 @@ public class DailyNotificationPlugin: CAPPlugin {
)
}
// Perform recovery on app launch (async, non-blocking)
reactivationManager?.performRecovery()
// Register for notification delivery events (Notification Center pattern)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleNotificationDelivery(_:)),
name: NSNotification.Name("DailyNotificationDelivered"),
object: nil
)
NSLog("DNP-DEBUG: DailyNotificationPlugin.load() completed - initialization done")
print("DNP-PLUGIN: Daily Notification Plugin loaded on iOS")
}
@@ -144,6 +165,66 @@ public class DailyNotificationPlugin: CAPPlugin {
}
}
/**
* Configure native fetcher with API credentials (cross-platform)
*
* Matches Android configureNativeFetcher() functionality:
* - Stores configuration in database for persistence
* - Supports both jwtToken and jwtSecret for backward compatibility
* - Note: iOS native fetcher interface not yet implemented, but configuration is stored
*
* @param call Plugin call containing configuration parameters
*/
@objc func configureNativeFetcher(_ call: CAPPluginCall) {
guard let options = call.options else {
call.reject("Options are required")
return
}
guard let apiBaseUrl = options["apiBaseUrl"] as? String else {
call.reject("apiBaseUrl is required")
return
}
guard let activeDid = options["activeDid"] as? String else {
call.reject("activeDid is required")
return
}
// Support both jwtToken and jwtSecret for backward compatibility
let jwtToken = (options["jwtToken"] as? String) ?? (options["jwtSecret"] as? String)
guard let jwtToken = jwtToken else {
call.reject("jwtToken or jwtSecret is required")
return
}
print("DNP-PLUGIN: Configuring native fetcher: apiBaseUrl=\(apiBaseUrl), activeDid=\(activeDid.prefix(30))...")
// Store configuration in database for persistence across app restarts
// Note: iOS native fetcher interface not yet implemented, but we store config for future use
let configId = "native_fetcher_config"
let configValue: [String: Any] = [
"apiBaseUrl": apiBaseUrl,
"activeDid": activeDid,
"jwtToken": jwtToken
]
// Convert to JSON string for storage
guard let jsonData = try? JSONSerialization.data(withJSONObject: configValue, options: []),
let jsonString = String(data: jsonData, encoding: .utf8) else {
call.reject("Failed to serialize configuration")
return
}
// Store configuration in UserDefaults for now
// This matches Android's approach of storing in database, but uses UserDefaults for simplicity
// Can be enhanced later to use CoreData when native fetcher interface is implemented
let configKey = "native_fetcher_config"
UserDefaults.standard.set(jsonString, forKey: configKey)
print("DNP-PLUGIN: Native fetcher configuration stored successfully")
call.resolve()
}
/**
* Store configuration values
*
@@ -336,15 +417,27 @@ public class DailyNotificationPlugin: CAPPlugin {
* Phase 1: Dummy fetcher - returns static content
* Phase 3: Will be replaced with JWT-signed fetcher
*
* Enhanced with:
* - Recovery logic (verify scheduled notifications)
* - Next task scheduling
* - Graceful expiration handling
*
* @param task BGAppRefreshTask
*/
private func handleBackgroundFetch(task: BGAppRefreshTask) {
print("DNP-FETCH: Background fetch task started")
// Set expiration handler
// Enhanced expiration handler with graceful cleanup
var taskCompleted = false
task.expirationHandler = {
print("DNP-FETCH: Background fetch task expired")
guard !taskCompleted else { return }
print("DNP-FETCH: Background fetch task expired - performing graceful cleanup")
// Cancel any ongoing operations
// Note: In production, you might want to cancel URLSession tasks here
task.setTaskCompleted(success: false)
taskCompleted = true
}
// Phase 1: Dummy content fetch (no network)
@@ -362,6 +455,7 @@ public class DailyNotificationPlugin: CAPPlugin {
// Save content to storage via state actor (thread-safe)
Task {
do {
if #available(iOS 13.0, *) {
if let stateActor = await self.stateActor {
await stateActor.saveNotificationContent(dummyContent)
@@ -381,34 +475,107 @@ public class DailyNotificationPlugin: CAPPlugin {
let currentTime = Int64(Date().timeIntervalSince1970 * 1000)
self.storage?.saveLastSuccessfulRun(timestamp: currentTime)
}
// Phase 3.3: Recovery logic - verify scheduled notifications
// Check if notifications are still scheduled after fetch
if let reactivationManager = self.reactivationManager {
// Perform lightweight verification (non-blocking)
Task {
do {
let verificationResult = try await reactivationManager.verifyFutureNotifications()
if verificationResult.notificationsMissing > 0 {
print("DNP-FETCH: Recovery - found \(verificationResult.notificationsMissing) missing notifications, will reschedule on next app launch")
// Note: Full recovery happens on app launch, not in background task
}
} catch {
// Non-fatal: Log but don't fail task
print("DNP-FETCH: Recovery verification failed (non-fatal): \(error.localizedDescription)")
}
}
}
// Schedule next fetch
// TODO: Calculate next fetch time based on notification schedule
// Phase 3.3: Schedule next background task
// Calculate next fetch time based on notification schedule
if let nextScheduledTime = self.getNextScheduledNotificationTime() {
self.scheduleBackgroundFetch(scheduledTime: nextScheduledTime)
print("DNP-FETCH: Next background fetch scheduled")
} else {
print("DNP-FETCH: No future notifications found, skipping next task schedule")
}
print("DNP-FETCH: Background fetch task completed successfully")
guard !taskCompleted else { return }
task.setTaskCompleted(success: true)
taskCompleted = true
print("DNP-FETCH: Background fetch task completed successfully")
} catch {
print("DNP-FETCH: Background fetch task failed: \(error.localizedDescription)")
guard !taskCompleted else { return }
task.setTaskCompleted(success: false)
taskCompleted = true
}
}
}
/**
* Handle background notification task
*
* Enhanced with:
* - Recovery logic (verify scheduled notifications)
* - Next task scheduling
* - Graceful expiration handling
*
* @param task BGProcessingTask
*/
private func handleBackgroundNotify(task: BGProcessingTask) {
print("DNP-NOTIFY: Background notify task started")
// Set expiration handler
// Enhanced expiration handler with graceful cleanup
var taskCompleted = false
task.expirationHandler = {
print("DNP-NOTIFY: Background notify task expired")
guard !taskCompleted else { return }
print("DNP-NOTIFY: Background notify task expired - performing graceful cleanup")
task.setTaskCompleted(success: false)
taskCompleted = true
}
Task {
do {
// Phase 3.3: Recovery logic - verify scheduled notifications
// Check if notifications are still scheduled
if let reactivationManager = self.reactivationManager {
// Perform lightweight verification (non-blocking)
let verificationResult = try await reactivationManager.verifyFutureNotifications()
if verificationResult.notificationsMissing > 0 {
print("DNP-NOTIFY: Recovery - found \(verificationResult.notificationsMissing) missing notifications, will reschedule on next app launch")
// Note: Full recovery happens on app launch, not in background task
}
}
// Phase 1: Not used for single daily schedule
// This will be used in Phase 2+ for rolling window maintenance
// For now, just verify state
print("DNP-NOTIFY: Background notify task completed")
// Phase 3.3: Schedule next background task if needed
// For notify task, schedule next occurrence if applicable
if let nextScheduledTime = self.getNextScheduledNotificationTime() {
// Calculate next notify task time (if applicable)
// Note: Notify tasks are typically scheduled less frequently than fetch tasks
print("DNP-NOTIFY: Next notification scheduled at \(nextScheduledTime)")
}
guard !taskCompleted else { return }
task.setTaskCompleted(success: true)
taskCompleted = true
print("DNP-NOTIFY: Background notify task completed successfully")
} catch {
print("DNP-NOTIFY: Background notify task failed: \(error.localizedDescription)")
guard !taskCompleted else { return }
task.setTaskCompleted(success: false)
taskCompleted = true
}
}
}
/**
@@ -909,6 +1076,38 @@ public class DailyNotificationPlugin: CAPPlugin {
// Store notification content via state actor (thread-safe)
Task {
// Reset: Cancel all existing notifications and clear rollover state
// This ensures clicking "Test Notification" starts fresh
// Cancel all pending notifications (including rollovers)
await scheduler.cancelAllNotifications()
NSLog("DNP-PLUGIN: Cleared all pending notifications for fresh schedule")
print("DNP-PLUGIN: Cleared all pending notifications for fresh schedule")
// Clear all stored notification content
if let storage = self.storage {
storage.clearAllNotifications()
NSLog("DNP-PLUGIN: Cleared all stored notification content")
print("DNP-PLUGIN: Cleared all stored notification content")
}
// Clear rollover state from UserDefaults
// Clear global rollover time
if let storage = self.storage {
storage.saveLastRolloverTime(0)
}
// Clear per-notification rollover times
// We need to clear all rollover_* keys from UserDefaults
let userDefaults = UserDefaults.standard
let allKeys = userDefaults.dictionaryRepresentation().keys
for key in allKeys {
if key.hasPrefix("rollover_") {
userDefaults.removeObject(forKey: key)
}
}
userDefaults.synchronize()
NSLog("DNP-PLUGIN: Cleared all rollover state")
print("DNP-PLUGIN: Cleared all rollover state")
if #available(iOS 13.0, *) {
if let stateActor = await self.stateActor {
await stateActor.saveNotificationContent(content)
@@ -1067,12 +1266,17 @@ public class DailyNotificationPlugin: CAPPlugin {
// Calculate next notification time
let nextNotificationTime = await scheduler.getNextNotificationTime() ?? 0
// Get rollover status
let lastRolloverTime = storage?.getLastRolloverTime() ?? 0
var result: [String: Any] = [
"isEnabled": isEnabled,
"isScheduled": pendingCount > 0,
"lastNotificationTime": lastNotification?.scheduledTime ?? 0,
"nextNotificationTime": nextNotificationTime,
"pending": pendingCount,
"rolloverEnabled": true, // Indicate rollover is active
"lastRolloverTime": lastRolloverTime, // When last rollover occurred
"settings": settings
]
@@ -1082,6 +1286,93 @@ public class DailyNotificationPlugin: CAPPlugin {
}
}
/**
* Handle notification delivery event (from Notification Center)
*
* This is called when AppDelegate posts notification delivery event
* Matches Android's scheduleNextNotification() behavior
*
* @param notification NSNotification with userInfo containing notification_id and scheduled_time
*/
@objc private func handleNotificationDelivery(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let notificationId = userInfo["notification_id"] as? String,
let scheduledTime = userInfo["scheduled_time"] as? Int64 else {
NSLog("DNP-ROLLOVER: INVALID_DATA userInfo=\(String(describing: notification.userInfo))")
print("DNP-ROLLOVER: INVALID_DATA userInfo=\(String(describing: notification.userInfo))")
return
}
let scheduledTimeStr = formatTime(scheduledTime)
NSLog("DNP-ROLLOVER: DELIVERY_DETECTED id=\(notificationId) scheduled_time=\(scheduledTimeStr)")
print("DNP-ROLLOVER: DELIVERY_DETECTED id=\(notificationId) scheduled_time=\(scheduledTimeStr)")
Task {
await processRollover(notificationId: notificationId, scheduledTime: scheduledTime)
}
}
/**
* Process rollover for delivered notification
*
* @param notificationId ID of notification that was delivered
* @param scheduledTime Scheduled time of delivered notification
*/
private func processRollover(notificationId: String, scheduledTime: Int64) async {
let scheduledTimeStr = formatTime(scheduledTime)
NSLog("DNP-ROLLOVER: PROCESS_START id=\(notificationId) scheduled_time=\(scheduledTimeStr)")
print("DNP-ROLLOVER: PROCESS_START id=\(notificationId) scheduled_time=\(scheduledTimeStr)")
guard let scheduler = scheduler, let storage = storage else {
NSLog("DNP-ROLLOVER: ERROR id=\(notificationId) plugin_not_initialized")
print("DNP-ROLLOVER: ERROR id=\(notificationId) plugin_not_initialized")
return
}
// Get the notification content that was delivered
guard let content = storage.getNotificationContent(id: notificationId) else {
NSLog("DNP-ROLLOVER: ERROR id=\(notificationId) content_not_found")
print("DNP-ROLLOVER: ERROR id=\(notificationId) content_not_found")
return
}
let contentTimeStr = formatTime(content.scheduledTime)
NSLog("DNP-ROLLOVER: CONTENT_FOUND id=\(notificationId) content_scheduled_time=\(contentTimeStr)")
print("DNP-ROLLOVER: CONTENT_FOUND id=\(notificationId) content_scheduled_time=\(contentTimeStr)")
// Schedule next notification
// Note: DailyNotificationFetcher integration deferred to Phase 2
let scheduled = await scheduler.scheduleNextNotification(
content,
storage: storage,
fetcher: nil // TODO: Phase 2 - Add fetcher instance
)
if scheduled {
NSLog("DNP-ROLLOVER: PROCESS_SUCCESS id=\(notificationId) next_notification_scheduled")
print("DNP-ROLLOVER: PROCESS_SUCCESS id=\(notificationId) next_notification_scheduled")
// Log success (non-fatal, background operation)
} else {
NSLog("DNP-ROLLOVER: PROCESS_FAILED id=\(notificationId) next_notification_not_scheduled")
print("DNP-ROLLOVER: PROCESS_FAILED id=\(notificationId) next_notification_not_scheduled")
// Log failure but continue (recovery will handle on next launch)
}
}
/**
* Format time for logging
*
* @param timestamp Timestamp in milliseconds
* @return Formatted time string
*/
private func formatTime(_ timestamp: Int64) -> String {
let date = Date(timeIntervalSince1970: Double(timestamp) / 1000.0)
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .short
return formatter.string(from: date)
}
/**
* Check permission status
* Returns boolean flags for each permission type
@@ -1259,6 +1550,211 @@ public class DailyNotificationPlugin: CAPPlugin {
}
}
// MARK: - iOS-Specific Methods
/**
* Get notification permission status (iOS-specific)
*
* Returns detailed permission status matching API.md specification
*
* @param call Plugin call
*/
@objc func getNotificationPermissionStatus(_ call: CAPPluginCall) {
Task {
do {
guard let scheduler = scheduler else {
throw NSError(domain: "DailyNotificationPlugin", code: -1, userInfo: [NSLocalizedDescriptionKey: "Scheduler not initialized"])
}
let status = await scheduler.checkPermissionStatus()
// Map to iOS-specific error if denied
if status == .denied {
let error = DailyNotificationErrorCodes.notificationPermissionDenied()
let errorMessage = error["message"] as? String ?? "Notification permission denied"
let errorCode = error["error"] as? String ?? DailyNotificationErrorCodes.NOTIFICATION_PERMISSION_DENIED
DispatchQueue.main.async {
call.reject(errorMessage, errorCode)
}
return
}
let result: [String: Any] = [
"authorized": status == .authorized,
"denied": status == .denied,
"notDetermined": status == .notDetermined,
"provisional": status == .provisional
]
DispatchQueue.main.async {
call.resolve(result)
}
} catch {
DispatchQueue.main.async {
call.reject("Failed to get permission status: \(error.localizedDescription)", "permission_status_failed")
}
}
}
}
/**
* Request notification permission (iOS-specific)
*
* @param call Plugin call
*/
@objc func requestNotificationPermission(_ call: CAPPluginCall) {
Task {
do {
guard let scheduler = scheduler else {
throw NSError(domain: "DailyNotificationPlugin", code: -1, userInfo: [NSLocalizedDescriptionKey: "Scheduler not initialized"])
}
let granted = await scheduler.requestPermissions()
let result: [String: Any] = [
"granted": granted
]
DispatchQueue.main.async {
call.resolve(result)
}
} catch {
DispatchQueue.main.async {
call.reject("Failed to request permission: \(error.localizedDescription)", "permission_request_failed")
}
}
}
}
/**
* Get pending notifications (iOS-specific)
*
* @param call Plugin call
*/
@objc func getPendingNotifications(_ call: CAPPluginCall) {
Task {
do {
let requests = try await notificationCenter.pendingNotificationRequests()
var notifications: [[String: Any]] = []
for request in requests {
let content = request.content
var triggerDate: Int64 = 0
if let calendarTrigger = request.trigger as? UNCalendarNotificationTrigger {
if let nextDate = calendarTrigger.nextTriggerDate() {
triggerDate = Int64(nextDate.timeIntervalSince1970 * 1000)
}
} else if let timeIntervalTrigger = request.trigger as? UNTimeIntervalNotificationTrigger {
if let nextDate = timeIntervalTrigger.nextTriggerDate() {
triggerDate = Int64(nextDate.timeIntervalSince1970 * 1000)
}
}
let notification: [String: Any] = [
"identifier": request.identifier,
"title": content.title,
"body": content.body,
"triggerDate": triggerDate,
"triggerType": request.trigger is UNCalendarNotificationTrigger ? "calendar" : (request.trigger is UNTimeIntervalNotificationTrigger ? "timeInterval" : "location"),
"repeats": request.trigger?.repeats ?? false
]
notifications.append(notification)
}
let result: [String: Any] = [
"count": notifications.count,
"notifications": notifications
]
DispatchQueue.main.async {
call.resolve(result)
}
} catch {
DispatchQueue.main.async {
call.reject("Failed to get pending notifications: \(error.localizedDescription)", "pending_notifications_failed")
}
}
}
}
/**
* Get background task status (iOS-specific)
*
* @param call Plugin call
*/
@objc func getBackgroundTaskStatus(_ call: CAPPluginCall) {
// Note: BGTaskScheduler doesn't provide a way to query registered task identifiers
// We assume tasks are registered if setupBackgroundTasks() was called
// Background App Refresh status cannot be checked programmatically
// User must check in Settings app
let result: [String: Any] = [
"fetchTaskRegistered": true, // Assumed registered if setupBackgroundTasks() was called
"notifyTaskRegistered": true, // Assumed registered if setupBackgroundTasks() was called
"lastFetchExecution": storage?.getLastSuccessfulRun() ?? NSNull(),
"lastNotifyExecution": NSNull(), // TODO: Track notify execution
"backgroundRefreshEnabled": NSNull() // Cannot check programmatically
]
call.resolve(result)
}
/**
* Open notification settings (iOS-specific)
*
* @param call Plugin call
*/
@objc func openNotificationSettings(_ call: CAPPluginCall) {
if let settingsUrl = URL(string: UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl) { success in
DispatchQueue.main.async {
if success {
call.resolve()
} else {
call.reject("Failed to open notification settings", "open_settings_failed")
}
}
}
} else {
call.reject("Cannot open settings URL", "open_settings_failed")
}
} else {
call.reject("Invalid settings URL", "open_settings_failed")
}
}
/**
* Open Background App Refresh settings (iOS-specific)
*
* Note: iOS doesn't provide a direct URL to Background App Refresh settings.
* This opens the app's settings page where user can find Background App Refresh.
*
* @param call Plugin call
*/
@objc func openBackgroundAppRefreshSettings(_ call: CAPPluginCall) {
// iOS doesn't have a direct URL to Background App Refresh settings
// Open app settings instead, where user can find Background App Refresh
if let settingsUrl = URL(string: UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl) { success in
DispatchQueue.main.async {
if success {
call.resolve()
} else {
call.reject("Failed to open settings", "open_settings_failed")
}
}
}
} else {
call.reject("Cannot open settings URL", "open_settings_failed")
}
} else {
call.reject("Invalid settings URL", "open_settings_failed")
}
}
// MARK: - Channel Methods (iOS Parity with Android)
/**
@@ -1382,6 +1878,34 @@ public class DailyNotificationPlugin: CAPPlugin {
// MARK: - Phase 1: Helper Methods
/**
* Get next scheduled notification time
*
* Helper method to get the next scheduled notification time for
* scheduling background tasks. Uses async/await internally.
*
* @return Next scheduled notification time in milliseconds (Int64), or nil if none
*/
private func getNextScheduledNotificationTime() -> Int64? {
guard let scheduler = scheduler else {
return nil
}
// Use async helper to get next notification time
// Note: This is called from background task handlers which are already async
var nextTime: Int64? = nil
let semaphore = DispatchSemaphore(value: 0)
Task {
nextTime = await scheduler.getNextNotificationTime()
semaphore.signal()
}
// Wait with timeout (2 seconds - background tasks have limited time)
_ = semaphore.wait(timeout: .now() + 2.0)
return nextTime
}
/**
* Calculate next scheduled time for given hour and minute
*
@@ -1484,6 +2008,7 @@ public class DailyNotificationPlugin: CAPPlugin {
// Core methods
methods.append(CAPPluginMethod(name: "configure", returnType: CAPPluginReturnPromise))
methods.append(CAPPluginMethod(name: "configureNativeFetcher", returnType: CAPPluginReturnPromise))
methods.append(CAPPluginMethod(name: "scheduleDailyNotification", returnType: CAPPluginReturnPromise))
methods.append(CAPPluginMethod(name: "getLastNotification", returnType: CAPPluginReturnPromise))
methods.append(CAPPluginMethod(name: "cancelAllNotifications", returnType: CAPPluginReturnPromise))
@@ -1494,6 +2019,14 @@ public class DailyNotificationPlugin: CAPPlugin {
methods.append(CAPPluginMethod(name: "checkPermissionStatus", returnType: CAPPluginReturnPromise))
methods.append(CAPPluginMethod(name: "requestNotificationPermissions", returnType: CAPPluginReturnPromise))
// iOS-specific methods
methods.append(CAPPluginMethod(name: "getNotificationPermissionStatus", returnType: CAPPluginReturnPromise))
methods.append(CAPPluginMethod(name: "requestNotificationPermission", returnType: CAPPluginReturnPromise))
methods.append(CAPPluginMethod(name: "getPendingNotifications", returnType: CAPPluginReturnPromise))
methods.append(CAPPluginMethod(name: "getBackgroundTaskStatus", returnType: CAPPluginReturnPromise))
methods.append(CAPPluginMethod(name: "openNotificationSettings", returnType: CAPPluginReturnPromise))
methods.append(CAPPluginMethod(name: "openBackgroundAppRefreshSettings", returnType: CAPPluginReturnPromise))
// Channel methods (iOS parity with Android)
methods.append(CAPPluginMethod(name: "isChannelEnabled", returnType: CAPPluginReturnPromise))
methods.append(CAPPluginMethod(name: "openChannelSettings", returnType: CAPPluginReturnPromise))

File diff suppressed because it is too large Load Diff

View File

@@ -188,7 +188,11 @@ class DailyNotificationScheduler {
self.scheduledNotifications.insert(content.id)
}
print("\(Self.TAG): Notification scheduled successfully for \(scheduledDate)")
// Log pending count for test scripts (matches Android's alarm count logging)
// Use NSLog to ensure it appears in system logs (print() may not always be captured)
let pendingCount = await getPendingNotificationCount()
NSLog("\(Self.TAG): Notification scheduled successfully for \(scheduledDate), id=\(content.id), pendingCount=\(pendingCount)")
print("\(Self.TAG): Notification scheduled successfully for \(scheduledDate), id=\(content.id), pendingCount=\(pendingCount)")
return true
} catch {
@@ -310,12 +314,251 @@ class DailyNotificationScheduler {
func getNextNotificationTime() async -> Int64? {
let requests = await notificationCenter.pendingNotificationRequests()
guard let trigger = requests.first?.trigger as? UNCalendarNotificationTrigger,
let nextDate = trigger.nextTriggerDate() else {
// Find the earliest scheduled notification by checking all requests
var earliestDate: Date? = nil
var earliestRequestId: String? = nil
var allTimes: [(String, String)] = []
for request in requests {
var requestTime: Date? = nil
if let trigger = request.trigger as? UNCalendarNotificationTrigger,
let nextDate = trigger.nextTriggerDate() {
requestTime = nextDate
} else if let trigger = request.trigger as? UNTimeIntervalNotificationTrigger,
let nextDate = trigger.nextTriggerDate() {
requestTime = nextDate
}
if let time = requestTime {
let timeStr = formatTime(Int64(time.timeIntervalSince1970 * 1000))
allTimes.append((request.identifier, timeStr))
if earliestDate == nil || time < earliestDate! {
earliestDate = time
earliestRequestId = request.identifier
}
}
}
guard let nextDate = earliestDate else {
NSLog("DNP-ROLLOVER: GET_NEXT_TIME no_pending_requests")
print("DNP-ROLLOVER: GET_NEXT_TIME no_pending_requests")
return nil
}
return Int64(nextDate.timeIntervalSince1970 * 1000)
let nextTime = Int64(nextDate.timeIntervalSince1970 * 1000)
let nextTimeStr = formatTime(nextTime)
let allTimesStr = allTimes.map { "\($0.0):\($0.1)" }.joined(separator: ", ")
NSLog("DNP-ROLLOVER: GET_NEXT_TIME found=\(nextTimeStr) id=\(earliestRequestId ?? "unknown") from \(requests.count) pending: [\(allTimesStr)]")
print("DNP-ROLLOVER: GET_NEXT_TIME found=\(nextTimeStr) id=\(earliestRequestId ?? "unknown") from \(requests.count) pending: [\(allTimesStr)]")
return nextTime
}
/**
* Calculate next scheduled time from current scheduled time (24 hours later, DST-safe)
*
* Matches Android calculateNextScheduledTime() functionality
* Handles DST transitions automatically using Calendar
*
* @param currentScheduledTime Current scheduled time in milliseconds
* @return Next scheduled time in milliseconds (24 hours later)
*/
func calculateNextScheduledTime(_ currentScheduledTime: Int64) -> Int64 {
let calendar = Calendar.current
let currentDate = Date(timeIntervalSince1970: Double(currentScheduledTime) / 1000.0)
let currentTimeStr = formatTime(currentScheduledTime)
// Add 24 hours (handles DST transitions automatically)
guard let nextDate = calendar.date(byAdding: .hour, value: 24, to: currentDate) else {
// Fallback to simple 24-hour addition if calendar calculation fails
let fallbackTime = currentScheduledTime + (24 * 60 * 60 * 1000)
let fallbackTimeStr = formatTime(fallbackTime)
NSLog("DNP-ROLLOVER: DST_CALC_FAILED current=\(currentTimeStr) using_fallback=\(fallbackTimeStr)")
print("DNP-ROLLOVER: DST_CALC_FAILED current=\(currentTimeStr) using_fallback=\(fallbackTimeStr)")
return fallbackTime
}
let nextTime = Int64(nextDate.timeIntervalSince1970 * 1000)
let nextTimeStr = formatTime(nextTime)
// Validate: Log DST transitions for debugging
let currentHour = calendar.component(.hour, from: currentDate)
let currentMinute = calendar.component(.minute, from: currentDate)
let nextHour = calendar.component(.hour, from: nextDate)
let nextMinute = calendar.component(.minute, from: nextDate)
if currentHour != nextHour || currentMinute != nextMinute {
NSLog("DNP-ROLLOVER: DST_TRANSITION current=\(currentHour):\(String(format: "%02d", currentMinute)) next=\(nextHour):\(String(format: "%02d", nextMinute))")
print("DNP-ROLLOVER: DST_TRANSITION current=\(currentHour):\(String(format: "%02d", currentMinute)) next=\(nextHour):\(String(format: "%02d", nextMinute))")
}
// Log the calculation result
let timeDiffMs = nextTime - currentScheduledTime
let timeDiffHours = Double(timeDiffMs) / 1000.0 / 60.0 / 60.0
NSLog("DNP-ROLLOVER: CALC_NEXT current=\(currentTimeStr) next=\(nextTimeStr) diff_hours=\(String(format: "%.2f", timeDiffHours))")
print("DNP-ROLLOVER: CALC_NEXT current=\(currentTimeStr) next=\(nextTimeStr) diff_hours=\(String(format: "%.2f", timeDiffHours))")
return nextTime
}
/**
* Schedule next notification after current one fires (rollover)
*
* Matches Android scheduleNextNotification() functionality
* Implements multi-level duplicate prevention
*
* @param content Current notification content that just fired
* @param storage Storage instance for duplicate checking
* @param fetcher Optional fetcher for scheduling prefetch (Phase 2)
* @return true if next notification was scheduled successfully
*/
func scheduleNextNotification(
_ content: NotificationContent,
storage: DailyNotificationStorage?,
fetcher: Any? = nil
) async -> Bool {
let currentTime = Int64(Date().timeIntervalSince1970 * 1000)
let currentTimeStr = formatTime(currentTime)
let currentScheduledTimeStr = formatTime(content.scheduledTime)
NSLog("DNP-ROLLOVER: START id=\(content.id) current_time=\(currentTimeStr) scheduled_time=\(currentScheduledTimeStr)")
print("DNP-ROLLOVER: START id=\(content.id) current_time=\(currentTimeStr) scheduled_time=\(currentScheduledTimeStr)")
// Check 1: Rollover state tracking (prevent duplicate rollover attempts)
if let storage = storage {
let lastRolloverTime = await storage.getLastRolloverTime(for: content.id)
// If rollover was processed recently (< 1 hour ago), skip
if let lastTime = lastRolloverTime,
(currentTime - lastTime) < (60 * 60 * 1000) {
let lastTimeStr = formatTime(lastTime)
let timeSinceRollover = (currentTime - lastTime) / 1000 / 60 // minutes
NSLog("DNP-ROLLOVER: SKIP id=\(content.id) already_processed last_rollover=\(lastTimeStr) \(timeSinceRollover)min_ago")
print("DNP-ROLLOVER: SKIP id=\(content.id) already_processed last_rollover=\(lastTimeStr) \(timeSinceRollover)min_ago")
return false
}
}
// Calculate next occurrence using DST-safe calculation
let nextScheduledTime = calculateNextScheduledTime(content.scheduledTime)
let nextScheduledTimeStr = formatTime(nextScheduledTime)
let hoursUntilNext = Double(nextScheduledTime - currentTime) / 1000.0 / 60.0 / 60.0
NSLog("DNP-ROLLOVER: CALCULATED id=\(content.id) next_time=\(nextScheduledTimeStr) hours_until=\(String(format: "%.2f", hoursUntilNext))")
print("DNP-ROLLOVER: CALCULATED id=\(content.id) next_time=\(nextScheduledTimeStr) hours_until=\(String(format: "%.2f", hoursUntilNext))")
// Check 2: Storage-level duplicate check (prevent duplicate notifications)
if let storage = storage {
let existingNotifications = storage.getAllNotifications()
let toleranceMs: Int64 = 60 * 1000 // 1 minute tolerance for DST shifts
for existing in existingNotifications {
if abs(existing.scheduledTime - nextScheduledTime) <= toleranceMs {
let existingTimeStr = formatTime(existing.scheduledTime)
let timeDiffMs = abs(existing.scheduledTime - nextScheduledTime)
NSLog("DNP-ROLLOVER: DUPLICATE_STORAGE id=\(content.id) existing_id=\(existing.id) existing_time=\(existingTimeStr) time_diff_ms=\(timeDiffMs)")
print("DNP-ROLLOVER: DUPLICATE_STORAGE id=\(content.id) existing_id=\(existing.id) existing_time=\(existingTimeStr) time_diff_ms=\(timeDiffMs)")
return false // Skip rescheduling to prevent duplicate
}
}
}
// Check 3: System-level duplicate check (query UNUserNotificationCenter)
let pendingNotifications = await notificationCenter.pendingNotificationRequests()
NSLog("DNP-ROLLOVER: CHECK_SYSTEM id=\(content.id) pending_count=\(pendingNotifications.count)")
print("DNP-ROLLOVER: CHECK_SYSTEM id=\(content.id) pending_count=\(pendingNotifications.count)")
for pending in pendingNotifications {
if let trigger = pending.trigger as? UNCalendarNotificationTrigger,
let nextDate = trigger.nextTriggerDate() {
let pendingTime = Int64(nextDate.timeIntervalSince1970 * 1000)
let toleranceMs: Int64 = 60 * 1000
if abs(pendingTime - nextScheduledTime) <= toleranceMs {
let pendingTimeStr = formatTime(pendingTime)
let timeDiffMs = abs(pendingTime - nextScheduledTime)
NSLog("DNP-ROLLOVER: DUPLICATE_SYSTEM id=\(content.id) system_pending_id=\(pending.identifier) pending_time=\(pendingTimeStr) time_diff_ms=\(timeDiffMs)")
print("DNP-ROLLOVER: DUPLICATE_SYSTEM id=\(content.id) system_pending_id=\(pending.identifier) pending_time=\(pendingTimeStr) time_diff_ms=\(timeDiffMs)")
return false
}
}
}
// Extract hour:minute from current scheduled time for logging
let calendar = Calendar.current
let scheduledDate = content.getScheduledTimeAsDate()
let hour = calendar.component(.hour, from: scheduledDate)
let minute = calendar.component(.minute, from: scheduledDate)
// Create new notification content for next occurrence
// Note: Content will be refreshed by prefetch, but we need placeholder
let nextId = "daily_rollover_\(Int64(Date().timeIntervalSince1970 * 1000))"
let nextContent = NotificationContent(
id: nextId,
title: content.title, // Will be updated by prefetch
body: content.body, // Will be updated by prefetch
scheduledTime: nextScheduledTime,
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: content.url,
payload: content.payload,
etag: content.etag
)
// Schedule the next notification
NSLog("DNP-ROLLOVER: SCHEDULING id=\(content.id) next_id=\(nextId) next_time=\(nextScheduledTimeStr)")
print("DNP-ROLLOVER: SCHEDULING id=\(content.id) next_id=\(nextId) next_time=\(nextScheduledTimeStr)")
let scheduled = await scheduleNotification(nextContent)
if scheduled {
// Verify the notification was actually scheduled
let pendingCount = await getPendingNotificationCount()
let isScheduled = await isNotificationScheduled(id: nextId)
NSLog("DNP-ROLLOVER: SUCCESS id=\(content.id) next_id=\(nextId) next_time=\(nextScheduledTimeStr) pending_count=\(pendingCount) is_scheduled=\(isScheduled)")
print("DNP-ROLLOVER: SUCCESS id=\(content.id) next_id=\(nextId) next_time=\(nextScheduledTimeStr) pending_count=\(pendingCount) is_scheduled=\(isScheduled)")
// Log time difference verification
let timeDiffMs = nextScheduledTime - content.scheduledTime
let timeDiffHours = Double(timeDiffMs) / 1000.0 / 60.0 / 60.0
NSLog("DNP-ROLLOVER: TIME_VERIFY id=\(content.id) current=\(currentScheduledTimeStr) next=\(nextScheduledTimeStr) diff_hours=\(String(format: "%.2f", timeDiffHours))")
print("DNP-ROLLOVER: TIME_VERIFY id=\(content.id) current=\(currentScheduledTimeStr) next=\(nextScheduledTimeStr) diff_hours=\(String(format: "%.2f", timeDiffHours))")
// Schedule background fetch for next notification (5 minutes before scheduled time)
// Note: DailyNotificationFetcher integration deferred to Phase 2
if fetcher != nil {
let fetchTime = nextScheduledTime - (5 * 60 * 1000) // 5 minutes before
let currentTime = Int64(Date().timeIntervalSince1970 * 1000)
if fetchTime > currentTime {
// TODO: Phase 2 - Implement fetcher.scheduleFetch(fetchTime)
NSLog("DNP-ROLLOVER: PREFETCH_SCHEDULED id=\(content.id) next_fetch=\(fetchTime) next_notification=\(nextScheduledTime)")
print("DNP-ROLLOVER: PREFETCH_SCHEDULED id=\(content.id) next_fetch=\(fetchTime) next_notification=\(nextScheduledTime)")
} else {
// TODO: Phase 2 - Implement fetcher.scheduleImmediateFetch()
NSLog("DNP-ROLLOVER: PREFETCH_PAST id=\(content.id) fetch_time=\(fetchTime) current=\(currentTime)")
print("DNP-ROLLOVER: PREFETCH_PAST id=\(content.id) fetch_time=\(fetchTime) current=\(currentTime)")
}
} else {
NSLog("DNP-ROLLOVER: PREFETCH_SKIP id=\(content.id) fetcher_not_available")
print("DNP-ROLLOVER: PREFETCH_SKIP id=\(content.id) fetcher_not_available")
}
// Mark rollover as processed
let rolloverProcessedTime = Int64(Date().timeIntervalSince1970 * 1000)
await storage?.saveLastRolloverTime(for: content.id, time: rolloverProcessedTime)
NSLog("DNP-ROLLOVER: COMPLETE id=\(content.id) next_id=\(nextId) next_time=\(nextScheduledTimeStr)")
print("DNP-ROLLOVER: COMPLETE id=\(content.id) next_id=\(nextId) next_time=\(nextScheduledTimeStr)")
return true
} else {
NSLog("DNP-ROLLOVER: ERROR id=\(content.id) scheduling_failed next_time=\(nextScheduledTimeStr)")
print("DNP-ROLLOVER: ERROR id=\(content.id) scheduling_failed next_time=\(nextScheduledTimeStr)")
return false
}
}
}

View File

@@ -151,6 +151,51 @@ class DailyNotificationStorage {
}
}
/**
* Get last rollover time for a notification ID
*
* @param notificationId Notification ID
* @return Last rollover time in milliseconds, or nil if never rolled over
*/
func getLastRolloverTime(for notificationId: String) async -> Int64? {
let key = "rollover_\(notificationId)"
let lastTime = userDefaults.object(forKey: key) as? Int64
return lastTime
}
/**
* Save last rollover time for a notification ID
*
* @param notificationId Notification ID
* @param time Rollover time in milliseconds
*/
func saveLastRolloverTime(for notificationId: String, time: Int64) async {
let key = "rollover_\(notificationId)"
userDefaults.set(time, forKey: key)
userDefaults.synchronize()
}
/**
* Get last rollover time (any notification)
*
* @return Last rollover time in milliseconds, or 0 if never rolled over
*/
func getLastRolloverTime() -> Int64 {
let key = "rollover_last"
return Int64(userDefaults.integer(forKey: key))
}
/**
* Save last rollover time (any notification)
*
* @param time Rollover time in milliseconds
*/
func saveLastRolloverTime(_ time: Int64) {
let key = "rollover_last"
userDefaults.set(time, forKey: key)
userDefaults.synchronize()
}
/**
* Get notifications that are ready to be displayed
*

300
ios/Plugin/HistoryDAO.swift Normal file
View File

@@ -0,0 +1,300 @@
/**
* HistoryDAO.swift
*
* Data Access Object (DAO) for History Core Data entity
* Provides helper methods for recording recovery and operation history
*
* @author Matthew Raymer
* @version 1.0.0
* @created 2025-12-08
*/
import Foundation
import CoreData
/**
* Extension providing DAO methods for History entity
*
* This extension adds helper methods for recording operation history
* including recovery operations, errors, and metrics.
*/
extension History {
// MARK: - Constants
private static let TAG = "DNP-HISTORY-DAO"
// MARK: - Create/Insert Methods
/**
* Create a new History entity in the given context
*
* @param context Core Data managed object context
* @param id Unique history identifier (UUID recommended)
* @param refId Reference ID (e.g., notification ID, schedule ID)
* @param kind History kind (e.g., "recovery", "fetch", "notify")
* @param occurredAt When the event occurred
* @param durationMs Duration in milliseconds
* @param outcome Outcome string (e.g., "success", "failure", "skipped")
* @param diagJson Diagnostic JSON string with additional details
* @return Created History entity
*/
static func create(
in context: NSManagedObjectContext,
id: String,
refId: String? = nil,
kind: String,
occurredAt: Date,
durationMs: Int32 = 0,
outcome: String,
diagJson: String? = nil
) -> History {
let entity = History(context: context)
entity.id = id
entity.refId = refId
entity.kind = kind
entity.occurredAt = occurredAt
entity.durationMs = durationMs
entity.outcome = outcome
entity.diagJson = diagJson
print("\(Self.TAG): Created History record: kind=\(kind), outcome=\(outcome)")
return entity
}
/**
* Create history record from dictionary
*
* @param context Core Data managed object context
* @param dict Dictionary with history data
* @return Created History entity or nil
*/
static func create(
in context: NSManagedObjectContext,
from dict: [String: Any]
) -> History? {
guard let id = dict["id"] as? String,
let kind = dict["kind"] as? String,
let outcome = dict["outcome"] as? String else {
print("\(Self.TAG): Missing required fields")
return nil
}
// Convert occurredAt from epoch milliseconds or Date
let occurredAt: Date
if let timeMillis = dict["occurredAt"] as? Int64 {
occurredAt = DailyNotificationDataConversions.dateFromEpochMillis(timeMillis)
} else if let timeDate = dict["occurredAt"] as? Date {
occurredAt = timeDate
} else {
occurredAt = Date()
}
let entity = History(context: context)
entity.id = id
entity.refId = dict["refId"] as? String
entity.kind = kind
entity.occurredAt = occurredAt
entity.durationMs = DailyNotificationDataConversions.int32FromInt(
dict["durationMs"] as? Int ?? 0
)
entity.outcome = outcome
// Convert diagJson from dictionary if needed
if let diagDict = dict["diagJson"] as? [String: Any] {
entity.diagJson = DailyNotificationDataConversions.jsonStringFromDictionary(diagDict)
} else if let diagString = dict["diagJson"] as? String {
entity.diagJson = diagString
}
return entity
}
/**
* Record recovery history with metrics
*
* @param context Core Data managed object context
* @param scenario Recovery scenario
* @param missedCount Number of missed notifications
* @param rescheduledCount Number of rescheduled notifications
* @param verifiedCount Number of verified notifications
* @param errors Number of errors
* @param startTime When recovery started
* @param endTime When recovery ended
* @return Created History entity
*/
static func recordRecovery(
in context: NSManagedObjectContext,
scenario: String,
missedCount: Int,
rescheduledCount: Int,
verifiedCount: Int,
errors: Int,
startTime: Date,
endTime: Date
) -> History {
let durationMs = Int32((endTime.timeIntervalSince(startTime) * 1000).rounded())
let diagJson: [String: Any] = [
"scenario": scenario,
"missedCount": missedCount,
"rescheduledCount": rescheduledCount,
"verifiedCount": verifiedCount,
"errors": errors,
"durationMs": durationMs
]
let diagJsonString = DailyNotificationDataConversions.jsonStringFromDictionary(diagJson) ?? "{}"
return create(
in: context,
id: UUID().uuidString,
kind: "recovery",
occurredAt: endTime,
durationMs: durationMs,
outcome: errors > 0 ? "partial_success" : "success",
diagJson: diagJsonString
)
}
/**
* Record recovery failure
*
* @param context Core Data managed object context
* @param error Error that occurred
* @param scenario Recovery scenario (if known)
* @return Created History entity
*/
static func recordRecoveryFailure(
in context: NSManagedObjectContext,
error: Error,
scenario: String? = nil
) -> History {
var errorInfo: [String: Any] = [
"error": error.localizedDescription,
"errorType": String(describing: type(of: error))
]
// Add scenario if provided
if let scenario = scenario {
errorInfo["scenario"] = scenario
}
// Add error details if available
if let nsError = error as NSError? {
errorInfo["errorCode"] = nsError.code
errorInfo["errorDomain"] = nsError.domain
if let userInfo = nsError.userInfo as? [String: Any] {
errorInfo["userInfo"] = userInfo
}
}
let diagJsonString = DailyNotificationDataConversions.jsonStringFromDictionary(errorInfo) ?? "{}"
return create(
in: context,
id: UUID().uuidString,
kind: "recovery",
occurredAt: Date(),
outcome: "failure",
diagJson: diagJsonString
)
}
// MARK: - Read/Query Methods
/**
* Fetch History by ID
*
* @param context Core Data managed object context
* @param id History ID
* @return History entity or nil
*/
static func fetch(
by id: String,
in context: NSManagedObjectContext
) -> History? {
let request: NSFetchRequest<History> = History.fetchRequest()
request.predicate = NSPredicate(format: "id == %@", id)
request.fetchLimit = 1
do {
let results = try context.fetch(request)
return results.first
} catch {
print("\(Self.TAG): Error fetching by id: \(error.localizedDescription)")
return nil
}
}
/**
* Query by kind
*
* @param context Core Data managed object context
* @param kind History kind
* @return Array of History entities
*/
static func query(
by kind: String,
in context: NSManagedObjectContext
) -> [History] {
let request: NSFetchRequest<History> = History.fetchRequest()
request.predicate = NSPredicate(format: "kind == %@", kind)
request.sortDescriptors = [NSSortDescriptor(key: "occurredAt", ascending: false)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by kind: \(error.localizedDescription)")
return []
}
}
/**
* Query by refId
*
* @param context Core Data managed object context
* @param refId Reference ID
* @return Array of History entities
*/
static func queryByRefId(
_ refId: String,
in context: NSManagedObjectContext
) -> [History] {
let request: NSFetchRequest<History> = History.fetchRequest()
request.predicate = NSPredicate(format: "refId == %@", refId)
request.sortDescriptors = [NSSortDescriptor(key: "occurredAt", ascending: false)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by refId: \(error.localizedDescription)")
return []
}
}
/**
* Query by outcome
*
* @param context Core Data managed object context
* @param outcome Outcome string
* @return Array of History entities
*/
static func queryByOutcome(
_ outcome: String,
in context: NSManagedObjectContext
) -> [History] {
let request: NSFetchRequest<History> = History.fetchRequest()
request.predicate = NSPredicate(format: "outcome == %@", outcome)
request.sortDescriptors = [NSSortDescriptor(key: "occurredAt", ascending: false)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by outcome: \(error.localizedDescription)")
return []
}
}
}

View File

@@ -0,0 +1,411 @@
/**
* NotificationConfigDAO.swift
*
* Data Access Object (DAO) for NotificationConfig Core Data entity
* Provides CRUD operations and query helpers for notification configuration
*
* @author Matthew Raymer
* @version 1.0.0
* @created 2025-12-08
*/
import Foundation
import CoreData
/**
* Extension providing DAO methods for NotificationConfig entity
*
* This extension adds CRUD operations and query helpers to the
* auto-generated NotificationConfig Core Data class.
*/
extension NotificationConfig {
// MARK: - Constants
private static let TAG = "DNP-NOTIFICATION-CONFIG-DAO"
// MARK: - Create/Insert Methods
/**
* Create a new NotificationConfig entity in the given context
*
* @param context Core Data managed object context
* @param id Unique configuration identifier
* @param timesafariDid TimeSafari device ID
* @param configType Type of configuration
* @param configKey Configuration key
* @param configValue Configuration value (string representation)
* @param configDataType Data type of value (e.g., "string", "int", "bool", "json")
* @param isEncrypted Whether value is encrypted
* @param encryptionKeyId Encryption key identifier
* @param ttlSeconds Time-to-live in seconds
* @param isActive Whether configuration is active
* @param metadata Additional metadata (JSON string)
* @return Created NotificationConfig entity
*/
static func create(
in context: NSManagedObjectContext,
id: String,
timesafariDid: String? = nil,
configType: String? = nil,
configKey: String? = nil,
configValue: String? = nil,
configDataType: String? = nil,
isEncrypted: Bool = false,
encryptionKeyId: String? = nil,
ttlSeconds: Int64 = 604800, // 7 days default
isActive: Bool = true,
metadata: String? = nil
) -> NotificationConfig {
let entity = NotificationConfig(context: context)
let now = Date()
entity.id = id
entity.timesafariDid = timesafariDid
entity.configType = configType
entity.configKey = configKey
entity.configValue = configValue
entity.configDataType = configDataType
entity.isEncrypted = isEncrypted
entity.encryptionKeyId = encryptionKeyId
entity.createdAt = now
entity.updatedAt = now
entity.ttlSeconds = ttlSeconds
entity.isActive = isActive
entity.metadata = metadata
print("\(Self.TAG): Created NotificationConfig with id: \(id)")
return entity
}
/**
* Create from dictionary representation
*
* @param context Core Data managed object context
* @param dict Dictionary with configuration data
* @return Created NotificationConfig entity or nil
*/
static func create(
in context: NSManagedObjectContext,
from dict: [String: Any]
) -> NotificationConfig? {
guard let id = dict["id"] as? String else {
print("\(Self.TAG): Missing required 'id' field")
return nil
}
// Convert createdAt/updatedAt if present
let createdAt: Date
if let createdMillis = dict["createdAt"] as? Int64 {
createdAt = DailyNotificationDataConversions.dateFromEpochMillis(createdMillis)
} else if let createdDate = dict["createdAt"] as? Date {
createdAt = createdDate
} else {
createdAt = Date()
}
let updatedAt: Date
if let updatedMillis = dict["updatedAt"] as? Int64 {
updatedAt = DailyNotificationDataConversions.dateFromEpochMillis(updatedMillis)
} else if let updatedDate = dict["updatedAt"] as? Date {
updatedAt = updatedDate
} else {
updatedAt = Date()
}
let entity = NotificationConfig(context: context)
entity.id = id
entity.timesafariDid = dict["timesafariDid"] as? String
entity.configType = dict["configType"] as? String
entity.configKey = dict["configKey"] as? String
entity.configValue = dict["configValue"] as? String
entity.configDataType = dict["configDataType"] as? String
entity.isEncrypted = dict["isEncrypted"] as? Bool ?? false
entity.encryptionKeyId = dict["encryptionKeyId"] as? String
entity.createdAt = createdAt
entity.updatedAt = updatedAt
entity.ttlSeconds = dict["ttlSeconds"] as? Int64 ?? 604800
entity.isActive = dict["isActive"] as? Bool ?? true
entity.metadata = dict["metadata"] as? String
return entity
}
// MARK: - Read/Query Methods
/**
* Fetch NotificationConfig by ID
*
* @param context Core Data managed object context
* @param id Configuration ID
* @return NotificationConfig entity or nil
*/
static func fetch(
by id: String,
in context: NSManagedObjectContext
) -> NotificationConfig? {
let request: NSFetchRequest<NotificationConfig> = NotificationConfig.fetchRequest()
request.predicate = NSPredicate(format: "id == %@", id)
request.fetchLimit = 1
do {
let results = try context.fetch(request)
return results.first
} catch {
print("\(Self.TAG): Error fetching by id: \(error.localizedDescription)")
return nil
}
}
/**
* Fetch NotificationConfig by key (configKey)
*
* @param context Core Data managed object context
* @param configKey Configuration key
* @return NotificationConfig entity or nil
*/
static func fetchByConfigKey(
_ configKey: String,
in context: NSManagedObjectContext
) -> NotificationConfig? {
let request: NSFetchRequest<NotificationConfig> = NotificationConfig.fetchRequest()
request.predicate = NSPredicate(format: "configKey == %@", configKey)
request.fetchLimit = 1
do {
let results = try context.fetch(request)
return results.first
} catch {
print("\(Self.TAG): Error fetching by configKey: \(error.localizedDescription)")
return nil
}
}
/**
* Fetch all NotificationConfig entities
*
* @param context Core Data managed object context
* @return Array of NotificationConfig entities
*/
static func fetchAll(
in context: NSManagedObjectContext
) -> [NotificationConfig] {
let request: NSFetchRequest<NotificationConfig> = NotificationConfig.fetchRequest()
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error fetching all: \(error.localizedDescription)")
return []
}
}
/**
* Query by timesafariDid
*
* @param context Core Data managed object context
* @param timesafariDid TimeSafari device ID
* @return Array of NotificationConfig entities
*/
static func queryByTimesafariDid(
_ timesafariDid: String,
in context: NSManagedObjectContext
) -> [NotificationConfig] {
let request: NSFetchRequest<NotificationConfig> = NotificationConfig.fetchRequest()
request.predicate = NSPredicate(format: "timesafariDid == %@", timesafariDid)
request.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: false)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by timesafariDid: \(error.localizedDescription)")
return []
}
}
/**
* Query by configType
*
* @param context Core Data managed object context
* @param configType Configuration type
* @return Array of NotificationConfig entities
*/
static func queryByConfigType(
_ configType: String,
in context: NSManagedObjectContext
) -> [NotificationConfig] {
let request: NSFetchRequest<NotificationConfig> = NotificationConfig.fetchRequest()
request.predicate = NSPredicate(format: "configType == %@", configType)
request.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: false)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by configType: \(error.localizedDescription)")
return []
}
}
/**
* Query active configurations only
*
* @param context Core Data managed object context
* @return Array of active NotificationConfig entities
*/
static func queryActive(
in context: NSManagedObjectContext
) -> [NotificationConfig] {
let request: NSFetchRequest<NotificationConfig> = NotificationConfig.fetchRequest()
request.predicate = NSPredicate(format: "isActive == YES")
request.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: false)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying active: \(error.localizedDescription)")
return []
}
}
/**
* Query by configType and isActive
*
* @param context Core Data managed object context
* @param configType Configuration type
* @param isActive Whether configuration is active
* @return Array of NotificationConfig entities
*/
static func query(
by configType: String,
isActive: Bool,
in context: NSManagedObjectContext
) -> [NotificationConfig] {
let request: NSFetchRequest<NotificationConfig> = NotificationConfig.fetchRequest()
request.predicate = NSPredicate(
format: "configType == %@ AND isActive == %@",
configType,
NSNumber(value: isActive)
)
request.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: false)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by configType and isActive: \(error.localizedDescription)")
return []
}
}
// MARK: - Update Methods
/**
* Update configuration value
*
* @param value New configuration value
*/
func updateValue(_ value: String?) {
self.configValue = value
self.updatedAt = Date()
}
/**
* Activate or deactivate configuration
*
* @param active Whether configuration should be active
*/
func setActive(_ active: Bool) {
self.isActive = active
self.updatedAt = Date()
}
/**
* Update this entity's updatedAt timestamp
*/
func touch() {
self.updatedAt = Date()
}
// MARK: - Delete Methods
/**
* Delete NotificationConfig by ID
*
* @param context Core Data managed object context
* @param id Configuration ID
* @return true if deleted, false otherwise
*/
static func delete(
by id: String,
in context: NSManagedObjectContext
) -> Bool {
guard let entity = fetch(by: id, in: context) else {
return false
}
context.delete(entity)
do {
try context.save()
print("\(Self.TAG): Deleted NotificationConfig with id: \(id)")
return true
} catch {
print("\(Self.TAG): Error deleting: \(error.localizedDescription)")
context.rollback()
return false
}
}
/**
* Delete NotificationConfig by configKey
*
* @param context Core Data managed object context
* @param configKey Configuration key
* @return true if deleted, false otherwise
*/
static func deleteByConfigKey(
_ configKey: String,
in context: NSManagedObjectContext
) -> Bool {
guard let entity = fetchByConfigKey(configKey, in: context) else {
return false
}
context.delete(entity)
do {
try context.save()
print("\(Self.TAG): Deleted NotificationConfig with configKey: \(configKey)")
return true
} catch {
print("\(Self.TAG): Error deleting: \(error.localizedDescription)")
context.rollback()
return false
}
}
/**
* Delete all NotificationConfig entities
*
* @param context Core Data managed object context
* @return Number of entities deleted
*/
static func deleteAll(
in context: NSManagedObjectContext
) -> Int {
let request: NSFetchRequest<NSFetchRequestResult> = NotificationConfig.fetchRequest()
let deleteRequest = NSBatchDeleteRequest(fetchRequest: request)
do {
let result = try context.execute(deleteRequest) as? NSBatchDeleteResult
try context.save()
let count = result?.result as? Int ?? 0
print("\(Self.TAG): Deleted \(count) NotificationConfig entities")
return count
} catch {
print("\(Self.TAG): Error deleting all: \(error.localizedDescription)")
context.rollback()
return 0
}
}
}

View File

@@ -0,0 +1,440 @@
/**
* NotificationContentDAO.swift
*
* Data Access Object (DAO) for NotificationContent Core Data entity
* Provides CRUD operations and query helpers for notification content
*
* @author Matthew Raymer
* @version 1.0.0
* @created 2025-12-08
*/
import Foundation
import CoreData
/**
* Extension providing DAO methods for NotificationContent entity
*
* This extension adds CRUD operations and query helpers to the
* auto-generated NotificationContent Core Data class.
*/
extension NotificationContentEntity {
// MARK: - Constants
private static let TAG = "DNP-NOTIFICATION-CONTENT-DAO"
// MARK: - Create/Insert Methods
/**
* Create a new NotificationContent entity in the given context
*
* @param context Core Data managed object context
* @param id Unique notification identifier
* @param pluginVersion Plugin version string
* @param timesafariDid TimeSafari device ID
* @param notificationType Type of notification
* @param title Notification title
* @param body Notification body
* @param scheduledTime Scheduled delivery time (Date)
* @param timezone Timezone string
* @param priority Notification priority (0-10)
* @param vibrationEnabled Whether vibration is enabled
* @param soundEnabled Whether sound is enabled
* @param mediaUrl URL to media content
* @param encryptedContent Encrypted content string
* @param encryptionKeyId Encryption key identifier
* @param ttlSeconds Time-to-live in seconds
* @param deliveryStatus Current delivery status
* @param deliveryAttempts Number of delivery attempts
* @param metadata Additional metadata (JSON string)
* @return Created NotificationContent entity
*/
static func create(
in context: NSManagedObjectContext,
id: String,
pluginVersion: String? = nil,
timesafariDid: String? = nil,
notificationType: String? = nil,
title: String? = nil,
body: String? = nil,
scheduledTime: Date,
timezone: String? = nil,
priority: Int32 = 0,
vibrationEnabled: Bool = false,
soundEnabled: Bool = true,
mediaUrl: String? = nil,
encryptedContent: String? = nil,
encryptionKeyId: String? = nil,
ttlSeconds: Int64 = 604800, // 7 days default
deliveryStatus: String? = nil,
deliveryAttempts: Int32 = 0,
metadata: String? = nil
) -> NotificationContentEntity {
let entity = NotificationContentEntity(context: context)
let now = Date()
entity.id = id
entity.pluginVersion = pluginVersion
entity.timesafariDid = timesafariDid
entity.notificationType = notificationType
entity.title = title
entity.body = body
entity.scheduledTime = scheduledTime
entity.timezone = timezone
entity.priority = priority
entity.vibrationEnabled = vibrationEnabled
entity.soundEnabled = soundEnabled
entity.mediaUrl = mediaUrl
entity.encryptedContent = encryptedContent
entity.encryptionKeyId = encryptionKeyId
entity.createdAt = now
entity.updatedAt = now
entity.ttlSeconds = ttlSeconds
entity.deliveryStatus = deliveryStatus
entity.deliveryAttempts = deliveryAttempts
entity.lastDeliveryAttempt = nil
entity.userInteractionCount = 0
entity.lastUserInteraction = nil
entity.metadata = metadata
print("\(Self.TAG): Created NotificationContent with id: \(id)")
return entity
}
/**
* Create from dictionary representation
*
* @param context Core Data managed object context
* @param dict Dictionary with notification data
* @return Created NotificationContent entity or nil
*/
static func create(
in context: NSManagedObjectContext,
from dict: [String: Any]
) -> NotificationContentEntity? {
guard let id = dict["id"] as? String else {
print("\(Self.TAG): Missing required 'id' field")
return nil
}
// Convert scheduledTime from epoch milliseconds or Date
let scheduledTime: Date
if let timeMillis = dict["scheduledTime"] as? Int64 {
scheduledTime = DailyNotificationDataConversions.dateFromEpochMillis(timeMillis)
} else if let timeDate = dict["scheduledTime"] as? Date {
scheduledTime = timeDate
} else {
print("\(Self.TAG): Missing or invalid 'scheduledTime' field")
return nil
}
// Convert createdAt/updatedAt if present
let createdAt: Date
if let createdMillis = dict["createdAt"] as? Int64 {
createdAt = DailyNotificationDataConversions.dateFromEpochMillis(createdMillis)
} else if let createdDate = dict["createdAt"] as? Date {
createdAt = createdDate
} else {
createdAt = Date()
}
let updatedAt: Date
if let updatedMillis = dict["updatedAt"] as? Int64 {
updatedAt = DailyNotificationDataConversions.dateFromEpochMillis(updatedMillis)
} else if let updatedDate = dict["updatedAt"] as? Date {
updatedAt = updatedDate
} else {
updatedAt = Date()
}
let entity = NotificationContentEntity(context: context)
entity.id = id
entity.pluginVersion = dict["pluginVersion"] as? String
entity.timesafariDid = dict["timesafariDid"] as? String
entity.notificationType = dict["notificationType"] as? String
entity.title = dict["title"] as? String
entity.body = dict["body"] as? String
entity.scheduledTime = scheduledTime
entity.timezone = dict["timezone"] as? String
entity.priority = DailyNotificationDataConversions.int32FromInt(
dict["priority"] as? Int ?? 0
)
entity.vibrationEnabled = dict["vibrationEnabled"] as? Bool ?? false
entity.soundEnabled = dict["soundEnabled"] as? Bool ?? true
entity.mediaUrl = dict["mediaUrl"] as? String
entity.encryptedContent = dict["encryptedContent"] as? String
entity.encryptionKeyId = dict["encryptionKeyId"] as? String
entity.createdAt = createdAt
entity.updatedAt = updatedAt
entity.ttlSeconds = dict["ttlSeconds"] as? Int64 ?? 604800
entity.deliveryStatus = dict["deliveryStatus"] as? String
entity.deliveryAttempts = DailyNotificationDataConversions.int32FromInt(
dict["deliveryAttempts"] as? Int ?? 0
)
if let lastAttemptMillis = dict["lastDeliveryAttempt"] as? Int64 {
entity.lastDeliveryAttempt = DailyNotificationDataConversions.dateFromEpochMillis(lastAttemptMillis)
}
entity.userInteractionCount = DailyNotificationDataConversions.int32FromInt(
dict["userInteractionCount"] as? Int ?? 0
)
if let lastInteractionMillis = dict["lastUserInteraction"] as? Int64 {
entity.lastUserInteraction = DailyNotificationDataConversions.dateFromEpochMillis(lastInteractionMillis)
}
entity.metadata = dict["metadata"] as? String
return entity
}
// MARK: - Read/Query Methods
/**
* Fetch NotificationContent by ID
*
* @param context Core Data managed object context
* @param id Notification ID
* @return NotificationContent entity or nil
*/
static func fetch(
by id: String,
in context: NSManagedObjectContext
) -> NotificationContentEntity? {
let request: NSFetchRequest<NotificationContentEntity> = NotificationContentEntity.fetchRequest()
request.predicate = NSPredicate(format: "id == %@", id)
request.fetchLimit = 1
do {
let results = try context.fetch(request)
return results.first
} catch {
print("\(Self.TAG): Error fetching by id: \(error.localizedDescription)")
return nil
}
}
/**
* Fetch all NotificationContent entities
*
* @param context Core Data managed object context
* @return Array of NotificationContent entities
*/
static func fetchAll(
in context: NSManagedObjectContext
) -> [NotificationContentEntity] {
let request: NSFetchRequest<NotificationContentEntity> = NotificationContentEntity.fetchRequest()
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error fetching all: \(error.localizedDescription)")
return []
}
}
/**
* Query by timesafariDid
*
* @param context Core Data managed object context
* @param timesafariDid TimeSafari device ID
* @return Array of NotificationContent entities
*/
static func query(
by timesafariDid: String,
in context: NSManagedObjectContext
) -> [NotificationContentEntity] {
let request: NSFetchRequest<NotificationContentEntity> = NotificationContentEntity.fetchRequest()
request.predicate = NSPredicate(format: "timesafariDid == %@", timesafariDid)
request.sortDescriptors = [NSSortDescriptor(key: "scheduledTime", ascending: true)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by timesafariDid: \(error.localizedDescription)")
return []
}
}
/**
* Query by notificationType
*
* @param context Core Data managed object context
* @param notificationType Notification type string
* @return Array of NotificationContent entities
*/
static func queryByNotificationType(
_ notificationType: String,
in context: NSManagedObjectContext
) -> [NotificationContentEntity] {
let request: NSFetchRequest<NotificationContentEntity> = NotificationContentEntity.fetchRequest()
request.predicate = NSPredicate(format: "notificationType == %@", notificationType)
request.sortDescriptors = [NSSortDescriptor(key: "scheduledTime", ascending: true)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by notificationType: \(error.localizedDescription)")
return []
}
}
/**
* Query by scheduledTime range
*
* @param context Core Data managed object context
* @param startDate Start date (inclusive)
* @param endDate End date (inclusive)
* @return Array of NotificationContent entities
*/
static func query(
scheduledTimeBetween startDate: Date,
and endDate: Date,
in context: NSManagedObjectContext
) -> [NotificationContentEntity] {
let request: NSFetchRequest<NotificationContentEntity> = NotificationContentEntity.fetchRequest()
request.predicate = NSPredicate(
format: "scheduledTime >= %@ AND scheduledTime <= %@",
startDate as NSDate,
endDate as NSDate
)
request.sortDescriptors = [NSSortDescriptor(key: "scheduledTime", ascending: true)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by scheduledTime range: \(error.localizedDescription)")
return []
}
}
/**
* Query by deliveryStatus
*
* @param context Core Data managed object context
* @param deliveryStatus Delivery status string
* @return Array of NotificationContent entities
*/
static func queryByDeliveryStatus(
_ deliveryStatus: String,
in context: NSManagedObjectContext
) -> [NotificationContentEntity] {
let request: NSFetchRequest<NotificationContentEntity> = NotificationContentEntity.fetchRequest()
request.predicate = NSPredicate(format: "deliveryStatus == %@", deliveryStatus)
request.sortDescriptors = [NSSortDescriptor(key: "scheduledTime", ascending: true)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by deliveryStatus: \(error.localizedDescription)")
return []
}
}
/**
* Query notifications ready for delivery (scheduledTime <= currentTime)
*
* @param context Core Data managed object context
* @param currentTime Current time for comparison
* @return Array of NotificationContent entities
*/
static func queryReadyForDelivery(
currentTime: Date,
in context: NSManagedObjectContext
) -> [NotificationContentEntity] {
let request: NSFetchRequest<NotificationContentEntity> = NotificationContentEntity.fetchRequest()
request.predicate = NSPredicate(format: "scheduledTime <= %@", currentTime as NSDate)
request.sortDescriptors = [NSSortDescriptor(key: "scheduledTime", ascending: true)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying ready for delivery: \(error.localizedDescription)")
return []
}
}
// MARK: - Update Methods
/**
* Update this entity's updatedAt timestamp
*/
func touch() {
self.updatedAt = Date()
}
/**
* Update delivery status and increment attempts
*
* @param status New delivery status
*/
func updateDeliveryStatus(_ status: String) {
self.deliveryStatus = status
self.deliveryAttempts += 1
self.lastDeliveryAttempt = Date()
self.touch()
}
/**
* Record user interaction
*/
func recordUserInteraction() {
self.userInteractionCount += 1
self.lastUserInteraction = Date()
self.touch()
}
// MARK: - Delete Methods
/**
* Delete NotificationContent by ID
*
* @param context Core Data managed object context
* @param id Notification ID
* @return true if deleted, false otherwise
*/
static func delete(
by id: String,
in context: NSManagedObjectContext
) -> Bool {
guard let entity = fetch(by: id, in: context) else {
return false
}
context.delete(entity)
do {
try context.save()
print("\(Self.TAG): Deleted NotificationContent with id: \(id)")
return true
} catch {
print("\(Self.TAG): Error deleting: \(error.localizedDescription)")
context.rollback()
return false
}
}
/**
* Delete all NotificationContent entities
*
* @param context Core Data managed object context
* @return Number of entities deleted
*/
static func deleteAll(
in context: NSManagedObjectContext
) -> Int {
let request: NSFetchRequest<NSFetchRequestResult> = NotificationContentEntity.fetchRequest()
let deleteRequest = NSBatchDeleteRequest(fetchRequest: request)
do {
let result = try context.execute(deleteRequest) as? NSBatchDeleteResult
try context.save()
let count = result?.result as? Int ?? 0
print("\(Self.TAG): Deleted \(count) NotificationContent entities")
return count
} catch {
print("\(Self.TAG): Error deleting all: \(error.localizedDescription)")
context.rollback()
return 0
}
}
}

View File

@@ -0,0 +1,414 @@
/**
* NotificationDeliveryDAO.swift
*
* Data Access Object (DAO) for NotificationDelivery Core Data entity
* Provides CRUD operations and query helpers for notification delivery tracking
*
* @author Matthew Raymer
* @version 1.0.0
* @created 2025-12-08
*/
import Foundation
import CoreData
/**
* Extension providing DAO methods for NotificationDelivery entity
*
* This extension adds CRUD operations and query helpers to the
* auto-generated NotificationDelivery Core Data class.
*/
extension NotificationDelivery {
// MARK: - Constants
private static let TAG = "DNP-NOTIFICATION-DELIVERY-DAO"
// MARK: - Create/Insert Methods
/**
* Create a new NotificationDelivery entity in the given context
*
* @param context Core Data managed object context
* @param id Unique delivery identifier
* @param notificationId Associated notification content ID
* @param notificationContent Associated NotificationContent entity
* @param timesafariDid TimeSafari device ID
* @param deliveryTimestamp When delivery occurred
* @param deliveryStatus Delivery status string
* @param deliveryMethod Delivery method string
* @param deliveryAttemptNumber Attempt number (1-based)
* @param deliveryDurationMs Duration of delivery in milliseconds
* @param userInteractionType Type of user interaction (if any)
* @param userInteractionTimestamp When user interacted
* @param userInteractionDurationMs Duration of interaction in milliseconds
* @param errorCode Error code (if delivery failed)
* @param errorMessage Error message (if delivery failed)
* @param deviceInfo Device information JSON string
* @param networkInfo Network information JSON string
* @param batteryLevel Battery level (0-100, -1 if unknown)
* @param dozeModeActive Whether device was in doze mode
* @param exactAlarmPermission Whether exact alarm permission granted
* @param notificationPermission Whether notification permission granted
* @param metadata Additional metadata (JSON string)
* @return Created NotificationDelivery entity
*/
static func create(
in context: NSManagedObjectContext,
id: String,
notificationId: String,
notificationContent: NotificationContentEntity? = nil,
timesafariDid: String? = nil,
deliveryTimestamp: Date,
deliveryStatus: String? = nil,
deliveryMethod: String? = nil,
deliveryAttemptNumber: Int32 = 1,
deliveryDurationMs: Int64 = 0,
userInteractionType: String? = nil,
userInteractionTimestamp: Date? = nil,
userInteractionDurationMs: Int64 = 0,
errorCode: String? = nil,
errorMessage: String? = nil,
deviceInfo: String? = nil,
networkInfo: String? = nil,
batteryLevel: Int32 = -1,
dozeModeActive: Bool = false,
exactAlarmPermission: Bool = false,
notificationPermission: Bool = false,
metadata: String? = nil
) -> NotificationDelivery {
let entity = NotificationDelivery(context: context)
entity.id = id
entity.notificationId = notificationId
entity.notificationContent = notificationContent
entity.timesafariDid = timesafariDid
entity.deliveryTimestamp = deliveryTimestamp
entity.deliveryStatus = deliveryStatus
entity.deliveryMethod = deliveryMethod
entity.deliveryAttemptNumber = deliveryAttemptNumber
entity.deliveryDurationMs = deliveryDurationMs
entity.userInteractionType = userInteractionType
entity.userInteractionTimestamp = userInteractionTimestamp
entity.userInteractionDurationMs = userInteractionDurationMs
entity.errorCode = errorCode
entity.errorMessage = errorMessage
entity.deviceInfo = deviceInfo
entity.networkInfo = networkInfo
entity.batteryLevel = batteryLevel
entity.dozeModeActive = dozeModeActive
entity.exactAlarmPermission = exactAlarmPermission
entity.notificationPermission = notificationPermission
entity.metadata = metadata
print("\(Self.TAG): Created NotificationDelivery with id: \(id)")
return entity
}
/**
* Create from dictionary representation
*
* @param context Core Data managed object context
* @param dict Dictionary with delivery data
* @param notificationContent Optional associated NotificationContent entity
* @return Created NotificationDelivery entity or nil
*/
static func create(
in context: NSManagedObjectContext,
from dict: [String: Any],
notificationContent: NotificationContentEntity? = nil
) -> NotificationDelivery? {
guard let id = dict["id"] as? String,
let notificationId = dict["notificationId"] as? String else {
print("\(Self.TAG): Missing required fields")
return nil
}
// Convert deliveryTimestamp from epoch milliseconds or Date
let deliveryTimestamp: Date
if let timeMillis = dict["deliveryTimestamp"] as? Int64 {
deliveryTimestamp = DailyNotificationDataConversions.dateFromEpochMillis(timeMillis)
} else if let timeDate = dict["deliveryTimestamp"] as? Date {
deliveryTimestamp = timeDate
} else {
print("\(Self.TAG): Missing or invalid 'deliveryTimestamp' field")
return nil
}
// Convert userInteractionTimestamp if present
let userInteractionTimestamp: Date?
if let interactionMillis = dict["userInteractionTimestamp"] as? Int64 {
userInteractionTimestamp = DailyNotificationDataConversions.dateFromEpochMillis(interactionMillis)
} else if let interactionDate = dict["userInteractionTimestamp"] as? Date {
userInteractionTimestamp = interactionDate
} else {
userInteractionTimestamp = nil
}
let entity = NotificationDelivery(context: context)
entity.id = id
entity.notificationId = notificationId
entity.notificationContent = notificationContent
entity.timesafariDid = dict["timesafariDid"] as? String
entity.deliveryTimestamp = deliveryTimestamp
entity.deliveryStatus = dict["deliveryStatus"] as? String
entity.deliveryMethod = dict["deliveryMethod"] as? String
entity.deliveryAttemptNumber = DailyNotificationDataConversions.int32FromInt(
dict["deliveryAttemptNumber"] as? Int ?? 1
)
entity.deliveryDurationMs = dict["deliveryDurationMs"] as? Int64 ?? 0
entity.userInteractionType = dict["userInteractionType"] as? String
entity.userInteractionTimestamp = userInteractionTimestamp
entity.userInteractionDurationMs = dict["userInteractionDurationMs"] as? Int64 ?? 0
entity.errorCode = dict["errorCode"] as? String
entity.errorMessage = dict["errorMessage"] as? String
entity.deviceInfo = dict["deviceInfo"] as? String
entity.networkInfo = dict["networkInfo"] as? String
entity.batteryLevel = DailyNotificationDataConversions.int32FromInt(
dict["batteryLevel"] as? Int ?? -1
)
entity.dozeModeActive = dict["dozeModeActive"] as? Bool ?? false
entity.exactAlarmPermission = dict["exactAlarmPermission"] as? Bool ?? false
entity.notificationPermission = dict["notificationPermission"] as? Bool ?? false
entity.metadata = dict["metadata"] as? String
return entity
}
// MARK: - Read/Query Methods
/**
* Fetch NotificationDelivery by ID
*
* @param context Core Data managed object context
* @param id Delivery ID
* @return NotificationDelivery entity or nil
*/
static func fetch(
by id: String,
in context: NSManagedObjectContext
) -> NotificationDelivery? {
let request: NSFetchRequest<NotificationDelivery> = NotificationDelivery.fetchRequest()
request.predicate = NSPredicate(format: "id == %@", id)
request.fetchLimit = 1
do {
let results = try context.fetch(request)
return results.first
} catch {
print("\(Self.TAG): Error fetching by id: \(error.localizedDescription)")
return nil
}
}
/**
* Query by notificationId
*
* @param context Core Data managed object context
* @param notificationId Notification content ID
* @return Array of NotificationDelivery entities
*/
static func queryByNotificationId(
_ notificationId: String,
in context: NSManagedObjectContext
) -> [NotificationDelivery] {
let request: NSFetchRequest<NotificationDelivery> = NotificationDelivery.fetchRequest()
request.predicate = NSPredicate(format: "notificationId == %@", notificationId)
request.sortDescriptors = [NSSortDescriptor(key: "deliveryTimestamp", ascending: false)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by notificationId: \(error.localizedDescription)")
return []
}
}
/**
* Query by deliveryTimestamp range
*
* @param context Core Data managed object context
* @param startDate Start date (inclusive)
* @param endDate End date (inclusive)
* @return Array of NotificationDelivery entities
*/
static func query(
deliveryTimestampBetween startDate: Date,
and endDate: Date,
in context: NSManagedObjectContext
) -> [NotificationDelivery] {
let request: NSFetchRequest<NotificationDelivery> = NotificationDelivery.fetchRequest()
request.predicate = NSPredicate(
format: "deliveryTimestamp >= %@ AND deliveryTimestamp <= %@",
startDate as NSDate,
endDate as NSDate
)
request.sortDescriptors = [NSSortDescriptor(key: "deliveryTimestamp", ascending: false)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by deliveryTimestamp range: \(error.localizedDescription)")
return []
}
}
/**
* Query by deliveryStatus
*
* @param context Core Data managed object context
* @param deliveryStatus Delivery status string
* @return Array of NotificationDelivery entities
*/
static func queryByDeliveryStatus(
_ deliveryStatus: String,
in context: NSManagedObjectContext
) -> [NotificationDelivery] {
let request: NSFetchRequest<NotificationDelivery> = NotificationDelivery.fetchRequest()
request.predicate = NSPredicate(format: "deliveryStatus == %@", deliveryStatus)
request.sortDescriptors = [NSSortDescriptor(key: "deliveryTimestamp", ascending: false)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by deliveryStatus: \(error.localizedDescription)")
return []
}
}
/**
* Query by timesafariDid
*
* @param context Core Data managed object context
* @param timesafariDid TimeSafari device ID
* @return Array of NotificationDelivery entities
*/
static func queryByTimesafariDid(
_ timesafariDid: String,
in context: NSManagedObjectContext
) -> [NotificationDelivery] {
let request: NSFetchRequest<NotificationDelivery> = NotificationDelivery.fetchRequest()
request.predicate = NSPredicate(format: "timesafariDid == %@", timesafariDid)
request.sortDescriptors = [NSSortDescriptor(key: "deliveryTimestamp", ascending: false)]
do {
return try context.fetch(request)
} catch {
print("\(Self.TAG): Error querying by timesafariDid: \(error.localizedDescription)")
return []
}
}
// MARK: - Update Methods
/**
* Update delivery status
*
* @param status New delivery status
*/
func updateDeliveryStatus(_ status: String) {
self.deliveryStatus = status
}
/**
* Record user interaction
*
* @param interactionType Type of interaction
* @param timestamp When interaction occurred
* @param durationMs Duration of interaction in milliseconds
*/
func recordUserInteraction(
type: String,
timestamp: Date,
durationMs: Int64
) {
self.userInteractionType = type
self.userInteractionTimestamp = timestamp
self.userInteractionDurationMs = durationMs
}
// MARK: - Delete Methods
/**
* Delete NotificationDelivery by ID
*
* @param context Core Data managed object context
* @param id Delivery ID
* @return true if deleted, false otherwise
*/
static func delete(
by id: String,
in context: NSManagedObjectContext
) -> Bool {
guard let entity = fetch(by: id, in: context) else {
return false
}
context.delete(entity)
do {
try context.save()
print("\(Self.TAG): Deleted NotificationDelivery with id: \(id)")
return true
} catch {
print("\(Self.TAG): Error deleting: \(error.localizedDescription)")
context.rollback()
return false
}
}
/**
* Delete all NotificationDelivery entities for a notification
*
* @param context Core Data managed object context
* @param notificationId Notification content ID
* @return Number of entities deleted
*/
static func deleteAll(
for notificationId: String,
in context: NSManagedObjectContext
) -> Int {
let deliveries = queryByNotificationId(notificationId, in: context)
let count = deliveries.count
for delivery in deliveries {
context.delete(delivery)
}
do {
try context.save()
print("\(Self.TAG): Deleted \(count) NotificationDelivery entities for notification: \(notificationId)")
return count
} catch {
print("\(Self.TAG): Error deleting all: \(error.localizedDescription)")
context.rollback()
return 0
}
}
/**
* Delete all NotificationDelivery entities
*
* @param context Core Data managed object context
* @return Number of entities deleted
*/
static func deleteAll(
in context: NSManagedObjectContext
) -> Int {
let request: NSFetchRequest<NSFetchRequestResult> = NotificationDelivery.fetchRequest()
let deleteRequest = NSBatchDeleteRequest(fetchRequest: request)
do {
let result = try context.execute(deleteRequest) as? NSBatchDeleteResult
try context.save()
let count = result?.result as? Int ?? 0
print("\(Self.TAG): Deleted \(count) NotificationDelivery entities")
return count
} catch {
print("\(Self.TAG): Error deleting all: \(error.localizedDescription)")
context.rollback()
return 0
}
}
}

View File

@@ -2,6 +2,9 @@
This directory contains the iOS-specific implementation of the DailyNotification plugin.
**Last Updated**: 2025-12-08
**Version**: 1.1.0
## Current Implementation Status
**✅ IMPLEMENTED:**
@@ -10,12 +13,38 @@ This directory contains the iOS-specific implementation of the DailyNotification
- Power management (`DailyNotificationPowerManager.swift`)
- Battery optimization handling
- iOS notification categories and actions
- **App Launch Recovery** (`DailyNotificationReactivationManager.swift`)
- Cold start recovery
- Termination recovery
- Boot recovery
- Scenario detection
- Missed notification detection
- Future notification verification
- **Core Data Integration**
- NotificationContent, NotificationDelivery, NotificationConfig entities
- DAO classes for all entities (CRUD operations)
- Data type conversions (Date ↔ Int64, etc.)
- PersistenceController with entity verification
- **Logging & Observability**
- Comprehensive recovery logging
- Metrics recording in Core Data History
- Execution time tracking
- **Error Handling**
- iOS-specific error codes
- Graceful error handling (non-fatal)
- Partial results on failures
- **API Methods**
- iOS-specific notification permission methods
- Background task status methods
- Pending notifications query
**⚠️ PARTIALLY IMPLEMENTED:**
- `BGTaskScheduler` for background data fetching (basic registration)
- Background task management (needs enhancement)
**❌ NOT IMPLEMENTED (Planned):**
- `BGTaskScheduler` for background data fetching
- Background task management
- Silent push nudge support
- Tlead prefetch logic
- Tlead prefetch logic (enhancement)
## Implementation Details
@@ -25,10 +54,19 @@ The iOS implementation currently uses:
- `UserDefaults` for local data storage ✅
- iOS notification categories and actions ✅
- Power management and battery optimization ✅
- **Core Data** for structured data persistence ✅
- **BGTaskScheduler** for background task registration ✅
- **App Launch Recovery** for notification reconciliation ✅
**Architecture:**
- **ReactivationManager**: Handles app launch recovery scenarios
- **DAO Layer**: Core Data access objects for all entities
- **Data Conversions**: Type conversion utilities (Date, Int, String, JSON)
- **History Recording**: Core Data persistence for recovery metrics
- **Error Handling**: Comprehensive error codes and graceful degradation
**Planned additions:**
- `BGTaskScheduler` for background data fetching
- Background task management
- Enhanced background task management
- Silent push support
## Native Code Location
@@ -42,23 +80,41 @@ The native iOS implementation is located in the `ios/` directory at the project
3. `DailyNotificationConfig.swift`: Configuration options ✅
4. `DailyNotificationMaintenanceWorker.swift`: Maintenance tasks ✅
5. `DailyNotificationLogger.swift`: Logging system ✅
6. **`DailyNotificationReactivationManager.swift`**: App launch recovery ✅
7. **`HistoryDAO.swift`**: Recovery history persistence ✅
8. **`NotificationContentDAO.swift`**: Notification content CRUD ✅
9. **`NotificationDeliveryDAO.swift`**: Delivery tracking CRUD ✅
10. **`NotificationConfigDAO.swift`**: Configuration CRUD ✅
11. **`DailyNotificationDataConversions.swift`**: Type conversion utilities ✅
12. **`DailyNotificationErrorCodes.swift`**: iOS-specific error codes ✅
13. **`DailyNotificationModel.swift`**: Core Data model & PersistenceController ✅
**Missing Components (Planned):**
- `BackgroundTaskManager.swift`: Handles background fetch scheduling
- `NotificationManager.swift`: Manages notification creation and display
- `DataStore.swift`: Handles local data persistence
**Background Task Components:**
- `DailyNotificationBackgroundTasks.swift`: Background task handlers ⚠️ (basic)
- `DailyNotificationBackgroundTaskManager.swift`: Task management ⚠️ (basic)
## Implementation Notes
- Uses UserDefaults for lightweight data storage ✅
- Uses Core Data for structured data persistence ✅
- Implements proper battery optimization handling ✅
- Supports iOS notification categories and actions ✅
- Handles background refresh limitations ✅
- **App launch recovery with scenario detection** ✅
- **Comprehensive error handling (non-fatal)** ✅
- **Metrics recording and observability** ✅
- **BGTaskScheduler registration** ✅
**Recovery Scenarios Supported:**
- ✅ Cold Start: App terminated, notifications may need verification
- ✅ Termination: App terminated, all notifications cleared
- ✅ Boot: Device rebooted, full recovery needed
- ✅ Warm Start: No recovery needed (optimization)
**Planned Features:**
- BGTaskScheduler for reliable background execution
- Enhanced background task budget management
- Silent push notification support
- Background task budget management
- Advanced prefetch logic
## Testing

View File

@@ -1,10 +1,10 @@
PODS:
- Capacitor (5.0.0):
- Capacitor (6.2.1):
- CapacitorCordova
- CapacitorCordova (5.0.0)
- CapacitorCordova (6.2.1)
- DailyNotificationPlugin (1.0.0):
- Capacitor (~> 5.0.0)
- CapacitorCordova (~> 5.0.0)
- Capacitor (>= 5.0.0)
- CapacitorCordova (>= 5.0.0)
DEPENDENCIES:
- "Capacitor (from `../node_modules/@capacitor/ios`)"
@@ -20,9 +20,9 @@ EXTERNAL SOURCES:
:path: "."
SPEC CHECKSUMS:
Capacitor: ba8cd5cce13c6ab3c4faf7ef98487be481c9c1c8
CapacitorCordova: 4ea17670ee562680988a7ce9db68dee5160fe564
DailyNotificationPlugin: 745a0606d51baec6fc9a025f1de1ade125ed193a
Capacitor: c95400d761e376be9da6be5a05f226c0e865cebf
CapacitorCordova: 8d93e14982f440181be7304aa9559ca631d77fff
DailyNotificationPlugin: bb72fde9eab3704a4e70af3c868a789da0650ddc
PODFILE CHECKSUM: ac8c229d24347f6f83e67e6b95458e0b81e68f7c

View File

@@ -0,0 +1,327 @@
//
// DailyNotificationDataConversionsTests.swift
// DailyNotificationPluginTests
//
// Created by Matthew Raymer on 2025-12-08
// Copyright © 2025 TimeSafari. All rights reserved.
//
import XCTest
@testable import DailyNotificationPlugin
/**
* Unit tests for DailyNotificationDataConversions
*
* Tests all data type conversion helpers:
* - Time conversions (Date Int64)
* - Numeric conversions (Int Int32, Int64 Int32)
* - String conversions (optional handling, JSON)
*/
class DailyNotificationDataConversionsTests: XCTestCase {
// MARK: - Time Conversion Tests
func testDateFromEpochMillis() {
// Given: Epoch milliseconds
let epochMillis: Int64 = 1609459200000 // 2021-01-01 00:00:00 UTC
// When: Convert to Date
let date = DailyNotificationDataConversions.dateFromEpochMillis(epochMillis)
// Then: Should match expected date
let expectedDate = Date(timeIntervalSince1970: 1609459200.0)
XCTAssertEqual(date.timeIntervalSince1970, expectedDate.timeIntervalSince1970,
accuracy: 0.001, "Date conversion should be accurate")
}
func testEpochMillisFromDate() {
// Given: Date
let date = Date(timeIntervalSince1970: 1609459200.0) // 2021-01-01 00:00:00 UTC
// When: Convert to epoch milliseconds
let epochMillis = DailyNotificationDataConversions.epochMillisFromDate(date)
// Then: Should match expected milliseconds
XCTAssertEqual(epochMillis, 1609459200000, "Epoch milliseconds should match")
}
func testDateFromEpochMillis_RoundTrip() {
// Given: Original epoch milliseconds
let originalMillis: Int64 = 1609459200000
// When: Convert to Date and back
let date = DailyNotificationDataConversions.dateFromEpochMillis(originalMillis)
let convertedMillis = DailyNotificationDataConversions.epochMillisFromDate(date)
// Then: Should match original
XCTAssertEqual(convertedMillis, originalMillis, "Round trip conversion should preserve value")
}
func testDateFromEpochMillis_Optional_Nil() {
// Given: Nil optional
let optionalMillis: Int64? = nil
// When: Convert to optional Date
let date = DailyNotificationDataConversions.dateFromEpochMillis(optionalMillis)
// Then: Should be nil
XCTAssertNil(date, "Nil input should produce nil output")
}
func testDateFromEpochMillis_Optional_Value() {
// Given: Optional with value
let optionalMillis: Int64? = 1609459200000
// When: Convert to optional Date
let date = DailyNotificationDataConversions.dateFromEpochMillis(optionalMillis)
// Then: Should have value
XCTAssertNotNil(date, "Non-nil input should produce non-nil output")
XCTAssertEqual(date!.timeIntervalSince1970, 1609459200.0, accuracy: 0.001)
}
func testEpochMillisFromDate_Optional_Nil() {
// Given: Nil optional Date
let optionalDate: Date? = nil
// When: Convert to optional milliseconds
let millis = DailyNotificationDataConversions.epochMillisFromDate(optionalDate)
// Then: Should be nil
XCTAssertNil(millis, "Nil input should produce nil output")
}
func testEpochMillisFromDate_Optional_Value() {
// Given: Optional Date with value
let optionalDate: Date? = Date(timeIntervalSince1970: 1609459200.0)
// When: Convert to optional milliseconds
let millis = DailyNotificationDataConversions.epochMillisFromDate(optionalDate)
// Then: Should have value
XCTAssertNotNil(millis, "Non-nil input should produce non-nil output")
XCTAssertEqual(millis, 1609459200000)
}
// MARK: - Numeric Conversion Tests
func testInt32FromInt() {
// Given: Int value
let intValue = 42
// When: Convert to Int32
let int32Value = DailyNotificationDataConversions.int32FromInt(intValue)
// Then: Should match
XCTAssertEqual(int32Value, 42, "Int to Int32 conversion should preserve value")
}
func testIntFromInt32() {
// Given: Int32 value
let int32Value: Int32 = 42
// When: Convert to Int
let intValue = DailyNotificationDataConversions.intFromInt32(int32Value)
// Then: Should match
XCTAssertEqual(intValue, 42, "Int32 to Int conversion should preserve value")
}
func testInt32FromInt64_WithinRange() {
// Given: Int64 value within Int32 range
let int64Value: Int64 = 2147483647 // Int32.max
// When: Convert to Int32
let int32Value = DailyNotificationDataConversions.int32FromInt64(int64Value)
// Then: Should match
XCTAssertEqual(int32Value, Int32.max, "Int64 to Int32 conversion should work within range")
}
func testInt32FromInt64_AboveMax() {
// Given: Int64 value above Int32.max
let int64Value: Int64 = 2147483648 // Int32.max + 1
// When: Convert to Int32
let int32Value = DailyNotificationDataConversions.int32FromInt64(int64Value)
// Then: Should be clamped to Int32.max
XCTAssertEqual(int32Value, Int32.max, "Int64 above max should be clamped")
}
func testInt32FromInt64_BelowMin() {
// Given: Int64 value below Int32.min
let int64Value: Int64 = -2147483649 // Int32.min - 1
// When: Convert to Int32
let int32Value = DailyNotificationDataConversions.int32FromInt64(int64Value)
// Then: Should be clamped to Int32.min
XCTAssertEqual(int32Value, Int32.min, "Int64 below min should be clamped")
}
func testInt64FromInt32() {
// Given: Int32 value
let int32Value: Int32 = 42
// When: Convert to Int64
let int64Value = DailyNotificationDataConversions.int64FromInt32(int32Value)
// Then: Should match
XCTAssertEqual(int64Value, 42, "Int32 to Int64 conversion should preserve value")
}
func testInt64FromLong() {
// Given: Int64 value (Long)
let longValue: Int64 = 42
// When: Convert to Int64 (no-op)
let int64Value = DailyNotificationDataConversions.int64FromLong(longValue)
// Then: Should match
XCTAssertEqual(int64Value, 42, "Long to Int64 conversion should preserve value")
}
func testBoolFromBoolean() {
// Given: Bool value
let boolValue = true
// When: Convert to Bool (no-op)
let convertedBool = DailyNotificationDataConversions.boolFromBoolean(boolValue)
// Then: Should match
XCTAssertEqual(convertedBool, true, "Boolean to Bool conversion should preserve value")
}
// MARK: - String Conversion Tests
func testStringFromOptional_Nil() {
// Given: Nil optional String
let optionalString: String? = nil
// When: Convert to String
let string = DailyNotificationDataConversions.stringFromOptional(optionalString)
// Then: Should be empty string
XCTAssertEqual(string, "", "Nil optional should produce empty string")
}
func testStringFromOptional_Value() {
// Given: Optional String with value
let optionalString: String? = "test"
// When: Convert to String
let string = DailyNotificationDataConversions.stringFromOptional(optionalString)
// Then: Should match
XCTAssertEqual(string, "test", "Non-nil optional should produce value")
}
func testOptionalStringFromString_Empty() {
// Given: Empty String
let string = ""
// When: Convert to optional String
let optionalString = DailyNotificationDataConversions.optionalStringFromString(string)
// Then: Should be nil
XCTAssertNil(optionalString, "Empty string should produce nil")
}
func testOptionalStringFromString_Value() {
// Given: Non-empty String
let string = "test"
// When: Convert to optional String
let optionalString = DailyNotificationDataConversions.optionalStringFromString(string)
// Then: Should have value
XCTAssertEqual(optionalString, "test", "Non-empty string should produce value")
}
// MARK: - JSON Conversion Tests
func testJsonStringFromDictionary_Valid() {
// Given: Valid dictionary
let dict: [String: Any] = ["key1": "value1", "key2": 42]
// When: Convert to JSON string
let jsonString = DailyNotificationDataConversions.jsonStringFromDictionary(dict)
// Then: Should be valid JSON
XCTAssertNotNil(jsonString, "Valid dictionary should produce JSON string")
// Verify can be parsed back
let parsedDict = DailyNotificationDataConversions.dictionaryFromJsonString(jsonString)
XCTAssertNotNil(parsedDict, "JSON string should be parseable")
XCTAssertEqual(parsedDict?["key1"] as? String, "value1")
XCTAssertEqual(parsedDict?["key2"] as? Int, 42)
}
func testJsonStringFromDictionary_Nil() {
// Given: Nil dictionary
let dict: [String: Any]? = nil
// When: Convert to JSON string
let jsonString = DailyNotificationDataConversions.jsonStringFromDictionary(dict)
// Then: Should be nil
XCTAssertNil(jsonString, "Nil dictionary should produce nil")
}
func testDictionaryFromJsonString_Valid() {
// Given: Valid JSON string
let jsonString = "{\"key1\":\"value1\",\"key2\":42}"
// When: Convert to dictionary
let dict = DailyNotificationDataConversions.dictionaryFromJsonString(jsonString)
// Then: Should be valid dictionary
XCTAssertNotNil(dict, "Valid JSON string should produce dictionary")
XCTAssertEqual(dict?["key1"] as? String, "value1")
XCTAssertEqual(dict?["key2"] as? Int, 42)
}
func testDictionaryFromJsonString_Invalid() {
// Given: Invalid JSON string
let jsonString = "{invalid json}"
// When: Convert to dictionary
let dict = DailyNotificationDataConversions.dictionaryFromJsonString(jsonString)
// Then: Should be nil
XCTAssertNil(dict, "Invalid JSON string should produce nil")
}
func testDictionaryFromJsonString_Nil() {
// Given: Nil JSON string
let jsonString: String? = nil
// When: Convert to dictionary
let dict = DailyNotificationDataConversions.dictionaryFromJsonString(jsonString)
// Then: Should be nil
XCTAssertNil(dict, "Nil JSON string should produce nil")
}
func testJsonStringFromDictionary_RoundTrip() {
// Given: Original dictionary
let originalDict: [String: Any] = [
"string": "value",
"number": 42,
"bool": true,
"nested": ["key": "value"]
]
// When: Convert to JSON and back
let jsonString = DailyNotificationDataConversions.jsonStringFromDictionary(originalDict)
let parsedDict = DailyNotificationDataConversions.dictionaryFromJsonString(jsonString)
// Then: Should match original (with type conversions)
XCTAssertNotNil(parsedDict, "Round trip should produce dictionary")
XCTAssertEqual(parsedDict?["string"] as? String, "value")
XCTAssertEqual(parsedDict?["number"] as? Int, 42)
XCTAssertEqual(parsedDict?["bool"] as? Bool, true)
}
}

View File

@@ -0,0 +1,346 @@
//
// DailyNotificationReactivationManagerTests.swift
// DailyNotificationPluginTests
//
// Created by Matthew Raymer on 2025-12-08
// Copyright © 2025 TimeSafari. All rights reserved.
//
import XCTest
import UserNotifications
@testable import DailyNotificationPlugin
/**
* Unit tests for DailyNotificationReactivationManager
*
* Tests all recovery scenarios: cold start, termination, boot, warm start
*/
class DailyNotificationReactivationManagerTests: XCTestCase {
var reactivationManager: DailyNotificationReactivationManager!
var database: DailyNotificationDatabase!
var storage: DailyNotificationStorage!
var scheduler: DailyNotificationScheduler!
var notificationCenter: UNUserNotificationCenter!
override func setUp() {
super.setUp()
// Use real notification center for testing
notificationCenter = UNUserNotificationCenter.current()
// Create real instances with test database paths
let testDbPath = NSTemporaryDirectory().appending("test_reactivation_db_\(UUID().uuidString).sqlite")
database = DailyNotificationDatabase(path: testDbPath)
storage = DailyNotificationStorage(databasePath: testDbPath)
scheduler = DailyNotificationScheduler()
// Create reactivation manager
reactivationManager = DailyNotificationReactivationManager(
database: database,
storage: storage,
scheduler: scheduler
)
// Clear UserDefaults for clean test state
UserDefaults.standard.removeObject(forKey: "DNP_LAST_LAUNCH_TIME")
}
override func tearDown() {
reactivationManager = nil
database = nil
storage = nil
scheduler = nil
notificationCenter = nil
// Clean up UserDefaults
UserDefaults.standard.removeObject(forKey: "DNP_LAST_LAUNCH_TIME")
// Clean up test database files
let fileManager = FileManager.default
let tempDir = NSTemporaryDirectory()
if let files = try? fileManager.contentsOfDirectory(atPath: tempDir) {
for file in files where file.hasPrefix("test_reactivation_db") {
try? fileManager.removeItem(atPath: tempDir.appending(file))
}
}
super.tearDown()
}
// MARK: - Scenario Detection Tests
func testDetectScenario_None_EmptyStorage() async throws {
// Given: Empty storage (no notifications added)
// Storage is already empty from setUp
// When: Detect scenario
let scenario = try await reactivationManager.detectScenario()
// Then: Should return .none
XCTAssertEqual(scenario, .none, "Empty storage should return .none scenario")
}
func testDetectScenario_ColdStart_Mismatch() async throws {
// Given: Storage has notifications but notification center doesn't
let notification1 = createTestNotification(id: "test-1", scheduledTime: futureTime())
storage.saveNotificationContent(notification1)
// Clear notification center
notificationCenter.removeAllPendingNotificationRequests()
// When: Detect scenario
let scenario = try await reactivationManager.detectScenario()
// Then: Should return .coldStart (or .termination if no pending)
XCTAssertTrue(scenario == .coldStart || scenario == .termination,
"Mismatch should return .coldStart or .termination")
}
func testDetectScenario_WarmStart_Match() async throws {
// Given: Storage and notification center have matching notifications
let notification1 = createTestNotification(id: "test-1", scheduledTime: futureTime())
storage.saveNotificationContent(notification1)
// Schedule notification in notification center
let content = UNMutableNotificationContent()
content.title = notification1.title ?? "Test"
content.body = notification1.body ?? "Test"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
let request = UNNotificationRequest(identifier: notification1.id, content: content, trigger: trigger)
try await notificationCenter.add(request)
// When: Detect scenario
let scenario = try await reactivationManager.detectScenario()
// Then: Should return .warmStart
XCTAssertEqual(scenario, .warmStart, "Matching notifications should return .warmStart")
// Cleanup
notificationCenter.removeAllPendingNotificationRequests()
}
func testDetectScenario_Termination_NoPending() async throws {
// Given: Storage has notifications but notification center is empty
let notification1 = createTestNotification(id: "test-1", scheduledTime: futureTime())
storage.saveNotificationContent(notification1)
// Clear notification center
notificationCenter.removeAllPendingNotificationRequests()
// When: Detect scenario
let scenario = try await reactivationManager.detectScenario()
// Then: Should return .termination
XCTAssertEqual(scenario, .termination, "No pending notifications with storage should return .termination")
}
// MARK: - Boot Detection Tests
func testDetectBootScenario_FirstLaunch_ReturnsFalse() {
// Given: No last launch time (first launch)
UserDefaults.standard.removeObject(forKey: "DNP_LAST_LAUNCH_TIME")
// When: Detect boot scenario
let isBoot = reactivationManager.detectBootScenario()
// Then: Should return false
XCTAssertFalse(isBoot, "First launch should not be detected as boot")
}
func testDetectBootScenario_RecentLaunch_ReturnsFalse() {
// Given: Recent launch time (not a boot)
let recentTime = Date().timeIntervalSince1970 - 300 // 5 minutes ago
UserDefaults.standard.set(recentTime, forKey: "DNP_LAST_LAUNCH_TIME")
// When: Detect boot scenario
let isBoot = reactivationManager.detectBootScenario()
// Then: Should return false
XCTAssertFalse(isBoot, "Recent launch should not be detected as boot")
}
func testDetectBootScenario_BootDetected_ReturnsTrue() {
// Given: Last launch time is far in past (simulating boot)
let oldTime = Date().timeIntervalSince1970 - 3600 // 1 hour ago
UserDefaults.standard.set(oldTime, forKey: "DNP_LAST_LAUNCH_TIME")
// Mock system uptime to be less than time since last launch
// Note: This is a simplified test - in real scenario, ProcessInfo.systemUptime would be small after boot
// When: Detect boot scenario
// Since we can't easily mock ProcessInfo.systemUptime, we'll test the logic
// by checking if the method handles the case correctly
let isBoot = reactivationManager.detectBootScenario()
// Then: May return true if system uptime is actually small (real device/simulator state)
// This test verifies the method doesn't crash
XCTAssertNotNil(isBoot, "Boot detection should not crash")
}
// MARK: - Missed Notification Detection Tests
func testDetectMissedNotifications_PastScheduledTime() async throws {
// Given: Notification with past scheduled time
let pastTime = Int64(Date().timeIntervalSince1970 * 1000) - 3600000 // 1 hour ago
let notification = createTestNotification(id: "missed-1", scheduledTime: pastTime)
storage.saveNotificationContent(notification)
// When: Detect missed notifications
let missed = try await reactivationManager.detectMissedNotifications(currentTime: Date())
// Then: Should detect the missed notification
XCTAssertEqual(missed.count, 1, "Should detect 1 missed notification")
XCTAssertEqual(missed.first?.id, "missed-1", "Should detect correct notification")
}
func testDetectMissedNotifications_FutureScheduledTime() async throws {
// Given: Notification with future scheduled time
let futureTime = Int64(Date().timeIntervalSince1970 * 1000) + 3600000 // 1 hour from now
let notification = createTestNotification(id: "future-1", scheduledTime: futureTime)
storage.saveNotificationContent(notification)
// When: Detect missed notifications
let missed = try await reactivationManager.detectMissedNotifications(currentTime: Date())
// Then: Should not detect as missed
XCTAssertEqual(missed.count, 0, "Should not detect future notifications as missed")
}
func testDetectMissedNotifications_MixedTimes() async throws {
// Given: Mix of past and future notifications
let pastTime = Int64(Date().timeIntervalSince1970 * 1000) - 3600000
let futureTime = Int64(Date().timeIntervalSince1970 * 1000) + 3600000
let pastNotification = createTestNotification(id: "past-1", scheduledTime: pastTime)
let futureNotification = createTestNotification(id: "future-1", scheduledTime: futureTime)
storage.saveNotificationContent(pastNotification)
storage.saveNotificationContent(futureNotification)
// When: Detect missed notifications
let missed = try await reactivationManager.detectMissedNotifications(currentTime: Date())
// Then: Should only detect past notification
XCTAssertEqual(missed.count, 1, "Should detect only past notification")
XCTAssertEqual(missed.first?.id, "past-1", "Should detect correct notification")
}
// MARK: - Future Notification Verification Tests
func testVerifyFutureNotifications_AllScheduled() async throws {
// Given: Future notifications in storage and notification center
let futureTime = Int64(Date().timeIntervalSince1970 * 1000) + 3600000
let notification = createTestNotification(id: "future-1", scheduledTime: futureTime)
storage.saveNotificationContent(notification)
// Schedule in notification center
let content = UNMutableNotificationContent()
content.title = notification.title ?? "Test"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3600, repeats: false)
let request = UNNotificationRequest(identifier: notification.id, content: content, trigger: trigger)
try await notificationCenter.add(request)
// When: Verify future notifications
let result = try await reactivationManager.verifyFutureNotifications()
// Then: Should verify all are scheduled
XCTAssertEqual(result.totalSchedules, 1, "Should have 1 future schedule")
XCTAssertEqual(result.notificationsFound, 1, "Should find 1 scheduled notification")
XCTAssertEqual(result.notificationsMissing, 0, "Should have 0 missing notifications")
// Cleanup
notificationCenter.removeAllPendingNotificationRequests()
}
func testVerifyFutureNotifications_SomeMissing() async throws {
// Given: Future notifications in storage but not all in notification center
let futureTime = Int64(Date().timeIntervalSince1970 * 1000) + 3600000
let notification1 = createTestNotification(id: "future-1", scheduledTime: futureTime)
let notification2 = createTestNotification(id: "future-2", scheduledTime: futureTime + 3600000)
storage.saveNotificationContent(notification1)
storage.saveNotificationContent(notification2)
// Only schedule one in notification center
let content = UNMutableNotificationContent()
content.title = notification1.title ?? "Test"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3600, repeats: false)
let request = UNNotificationRequest(identifier: notification1.id, content: content, trigger: trigger)
try await notificationCenter.add(request)
// When: Verify future notifications
let result = try await reactivationManager.verifyFutureNotifications()
// Then: Should detect missing notification
XCTAssertEqual(result.totalSchedules, 2, "Should have 2 future schedules")
XCTAssertEqual(result.notificationsFound, 1, "Should find 1 scheduled notification")
XCTAssertEqual(result.notificationsMissing, 1, "Should have 1 missing notification")
XCTAssertTrue(result.missingIds.contains("future-2"), "Should identify missing notification")
// Cleanup
notificationCenter.removeAllPendingNotificationRequests()
}
// MARK: - Recovery Result Tests
func testRecoveryResult_Initialization() {
// Given: Recovery result data
let result = RecoveryResult(
missedCount: 2,
rescheduledCount: 3,
verifiedCount: 5,
errors: 1
)
// Then: Should have correct values
XCTAssertEqual(result.missedCount, 2)
XCTAssertEqual(result.rescheduledCount, 3)
XCTAssertEqual(result.verifiedCount, 5)
XCTAssertEqual(result.errors, 1)
}
func testVerificationResult_Initialization() {
// Given: Verification result data
let result = VerificationResult(
totalSchedules: 10,
notificationsFound: 8,
notificationsMissing: 2,
missingIds: ["id-1", "id-2"]
)
// Then: Should have correct values
XCTAssertEqual(result.totalSchedules, 10)
XCTAssertEqual(result.notificationsFound, 8)
XCTAssertEqual(result.notificationsMissing, 2)
XCTAssertEqual(result.missingIds.count, 2)
}
// MARK: - Helper Methods
private func createTestNotification(id: String, scheduledTime: Int64) -> NotificationContent {
return NotificationContent(
id: id,
title: "Test Notification",
body: "Test body",
scheduledTime: scheduledTime,
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: nil,
payload: nil,
etag: nil
)
}
private func futureTime() -> Int64 {
return Int64(Date().timeIntervalSince1970 * 1000) + 3600000 // 1 hour from now
}
}
// MARK: - Mock Classes
// Note: We use real instances of DailyNotificationDatabase, DailyNotificationStorage, and DailyNotificationScheduler
// with test database paths for testing. This provides more realistic testing while still being isolated.
// Note: Methods are now internal in ReactivationManager, so they can be tested directly

View File

@@ -0,0 +1,468 @@
//
// DailyNotificationRecoveryIntegrationTests.swift
// DailyNotificationPluginTests
//
// Created by Matthew Raymer on 2025-12-08
// Copyright © 2025 TimeSafari. All rights reserved.
//
import XCTest
import UserNotifications
import CoreData
@testable import DailyNotificationPlugin
/**
* Integration tests for recovery flow
*
* Tests full recovery scenarios and error handling:
* - Full recovery flow (simulated app termination and launch)
* - Error handling (database errors, notification center errors)
* - App stability (verify app doesn't crash)
*/
class DailyNotificationRecoveryIntegrationTests: XCTestCase {
var reactivationManager: DailyNotificationReactivationManager!
var database: DailyNotificationDatabase!
var storage: DailyNotificationStorage!
var scheduler: DailyNotificationScheduler!
var notificationCenter: UNUserNotificationCenter!
var persistenceController: PersistenceController!
override func setUp() {
super.setUp()
// Use real notification center for testing
notificationCenter = UNUserNotificationCenter.current()
// Create real instances with test database paths
let testDbPath = NSTemporaryDirectory().appending("test_integration_db_\(UUID().uuidString).sqlite")
database = DailyNotificationDatabase(path: testDbPath)
storage = DailyNotificationStorage(databasePath: testDbPath)
scheduler = DailyNotificationScheduler()
// Create in-memory Core Data for history
persistenceController = PersistenceController(inMemory: true)
// Create reactivation manager
reactivationManager = DailyNotificationReactivationManager(
database: database,
storage: storage,
scheduler: scheduler
)
// Clear UserDefaults for clean test state
UserDefaults.standard.removeObject(forKey: "DNP_LAST_LAUNCH_TIME")
// Clear all pending notifications
let expectation = XCTestExpectation(description: "Clear notifications")
notificationCenter.removeAllPendingNotificationRequests { error in
if let error = error {
print("Warning: Failed to clear notifications: \(error.localizedDescription)")
}
expectation.fulfill()
}
wait(for: [expectation], timeout: 2.0)
}
override func tearDown() {
reactivationManager = nil
database = nil
storage = nil
scheduler = nil
notificationCenter = nil
persistenceController = nil
// Clean up UserDefaults
UserDefaults.standard.removeObject(forKey: "DNP_LAST_LAUNCH_TIME")
// Clean up test database files
let fileManager = FileManager.default
let tempDir = NSTemporaryDirectory()
if let files = try? fileManager.contentsOfDirectory(atPath: tempDir) {
for file in files where file.hasPrefix("test_integration_db") {
try? fileManager.removeItem(atPath: tempDir.appending(file))
}
}
super.tearDown()
}
// MARK: - Full Recovery Flow Tests
/**
* Test full recovery flow: schedule notification, simulate termination, launch, verify recovery
*/
func testFullRecoveryFlow_ColdStart() async throws {
// Given: Schedule a notification
let notificationId = UUID().uuidString
let futureTime = Date().addingTimeInterval(3600) // 1 hour from now
let notification = NotificationContent(
id: notificationId,
title: "Test Notification",
body: "Test Body",
scheduledTime: Int64(futureTime.timeIntervalSince1970 * 1000),
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: nil,
payload: nil,
etag: nil
)
// Save to storage
storage.saveNotificationContent(notification)
// Schedule with notification center
let success = await scheduler.scheduleNotification(notification)
XCTAssertTrue(success, "Notification should be scheduled")
// Verify notification is scheduled
let pendingBefore = try await notificationCenter.pendingNotificationRequests()
XCTAssertTrue(pendingBefore.contains { $0.identifier == notificationId },
"Notification should be in pending list")
// When: Simulate app termination (clear notifications but keep storage)
notificationCenter.removeAllPendingNotificationRequests { _ in }
// Wait a bit for removal to complete
try await Task.sleep(nanoseconds: 100_000_000) // 0.1 seconds
// Verify notifications are cleared
let pendingAfterClear = try await notificationCenter.pendingNotificationRequests()
XCTAssertTrue(pendingAfterClear.isEmpty, "Notifications should be cleared")
// Simulate app launch: perform recovery
let expectation = XCTestExpectation(description: "Recovery completed")
reactivationManager.performRecovery()
// Wait for recovery to complete (with timeout)
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
expectation.fulfill()
}
wait(for: [expectation], timeout: 5.0)
// Then: Verify recovery executed and notifications rescheduled
let pendingAfterRecovery = try await notificationCenter.pendingNotificationRequests()
XCTAssertGreaterThanOrEqual(pendingAfterRecovery.count, 0,
"Recovery should attempt to reschedule")
// Verify notification is back in pending list (if recovery succeeded)
let found = pendingAfterRecovery.contains { $0.identifier == notificationId }
// Note: Recovery may or may not succeed depending on permissions, but app shouldn't crash
XCTAssertNoThrow(found, "App should not crash during recovery")
}
/**
* Test full recovery flow: termination scenario
*/
func testFullRecoveryFlow_Termination() async throws {
// Given: Multiple notifications scheduled
let notificationIds = (1...3).map { _ in UUID().uuidString }
let futureTime = Date().addingTimeInterval(3600)
for notificationId in notificationIds {
let notification = NotificationContent(
id: notificationId,
title: "Test \(notificationId)",
body: "Body",
scheduledTime: Int64(futureTime.timeIntervalSince1970 * 1000),
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: nil,
payload: nil,
etag: nil
)
storage.saveNotificationContent(notification)
_ = await scheduler.scheduleNotification(notification)
}
// Verify all are scheduled
let pendingBefore = try await notificationCenter.pendingNotificationRequests()
XCTAssertEqual(pendingBefore.count, 3, "All notifications should be scheduled")
// When: Simulate termination (clear all notifications)
notificationCenter.removeAllPendingNotificationRequests { _ in }
try await Task.sleep(nanoseconds: 100_000_000)
// Perform recovery
let expectation = XCTestExpectation(description: "Recovery completed")
reactivationManager.performRecovery()
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
expectation.fulfill()
}
await fulfillment(of: [expectation], timeout: 5.0)
// Then: Verify recovery attempted (app doesn't crash)
let pendingAfter = try await notificationCenter.pendingNotificationRequests()
XCTAssertNoThrow(pendingAfter, "App should not crash during recovery")
}
// MARK: - Error Handling Tests
/**
* Test that database errors don't crash the app
*/
func testErrorHandling_DatabaseError() async throws {
// Given: Storage with notifications
let notification = NotificationContent(
id: UUID().uuidString,
title: "Test",
body: "Body",
scheduledTime: Int64(Date().addingTimeInterval(3600).timeIntervalSince1970 * 1000),
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: nil,
payload: nil,
etag: nil
)
storage.saveNotificationContent(notification)
// When: Close database to simulate error
database.close()
// Perform recovery (should handle error gracefully)
let expectation = XCTestExpectation(description: "Recovery handles error")
reactivationManager.performRecovery()
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
expectation.fulfill()
}
wait(for: [expectation], timeout: 3.0)
// Then: App should not crash
XCTAssertTrue(true, "App should not crash on database error")
}
/**
* Test that notification center errors don't crash the app
*/
func testErrorHandling_NotificationCenterError() async throws {
// Given: Storage with notifications
let notification = NotificationContent(
id: UUID().uuidString,
title: "Test",
body: "Body",
scheduledTime: Int64(Date().addingTimeInterval(3600).timeIntervalSince1970 * 1000),
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: nil,
payload: nil,
etag: nil
)
storage.saveNotificationContent(notification)
// When: Perform recovery (notification center may have errors)
// Note: We can't easily simulate notification center errors, but we can verify
// that recovery handles them gracefully
let expectation = XCTestExpectation(description: "Recovery handles notification center")
reactivationManager.performRecovery()
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
expectation.fulfill()
}
wait(for: [expectation], timeout: 3.0)
// Then: App should not crash
XCTAssertTrue(true, "App should not crash on notification center error")
}
/**
* Test that scheduling errors don't crash the app
*/
func testErrorHandling_SchedulingError() async throws {
// Given: Invalid notification (missing required fields)
// Note: We'll create a notification that might fail to schedule
let notification = NotificationContent(
id: "", // Empty ID might cause issues
title: nil,
body: nil,
scheduledTime: Int64(Date().addingTimeInterval(3600).timeIntervalSince1970 * 1000),
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: nil,
payload: nil,
etag: nil
)
// When: Try to schedule (may fail)
let success = await scheduler.scheduleNotification(notification)
// Scheduling may succeed or fail, but shouldn't crash
// Then: App should not crash
XCTAssertNoThrow(success, "App should not crash on scheduling error")
}
/**
* Test partial recovery when some operations fail
*/
func testErrorHandling_PartialRecovery() async throws {
// Given: Multiple notifications, some valid, some invalid
let validNotification = NotificationContent(
id: UUID().uuidString,
title: "Valid",
body: "Body",
scheduledTime: Int64(Date().addingTimeInterval(3600).timeIntervalSince1970 * 1000),
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: nil,
payload: nil,
etag: nil
)
let invalidNotification = NotificationContent(
id: "", // Invalid: empty ID
title: nil,
body: nil,
scheduledTime: Int64(Date().addingTimeInterval(3600).timeIntervalSince1970 * 1000),
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: nil,
payload: nil,
etag: nil
)
storage.saveNotificationContent(validNotification)
storage.saveNotificationContent(invalidNotification)
// When: Perform recovery
let expectation = XCTestExpectation(description: "Recovery with partial failures")
reactivationManager.performRecovery()
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
expectation.fulfill()
}
wait(for: [expectation], timeout: 3.0)
// Then: App should not crash, recovery should complete with partial results
XCTAssertTrue(true, "App should handle partial failures gracefully")
}
/**
* Test that recovery timeout doesn't crash the app
*/
func testErrorHandling_RecoveryTimeout() async throws {
// Given: Many notifications to process (might cause timeout)
for i in 1...10 {
let notification = NotificationContent(
id: UUID().uuidString,
title: "Test \(i)",
body: "Body",
scheduledTime: Int64(Date().addingTimeInterval(3600).timeIntervalSince1970 * 1000),
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: nil,
payload: nil,
etag: nil
)
storage.saveNotificationContent(notification)
}
// When: Perform recovery (may timeout)
let expectation = XCTestExpectation(description: "Recovery timeout handling")
reactivationManager.performRecovery()
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
expectation.fulfill()
}
await fulfillment(of: [expectation], timeout: 5.0)
// Then: App should not crash on timeout
XCTAssertTrue(true, "App should handle timeout gracefully")
}
// MARK: - App Stability Tests
/**
* Test that app doesn't crash during recovery
*/
func testAppStability_NoCrashOnRecovery() async throws {
// Given: Various notification states
let pastNotification = NotificationContent(
id: UUID().uuidString,
title: "Past",
body: "Body",
scheduledTime: Int64(Date().addingTimeInterval(-3600).timeIntervalSince1970 * 1000), // 1 hour ago
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: nil,
payload: nil,
etag: nil
)
let futureNotification = NotificationContent(
id: UUID().uuidString,
title: "Future",
body: "Body",
scheduledTime: Int64(Date().addingTimeInterval(3600).timeIntervalSince1970 * 1000), // 1 hour from now
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: nil,
payload: nil,
etag: nil
)
storage.saveNotificationContent(pastNotification)
storage.saveNotificationContent(futureNotification)
// When: Perform recovery multiple times
for _ in 1...3 {
let expectation = XCTestExpectation(description: "Recovery iteration")
reactivationManager.performRecovery()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
expectation.fulfill()
}
wait(for: [expectation], timeout: 2.0)
}
// Then: App should not crash
XCTAssertTrue(true, "App should remain stable after multiple recoveries")
}
/**
* Test recovery with empty storage
*/
func testAppStability_EmptyStorage() async throws {
// Given: Empty storage (no notifications)
// When: Perform recovery
let expectation = XCTestExpectation(description: "Recovery with empty storage")
reactivationManager.performRecovery()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
expectation.fulfill()
}
await fulfillment(of: [expectation], timeout: 2.0)
// Then: App should not crash
XCTAssertTrue(true, "App should handle empty storage gracefully")
}
/**
* Test recovery with concurrent operations
*/
func testAppStability_ConcurrentRecovery() async throws {
// Given: Notifications in storage
let notification = NotificationContent(
id: UUID().uuidString,
title: "Test",
body: "Body",
scheduledTime: Int64(Date().addingTimeInterval(3600).timeIntervalSince1970 * 1000),
fetchedAt: Int64(Date().timeIntervalSince1970 * 1000),
url: nil,
payload: nil,
etag: nil
)
storage.saveNotificationContent(notification)
// When: Perform recovery concurrently
let expectation1 = XCTestExpectation(description: "Recovery 1")
let expectation2 = XCTestExpectation(description: "Recovery 2")
reactivationManager.performRecovery()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
expectation1.fulfill()
}
reactivationManager.performRecovery()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
expectation2.fulfill()
}
wait(for: [expectation1, expectation2], timeout: 3.0)
// Then: App should not crash
XCTAssertTrue(true, "App should handle concurrent recovery gracefully")
}
}

View File

@@ -0,0 +1,469 @@
//
// NotificationConfigDAOTests.swift
// DailyNotificationPluginTests
//
// Created by Matthew Raymer on 2025-12-08
// Copyright © 2025 TimeSafari. All rights reserved.
//
import XCTest
import CoreData
@testable import DailyNotificationPlugin
/**
* Unit tests for NotificationConfigDAO
*
* Tests CRUD operations and query helpers for configuration management
*/
class NotificationConfigDAOTests: XCTestCase {
var persistenceController: PersistenceController!
var context: NSManagedObjectContext!
override func setUp() {
super.setUp()
// Create in-memory Core Data stack
persistenceController = PersistenceController(inMemory: true)
context = persistenceController.viewContext
XCTAssertNotNil(context, "Context should be available")
}
override func tearDown() {
context = nil
persistenceController = nil
super.tearDown()
}
// MARK: - Create/Insert Tests
func testCreate_WithAllParameters() {
// Given: All parameters
let id = UUID().uuidString
// When: Create entity
let entity = NotificationConfig.create(
in: context,
id: id,
timesafariDid: "test-did",
configType: "notification",
configKey: "sound_enabled",
configValue: "true",
configDataType: "bool",
isEncrypted: false,
encryptionKeyId: nil,
ttlSeconds: 86400,
isActive: true,
metadata: "{\"key\":\"value\"}"
)
// Then: Entity should be created with correct values
XCTAssertNotNil(entity, "Entity should be created")
XCTAssertEqual(entity.id, id)
XCTAssertEqual(entity.timesafariDid, "test-did")
XCTAssertEqual(entity.configType, "notification")
XCTAssertEqual(entity.configKey, "sound_enabled")
XCTAssertEqual(entity.configValue, "true")
XCTAssertEqual(entity.configDataType, "bool")
XCTAssertEqual(entity.isEncrypted, false)
XCTAssertEqual(entity.ttlSeconds, 86400)
XCTAssertEqual(entity.isActive, true)
XCTAssertNotNil(entity.createdAt)
XCTAssertNotNil(entity.updatedAt)
}
func testCreate_WithMinimalParameters() {
// Given: Minimal parameters (only required id)
let id = UUID().uuidString
// When: Create entity
let entity = NotificationConfig.create(
in: context,
id: id
)
// Then: Entity should be created with defaults
XCTAssertNotNil(entity, "Entity should be created")
XCTAssertEqual(entity.id, id)
XCTAssertEqual(entity.isEncrypted, false) // Default
XCTAssertEqual(entity.ttlSeconds, 604800) // Default (7 days)
XCTAssertEqual(entity.isActive, true) // Default
XCTAssertNotNil(entity.createdAt)
XCTAssertNotNil(entity.updatedAt)
}
func testCreate_FromDictionary_WithEpochMillis() {
// Given: Dictionary with epoch milliseconds
let createdAtMillis: Int64 = 1609459200000
let dict: [String: Any] = [
"id": "test-id",
"configKey": "test_key",
"configValue": "test_value",
"createdAt": createdAtMillis,
"isActive": true
]
// When: Create from dictionary
let entity = NotificationConfig.create(in: context, from: dict)
// Then: Entity should be created with converted dates
XCTAssertNotNil(entity, "Entity should be created")
XCTAssertEqual(entity.id, "test-id")
XCTAssertEqual(entity.configKey, "test_key")
XCTAssertEqual(entity.configValue, "test_value")
XCTAssertEqual(entity.isActive, true)
// Verify date conversion
let expectedDate = DailyNotificationDataConversions.dateFromEpochMillis(createdAtMillis)
XCTAssertEqual(entity.createdAt, expectedDate)
}
func testCreate_FromDictionary_MissingRequiredId() {
// Given: Dictionary without required id
let dict: [String: Any] = [
"configKey": "test_key"
]
// When: Create from dictionary
let entity = NotificationConfig.create(in: context, from: dict)
// Then: Should be nil
XCTAssertNil(entity, "Missing id should produce nil")
}
// MARK: - Read/Query Tests
func testFetch_ById_Found() {
// Given: Entity in database
let id = UUID().uuidString
let entity = NotificationConfig.create(
in: context,
id: id
)
try! context.save()
// When: Fetch by id
let fetched = NotificationConfig.fetch(by: id, in: context)
// Then: Should find entity
XCTAssertNotNil(fetched, "Should find entity")
XCTAssertEqual(fetched?.id, id)
}
func testFetch_ById_NotFound() {
// Given: No entity in database
// When: Fetch by non-existent id
let fetched = NotificationConfig.fetch(by: "non-existent", in: context)
// Then: Should be nil
XCTAssertNil(fetched, "Should not find entity")
}
func testFetch_ByConfigKey_Found() {
// Given: Entity with configKey
let configKey = "sound_enabled"
let entity = NotificationConfig.create(
in: context,
id: UUID().uuidString,
configKey: configKey
)
try! context.save()
// When: Fetch by configKey
let fetched = NotificationConfig.fetch(by: configKey, in: context)
// Then: Should find entity
XCTAssertNotNil(fetched, "Should find entity")
XCTAssertEqual(fetched?.configKey, configKey)
}
func testFetch_ByConfigKey_NotFound() {
// Given: No entity in database
// When: Fetch by non-existent configKey
let fetched = NotificationConfig.fetch(by: "non-existent", in: context)
// Then: Should be nil
XCTAssertNil(fetched, "Should not find entity")
}
func testFetchAll_Empty() {
// Given: Empty database
// When: Fetch all
let all = NotificationConfig.fetchAll(in: context)
// Then: Should be empty
XCTAssertEqual(all.count, 0, "Should be empty")
}
func testFetchAll_WithEntities() {
// Given: Multiple entities
for i in 1...5 {
NotificationConfig.create(
in: context,
id: "id-\(i)"
)
}
try! context.save()
// When: Fetch all
let all = NotificationConfig.fetchAll(in: context)
// Then: Should find all
XCTAssertEqual(all.count, 5, "Should find all entities")
}
func testQuery_ByTimesafariDid() {
// Given: Entities with different timesafariDid
NotificationConfig.create(
in: context,
id: "id-1",
timesafariDid: "did-1"
)
NotificationConfig.create(
in: context,
id: "id-2",
timesafariDid: "did-1"
)
NotificationConfig.create(
in: context,
id: "id-3",
timesafariDid: "did-2"
)
try! context.save()
// When: Query by timesafariDid
let results = NotificationConfig.query(by: "did-1", in: context)
// Then: Should find only matching entities
XCTAssertEqual(results.count, 2, "Should find 2 entities")
XCTAssertTrue(results.allSatisfy { $0.timesafariDid == "did-1" })
}
func testQuery_ByConfigType() {
// Given: Entities with different config types
NotificationConfig.create(
in: context,
id: "id-1",
configType: "notification"
)
NotificationConfig.create(
in: context,
id: "id-2",
configType: "notification"
)
NotificationConfig.create(
in: context,
id: "id-3",
configType: "scheduling"
)
try! context.save()
// When: Query by configType
let results = NotificationConfig.query(by: "notification", in: context)
// Then: Should find only matching entities
XCTAssertEqual(results.count, 2, "Should find 2 entities")
XCTAssertTrue(results.allSatisfy { $0.configType == "notification" })
}
func testQueryActive() {
// Given: Entities with different active states
NotificationConfig.create(
in: context,
id: "id-1",
isActive: true
)
NotificationConfig.create(
in: context,
id: "id-2",
isActive: true
)
NotificationConfig.create(
in: context,
id: "id-3",
isActive: false
)
try! context.save()
// When: Query active
let results = NotificationConfig.queryActive(in: context)
// Then: Should find only active entities
XCTAssertEqual(results.count, 2, "Should find 2 active entities")
XCTAssertTrue(results.allSatisfy { $0.isActive == true })
}
func testQuery_ByConfigTypeAndIsActive() {
// Given: Entities with different types and active states
NotificationConfig.create(
in: context,
id: "id-1",
configType: "notification",
isActive: true
)
NotificationConfig.create(
in: context,
id: "id-2",
configType: "notification",
isActive: true
)
NotificationConfig.create(
in: context,
id: "id-3",
configType: "notification",
isActive: false
)
NotificationConfig.create(
in: context,
id: "id-4",
configType: "scheduling",
isActive: true
)
try! context.save()
// When: Query by configType and isActive
let results = NotificationConfig.query(
by: "notification",
isActive: true,
in: context
)
// Then: Should find only matching entities
XCTAssertEqual(results.count, 2, "Should find 2 entities")
XCTAssertTrue(results.allSatisfy {
$0.configType == "notification" && $0.isActive == true
})
}
// MARK: - Update Tests
func testUpdateValue() {
// Given: Entity with initial value
let entity = NotificationConfig.create(
in: context,
id: UUID().uuidString,
configValue: "old_value"
)
let originalUpdatedAt = entity.updatedAt
try! context.save()
// Wait a bit to ensure time difference
Thread.sleep(forTimeInterval: 0.1)
// When: Update value
entity.updateValue("new_value")
try! context.save()
// Then: Value and updatedAt should be updated
XCTAssertEqual(entity.configValue, "new_value")
XCTAssertNotNil(entity.updatedAt)
XCTAssertGreaterThan(entity.updatedAt!, originalUpdatedAt!)
}
func testSetActive() {
// Given: Entity with initial active state
let entity = NotificationConfig.create(
in: context,
id: UUID().uuidString,
isActive: true
)
try! context.save()
// When: Set inactive
entity.setActive(false)
try! context.save()
// Then: Active state should be updated
XCTAssertEqual(entity.isActive, false)
}
func testTouch_UpdatesUpdatedAt() {
// Given: Entity with original updatedAt
let entity = NotificationConfig.create(
in: context,
id: UUID().uuidString
)
let originalUpdatedAt = entity.updatedAt
try! context.save()
// Wait a bit to ensure time difference
Thread.sleep(forTimeInterval: 0.1)
// When: Touch entity
entity.touch()
try! context.save()
// Then: updatedAt should be newer
XCTAssertNotNil(entity.updatedAt)
XCTAssertGreaterThan(entity.updatedAt!, originalUpdatedAt!)
}
// MARK: - Delete Tests
func testDelete_ById_Found() {
// Given: Entity in database
let id = UUID().uuidString
NotificationConfig.create(
in: context,
id: id
)
try! context.save()
// When: Delete by id
let deleted = NotificationConfig.delete(by: id, in: context)
// Then: Should be deleted
XCTAssertTrue(deleted, "Should delete entity")
// Verify deleted
let fetched = NotificationConfig.fetch(by: id, in: context)
XCTAssertNil(fetched, "Entity should be deleted")
}
func testDelete_ByConfigKey_Found() {
// Given: Entity with configKey
let configKey = "sound_enabled"
NotificationConfig.create(
in: context,
id: UUID().uuidString,
configKey: configKey
)
try! context.save()
// When: Delete by configKey
let deleted = NotificationConfig.delete(by: configKey, in: context)
// Then: Should be deleted
XCTAssertTrue(deleted, "Should delete entity")
// Verify deleted
let fetched = NotificationConfig.fetch(by: configKey, in: context)
XCTAssertNil(fetched, "Entity should be deleted")
}
func testDeleteAll() {
// Given: Multiple entities
for i in 1...5 {
NotificationConfig.create(
in: context,
id: "id-\(i)"
)
}
try! context.save()
// When: Delete all
let count = NotificationConfig.deleteAll(in: context)
// Then: Should delete all
XCTAssertEqual(count, 5, "Should delete 5 entities")
// Verify all deleted
let all = NotificationConfig.fetchAll(in: context)
XCTAssertEqual(all.count, 0, "Should be empty")
}
}

Some files were not shown because too many files have changed in this diff Show More