5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-10 15:20:41 +08:00

update example

This commit is contained in:
popaprozac 2025-02-23 16:30:13 -08:00
parent bf4222c9cc
commit c904433ff3
2 changed files with 58 additions and 46 deletions

View File

@ -1,7 +1,6 @@
import * as Notifications from "./bindings/github.com/wailsapp/wails/v3/pkg/services/notifications/service"; import * as Notifications from "./bindings/github.com/wailsapp/wails/v3/pkg/services/notifications/service";
import { Events, System } from "@wailsio/runtime"; import { Events, System } from "@wailsio/runtime";
const timeElement = document.getElementById('time');
const notificationsElement = document.getElementById('notifications'); const notificationsElement = document.getElementById('notifications');
window.sendNotification = async () => { window.sendNotification = async () => {
@ -51,10 +50,6 @@ window.sendComplexNotification = async () => {
} }
} }
Events.On('time', (time) => {
timeElement.innerText = time.data;
});
Events.On("notificationResponse", (response) => { Events.On("notificationResponse", (response) => {
console.log(response) console.log(response)
notificationsElement.innerText += JSON.stringify(response.data[0].data); notificationsElement.innerText += JSON.stringify(response.data[0].data);

View File

@ -60,18 +60,28 @@ func main() {
URL: "/", URL: "/",
}) })
go func() {
app.OnEvent("notificationResponse", func(event *application.CustomEvent) { app.OnEvent("notificationResponse", func(event *application.CustomEvent) {
data, _ := json.Marshal(event.Data) data, _ := json.Marshal(event.Data)
fmt.Printf("%s\n", string(data)) fmt.Printf("%s\n", string(data))
}) })
NotificationService.RequestUserNotificationAuthorization() go func() {
NotificationService.CheckNotificationAuthorization() granted, err := NotificationService.RequestUserNotificationAuthorization()
if err != nil {
log.Default().Printf("WARNING: %s\n", err)
return
}
if granted {
time.Sleep(time.Second * 2) time.Sleep(time.Second * 2)
var uuid1 string = "Wails Notification Demo"
if application.Get().Environment().OS == "darwin" {
uuid1 = "uuid1"
}
NotificationService.SendNotification(notifications.NotificationOptions{ NotificationService.SendNotification(notifications.NotificationOptions{
ID: "Wails Notification Demo", ID: uuid1,
Title: "Title!", Title: "Title!",
Body: "Body!", Body: "Body!",
Data: map[string]interface{}{ Data: map[string]interface{}{
@ -82,6 +92,12 @@ func main() {
}) })
time.Sleep(time.Second * 2) time.Sleep(time.Second * 2)
var uuid2 string = "Wails Notification Demo"
if application.Get().Environment().OS == "darwin" {
uuid2 = "uuid2"
}
NotificationService.RegisterNotificationCategory(notifications.NotificationCategory{ NotificationService.RegisterNotificationCategory(notifications.NotificationCategory{
ID: "BACKEND_NOTIF", ID: "BACKEND_NOTIF",
Actions: []notifications.NotificationAction{ Actions: []notifications.NotificationAction{
@ -95,7 +111,7 @@ func main() {
}) })
NotificationService.SendNotificationWithActions(notifications.NotificationOptions{ NotificationService.SendNotificationWithActions(notifications.NotificationOptions{
ID: "Wails Notification Demo", ID: uuid2,
Title: "Complex Backend Notification", Title: "Complex Backend Notification",
Subtitle: "Should not show on Windows", Subtitle: "Should not show on Windows",
Body: "Is it raining today where you are?", Body: "Is it raining today where you are?",
@ -106,6 +122,7 @@ func main() {
"timestamp": time.Now().String(), "timestamp": time.Now().String(),
}, },
}) })
}
}() }()
// Run the application. This blocks until the application has been exited. // Run the application. This blocks until the application has been exited.