diff --git a/README.md b/README.md index 86ba4f1..7df55e9 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,6 @@ TODO - [x] 保存历史 - [x] 自动接收 - [ ] 传输加密 -- [ ] 单例模式 \ No newline at end of file +- [x] 单例模式 +- [ ] icon +- [x] 通知 \ No newline at end of file diff --git a/internal/transfer/server.go b/internal/transfer/server.go index 75ea198..5801b34 100644 --- a/internal/transfer/server.go +++ b/internal/transfer/server.go @@ -15,6 +15,7 @@ import ( "github.com/gin-gonic/gin" "github.com/google/uuid" + "github.com/wailsapp/wails/v3/pkg/services/notifications" ) // handleAsk 处理接收文件请求 @@ -48,6 +49,12 @@ func (s *Service) handleAsk(c *gin.Context) { Accepted: true, SavePath: s.config.GetSavePath(), } + } else { + _ = s.notifier.SendNotification(notifications.NotificationOptions{ + ID: uuid.New().String(), + Title: "File Transfer Request", + Body: fmt.Sprintf("%s wants to transfer %s", task.Sender, task.FileName), + }) } // 等待用户决策或发送端放弃 diff --git a/internal/transfer/service.go b/internal/transfer/service.go index e1ca57a..6f54d27 100644 --- a/internal/transfer/service.go +++ b/internal/transfer/service.go @@ -10,12 +10,14 @@ import ( "github.com/gin-gonic/gin" "github.com/wailsapp/wails/v3/pkg/application" + "github.com/wailsapp/wails/v3/pkg/services/notifications" ) type Service struct { - config *config.Config - app *application.App - port int + config *config.Config + notifier *notifications.NotificationService + app *application.App + port int // pendingRequests 存储等待用户确认的通道 // Key: TransferID, Value: *Transfer @@ -28,11 +30,12 @@ type Service struct { cancelMap sync.Map } -func NewService(config *config.Config, app *application.App, port int, discoveryService *discovery.Service) *Service { +func NewService(config *config.Config, app *application.App, notifier *notifications.NotificationService, port int, discoveryService *discovery.Service) *Service { gin.SetMode(gin.ReleaseMode) return &Service{ app: app, + notifier: notifier, port: port, discoveryService: discoveryService, config: config, diff --git a/main.go b/main.go index 112a04a..a6c4e56 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/wailsapp/wails/v3/pkg/application" "github.com/wailsapp/wails/v3/pkg/events" + "github.com/wailsapp/wails/v3/pkg/services/notifications" ) //go:embed all:frontend/dist @@ -28,6 +29,9 @@ func main() { Assets: application.AssetOptions{ Handler: application.AssetFileServerFS(assets), }, + SingleInstance: &application.SingleInstanceOptions{ + UniqueID: "com.nite07.mesh-drop", + }, }) // 创建保存路径 @@ -36,7 +40,16 @@ func main() { slog.Error("Failed to create save path", "path", conf.SavePath, "error", err) } - // 文件传输端口 + // 通知 + notifier := notifications.New() + authorized, err := notifier.RequestNotificationAuthorization() + if err != nil { + slog.Error("Failed to request notification authorization", "error", err) + } + if !authorized { + slog.Error("Notification authorization not granted") + } + port := 9989 // 初始化发现服务 @@ -44,7 +57,7 @@ func main() { discoveryService.Start() // 初始化传输服务 - transferService := transfer.NewService(conf, app, port, discoveryService) + transferService := transfer.NewService(conf, app, notifier, port, discoveryService) transferService.Start() // 加载传输历史 if conf.GetSaveHistory() { @@ -56,6 +69,7 @@ func main() { app.RegisterService(application.NewService(discoveryService)) app.RegisterService(application.NewService(transferService)) app.RegisterService(application.NewService(conf)) + app.RegisterService(application.NewService(notifier)) window := app.Window.NewWithOptions(application.WebviewWindowOptions{ Title: "mesh drop",