5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 16:59:31 +08:00

update example

This commit is contained in:
popaprozac 2025-03-24 21:34:10 -07:00
parent 7d0f7f4652
commit af5abda8b1

View File

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/wailsapp/wails/v3/pkg/application" "github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
"github.com/wailsapp/wails/v3/pkg/services/notifications" "github.com/wailsapp/wails/v3/pkg/services/notifications"
) )
@ -61,85 +62,87 @@ func main() {
URL: "/", URL: "/",
}) })
// Create a goroutine that spawns desktop notifications from Go app.OnApplicationEvent(events.Common.ApplicationStarted, func(event *application.ApplicationEvent) {
go func() { // Create a goroutine that spawns desktop notifications from Go
var authorized bool go func() {
var err error var authorized bool
authorized, err = ns.CheckNotificationAuthorization() var err error
if err != nil { authorized, err = ns.CheckNotificationAuthorization()
println(fmt.Errorf("checking app notification authorization failed: %s", err))
}
if !authorized {
authorized, err = ns.RequestNotificationAuthorization()
if err != nil { if err != nil {
println(fmt.Errorf("requesting app notification authorization failed: %s", err)) println(fmt.Errorf("checking app notification authorization failed: %s", err))
} }
}
if authorized { if !authorized {
ns.OnNotificationResponse(func(result notifications.NotificationResult) { authorized, err = ns.RequestNotificationAuthorization()
if result.Error != nil { if err != nil {
println(fmt.Errorf("parsing notification result failed: %s", result.Error)) println(fmt.Errorf("requesting app notification authorization failed: %s", err))
} else {
fmt.Printf("Response: %+v\n", result.Response)
println("Sending response to frontend...")
app.EmitEvent("notification:action", result.Response)
} }
})
err = ns.SendNotification(notifications.NotificationOptions{
ID: "uuid-basic-1",
Title: "Notification Title",
Subtitle: "Subtitle on macOS and Linux",
Body: "Body text of notification.",
Data: map[string]interface{}{
"user-id": "user-123",
"message-id": "msg-123",
"timestamp": time.Now().Unix(),
},
})
if err != nil {
println(fmt.Errorf("sending basic notification failed: %s", err))
} }
// Delay before sending next notification if authorized {
time.Sleep(time.Second * 2) ns.OnNotificationResponse(func(result notifications.NotificationResult) {
if result.Error != nil {
println(fmt.Errorf("parsing notification result failed: %s", result.Error))
} else {
fmt.Printf("Response: %+v\n", result.Response)
println("Sending response to frontend...")
app.EmitEvent("notification:action", result.Response)
}
})
const CategoryID = "backend-notification-id" err = ns.SendNotification(notifications.NotificationOptions{
ID: "uuid-basic-1",
Title: "Notification Title",
Subtitle: "Subtitle on macOS and Linux",
Body: "Body text of notification.",
Data: map[string]interface{}{
"user-id": "user-123",
"message-id": "msg-123",
"timestamp": time.Now().Unix(),
},
})
if err != nil {
println(fmt.Errorf("sending basic notification failed: %s", err))
}
err = ns.RegisterNotificationCategory(notifications.NotificationCategory{ // Delay before sending next notification
ID: CategoryID, time.Sleep(time.Second * 2)
Actions: []notifications.NotificationAction{
{ID: "VIEW", Title: "View"}, const CategoryID = "backend-notification-id"
{ID: "MARK_READ", Title: "Mark as read"},
{ID: "DELETE", Title: "Delete", Destructive: true}, err = ns.RegisterNotificationCategory(notifications.NotificationCategory{
}, ID: CategoryID,
HasReplyField: true, Actions: []notifications.NotificationAction{
ReplyPlaceholder: "Message...", {ID: "VIEW", Title: "View"},
ReplyButtonTitle: "Reply", {ID: "MARK_READ", Title: "Mark as read"},
}) {ID: "DELETE", Title: "Delete", Destructive: true},
if err != nil { },
println(fmt.Errorf("creating notification category failed: %s", err)) HasReplyField: true,
ReplyPlaceholder: "Message...",
ReplyButtonTitle: "Reply",
})
if err != nil {
println(fmt.Errorf("creating notification category failed: %s", err))
}
err = ns.SendNotificationWithActions(notifications.NotificationOptions{
ID: "uuid-with-actions-1",
Title: "Actions Notification Title",
Subtitle: "Subtitle on macOS and Linux",
Body: "Body text of notification with actions.",
CategoryID: CategoryID,
Data: map[string]interface{}{
"user-id": "user-123",
"message-id": "msg-123",
"timestamp": time.Now().Unix(),
},
})
if err != nil {
println(fmt.Errorf("sending notification with actions failed: %s", err))
}
} }
}()
err = ns.SendNotificationWithActions(notifications.NotificationOptions{ })
ID: "uuid-with-actions-1",
Title: "Actions Notification Title",
Subtitle: "Subtitle on macOS and Linux",
Body: "Body text of notification with actions.",
CategoryID: CategoryID,
Data: map[string]interface{}{
"user-id": "user-123",
"message-id": "msg-123",
"timestamp": time.Now().Unix(),
},
})
if err != nil {
println(fmt.Errorf("sending notification with actions failed: %s", err))
}
}
}()
// Run the application. This blocks until the application has been exited. // Run the application. This blocks until the application has been exited.
err := app.Run() err := app.Run()