refactor: improve build configuration and code organization
- Add build scripts for Android and iOS platforms - Remove duplicate web implementation (src/web.ts) - Add proper TypeScript configuration - Add module documentation to index.ts - Clean up package.json scripts This commit improves the project structure and build process by: 1. Adding dedicated build scripts for native platforms 2. Removing redundant web implementation 3. Adding proper TypeScript configuration with strict mode 4. Improving code documentation 5. Organizing package.json scripts The changes maintain backward compatibility while improving the development experience and code quality.
This commit is contained in:
109
ios/Plugin/DailyNotificationPlugin.swift
Normal file
109
ios/Plugin/DailyNotificationPlugin.swift
Normal file
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* DailyNotificationPlugin.swift
|
||||
* Daily Notification Plugin for Capacitor
|
||||
*
|
||||
* Handles daily notification scheduling and management on iOS
|
||||
*/
|
||||
|
||||
import Foundation
|
||||
import Capacitor
|
||||
import UserNotifications
|
||||
|
||||
@objc(DailyNotificationPlugin)
|
||||
public class DailyNotificationPlugin: CAPPlugin {
|
||||
private let notificationCenter = UNUserNotificationCenter.current()
|
||||
|
||||
@objc func scheduleDailyNotification(_ call: CAPPluginCall) {
|
||||
guard let url = call.getString("url"),
|
||||
let time = call.getString("time") else {
|
||||
call.reject("Missing required parameters")
|
||||
return
|
||||
}
|
||||
|
||||
// Parse time string (HH:mm format)
|
||||
let timeComponents = time.split(separator: ":")
|
||||
guard timeComponents.count == 2,
|
||||
let hour = Int(timeComponents[0]),
|
||||
let minute = Int(timeComponents[1]),
|
||||
hour >= 0 && hour < 24,
|
||||
minute >= 0 && minute < 60 else {
|
||||
call.reject("Invalid time format")
|
||||
return
|
||||
}
|
||||
|
||||
// Create notification content
|
||||
let content = UNMutableNotificationContent()
|
||||
content.title = call.getString("title") ?? "Daily Notification"
|
||||
content.body = call.getString("body") ?? "Your daily update is ready"
|
||||
content.sound = call.getBool("sound", true) ? .default : nil
|
||||
|
||||
// Set priority
|
||||
if let priority = call.getString("priority") {
|
||||
switch priority {
|
||||
case "high":
|
||||
content.interruptionLevel = .timeSensitive
|
||||
case "low":
|
||||
content.interruptionLevel = .passive
|
||||
default:
|
||||
content.interruptionLevel = .active
|
||||
}
|
||||
}
|
||||
|
||||
// Create trigger for daily notification
|
||||
var dateComponents = DateComponents()
|
||||
dateComponents.hour = hour
|
||||
dateComponents.minute = minute
|
||||
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
|
||||
|
||||
// Create request
|
||||
let request = UNNotificationRequest(
|
||||
identifier: "daily-notification-\(url)",
|
||||
content: content,
|
||||
trigger: trigger
|
||||
)
|
||||
|
||||
// Schedule notification
|
||||
notificationCenter.add(request) { error in
|
||||
if let error = error {
|
||||
call.reject("Failed to schedule notification: \(error.localizedDescription)")
|
||||
} else {
|
||||
call.resolve()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@objc func getLastNotification(_ call: CAPPluginCall) {
|
||||
notificationCenter.getDeliveredNotifications { notifications in
|
||||
let lastNotification = notifications.first
|
||||
let result: [String: Any] = [
|
||||
"id": lastNotification?.request.identifier ?? "",
|
||||
"title": lastNotification?.request.content.title ?? "",
|
||||
"body": lastNotification?.request.content.body ?? "",
|
||||
"timestamp": lastNotification?.date.timeIntervalSince1970 ?? 0
|
||||
]
|
||||
call.resolve(result)
|
||||
}
|
||||
}
|
||||
|
||||
@objc func cancelAllNotifications(_ call: CAPPluginCall) {
|
||||
notificationCenter.removeAllPendingNotificationRequests()
|
||||
notificationCenter.removeAllDeliveredNotifications()
|
||||
call.resolve()
|
||||
}
|
||||
|
||||
@objc func getNotificationStatus(_ call: CAPPluginCall) {
|
||||
notificationCenter.getPendingNotificationRequests { requests in
|
||||
let nextNotification = requests.first
|
||||
let result: [String: Any] = [
|
||||
"nextNotificationTime": nextNotification?.trigger?.nextTriggerDate?.timeIntervalSince1970 ?? 0,
|
||||
"isEnabled": true // TODO: Check system notification settings
|
||||
]
|
||||
call.resolve(result)
|
||||
}
|
||||
}
|
||||
|
||||
@objc func updateSettings(_ call: CAPPluginCall) {
|
||||
// TODO: Implement settings update
|
||||
call.resolve()
|
||||
}
|
||||
}
|
||||
39
ios/Plugin/README.md
Normal file
39
ios/Plugin/README.md
Normal file
@@ -0,0 +1,39 @@
|
||||
# iOS Implementation
|
||||
|
||||
This directory contains the iOS-specific implementation of the DailyNotification plugin.
|
||||
|
||||
## Implementation Details
|
||||
|
||||
The iOS implementation uses:
|
||||
|
||||
- `BGTaskScheduler` for background data fetching
|
||||
- `UNUserNotificationCenter` for notification management
|
||||
- `UserDefaults` for local data storage
|
||||
- iOS notification categories and actions
|
||||
|
||||
## Native Code Location
|
||||
|
||||
The native iOS implementation is located in the `ios/` directory at the project root.
|
||||
|
||||
## Key Components
|
||||
|
||||
1. `DailyNotificationIOS.swift`: Main plugin class
|
||||
2. `BackgroundTaskManager.swift`: Handles background fetch scheduling
|
||||
3. `NotificationManager.swift`: Manages notification creation and display
|
||||
4. `DataStore.swift`: Handles local data persistence
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
- Uses BGTaskScheduler for reliable background execution
|
||||
- Implements proper battery optimization handling
|
||||
- Supports iOS notification categories and actions
|
||||
- Handles background refresh limitations
|
||||
- Uses UserDefaults for lightweight data storage
|
||||
|
||||
## Testing
|
||||
|
||||
Run iOS-specific tests with:
|
||||
|
||||
```bash
|
||||
npm run test:ios
|
||||
```
|
||||
58
ios/Plugin/index.ts
Normal file
58
ios/Plugin/index.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* iOS implementation of the DailyNotification plugin
|
||||
* @module DailyNotificationIOS
|
||||
*/
|
||||
|
||||
import { Capacitor } from '@capacitor/core';
|
||||
import type { DailyNotificationPlugin, DailyNotificationOptions, PermissionStatus } from '../definitions';
|
||||
|
||||
export class DailyNotificationIOS implements DailyNotificationPlugin {
|
||||
private options: DailyNotificationOptions = {
|
||||
url: '',
|
||||
notificationTime: '09:00',
|
||||
title: 'Daily Update',
|
||||
body: 'Your daily notification is ready'
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize the daily notification system for iOS
|
||||
* @param options Configuration options for the notification system
|
||||
*/
|
||||
async initialize(options: DailyNotificationOptions): Promise<void> {
|
||||
if (Capacitor.getPlatform() !== 'ios') {
|
||||
throw new Error('This implementation is for iOS only');
|
||||
}
|
||||
this.options = options;
|
||||
// TODO: Implement iOS-specific initialization
|
||||
}
|
||||
|
||||
/**
|
||||
* Check current permission status for notifications and background refresh
|
||||
* @returns Current permission status
|
||||
*/
|
||||
async checkPermissions(): Promise<PermissionStatus> {
|
||||
if (Capacitor.getPlatform() !== 'ios') {
|
||||
throw new Error('This implementation is for iOS only');
|
||||
}
|
||||
// TODO: Implement iOS-specific permission check
|
||||
return {
|
||||
notifications: 'prompt',
|
||||
backgroundRefresh: 'prompt'
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Request notification and background refresh permissions from the user
|
||||
* @returns Updated permission status after request
|
||||
*/
|
||||
async requestPermissions(): Promise<PermissionStatus> {
|
||||
if (Capacitor.getPlatform() !== 'ios') {
|
||||
throw new Error('This implementation is for iOS only');
|
||||
}
|
||||
// TODO: Implement iOS-specific permission request
|
||||
return {
|
||||
notifications: 'prompt',
|
||||
backgroundRefresh: 'prompt'
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user