From 67adc452cbb898b4fdb30910542a4fd10f7c763b Mon Sep 17 00:00:00 2001 From: popaprozac Date: Mon, 24 Feb 2025 22:00:58 -0800 Subject: [PATCH] update example --- .../v3/pkg/services/notifications/models.js | 3 +- .../v3/pkg/services/notifications/service.js | 53 +++++++++++-------- v3/examples/notifications/frontend/index.html | 2 +- v3/examples/notifications/frontend/main.js | 5 +- v3/examples/notifications/main.go | 43 +++++---------- 5 files changed, 47 insertions(+), 59 deletions(-) diff --git a/v3/examples/notifications/frontend/bindings/github.com/wailsapp/wails/v3/pkg/services/notifications/models.js b/v3/examples/notifications/frontend/bindings/github.com/wailsapp/wails/v3/pkg/services/notifications/models.js index 958e7d79c..c52cbf1c2 100644 --- a/v3/examples/notifications/frontend/bindings/github.com/wailsapp/wails/v3/pkg/services/notifications/models.js +++ b/v3/examples/notifications/frontend/bindings/github.com/wailsapp/wails/v3/pkg/services/notifications/models.js @@ -11,8 +11,7 @@ import {Create as $Create} from "@wailsio/runtime"; * @typedef {Object} NotificationAction * @property {string} [id] * @property {string} [title] - * @property {boolean} [destructive] - * @property {boolean} [authenticationRequired] + * @property {boolean} [destructive] - macOS only */ /** diff --git a/v3/examples/notifications/frontend/bindings/github.com/wailsapp/wails/v3/pkg/services/notifications/service.js b/v3/examples/notifications/frontend/bindings/github.com/wailsapp/wails/v3/pkg/services/notifications/service.js index 85da85975..ef81ee81a 100644 --- a/v3/examples/notifications/frontend/bindings/github.com/wailsapp/wails/v3/pkg/services/notifications/service.js +++ b/v3/examples/notifications/frontend/bindings/github.com/wailsapp/wails/v3/pkg/services/notifications/service.js @@ -2,6 +2,11 @@ // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // This file is automatically generated. DO NOT EDIT +/** + * Service represents the notifications service + * @module + */ + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore: Unused imports import {Call as $Call, Create as $Create} from "@wailsio/runtime"; @@ -11,8 +16,7 @@ import {Call as $Call, Create as $Create} from "@wailsio/runtime"; import * as $models from "./models.js"; /** - * CheckNotificationAuthorization is a Windows stub that always returns true. - * (user authorization is macOS-specific) + * CheckNotificationAuthorization checks current notification permission status. * @returns {Promise & { cancel(): void }} */ export function CheckNotificationAuthorization() { @@ -20,8 +24,20 @@ export function CheckNotificationAuthorization() { return $resultPromise; } +/** + * OnNotificationResponse registers a callback function that will be called when + * a notification response is received from the user + * @param {any} callback + * @returns {Promise & { cancel(): void }} + */ +export function OnNotificationResponse(callback) { + let $resultPromise = /** @type {any} */($Call.ByID(2829398511, callback)); + return $resultPromise; +} + /** * RegisterNotificationCategory registers a new NotificationCategory to be used with SendNotificationWithActions. + * Registering a category with the same name as a previously registered NotificationCategory will override it. * @param {$models.NotificationCategory} category * @returns {Promise & { cancel(): void }} */ @@ -31,8 +47,7 @@ export function RegisterNotificationCategory(category) { } /** - * RemoveAllDeliveredNotifications is a Windows stub that always returns nil. - * (macOS-specific) + * RemoveAllDeliveredNotifications removes all delivered notifications. * @returns {Promise & { cancel(): void }} */ export function RemoveAllDeliveredNotifications() { @@ -41,8 +56,7 @@ export function RemoveAllDeliveredNotifications() { } /** - * RemoveAllPendingNotifications is a Windows stub that always returns nil. - * (macOS-specific) + * RemoveAllPendingNotifications removes all pending notifications. * @returns {Promise & { cancel(): void }} */ export function RemoveAllPendingNotifications() { @@ -51,18 +65,17 @@ export function RemoveAllPendingNotifications() { } /** - * RemoveDeliveredNotification is a Windows stub that always returns nil. - * (macOS-specific) - * @param {string} $0 + * RemoveDeliveredNotification removes a delivered notification matching the unique identifier. + * @param {string} identifier * @returns {Promise & { cancel(): void }} */ -export function RemoveDeliveredNotification($0) { - let $resultPromise = /** @type {any} */($Call.ByID(149440045, $0)); +export function RemoveDeliveredNotification(identifier) { + let $resultPromise = /** @type {any} */($Call.ByID(149440045, identifier)); return $resultPromise; } /** - * RemoveNotificationCategory removes a previously registered NotificationCategory. + * RemoveNotificationCategory remove a previously registered NotificationCategory. * @param {string} categoryId * @returns {Promise & { cancel(): void }} */ @@ -72,19 +85,17 @@ export function RemoveNotificationCategory(categoryId) { } /** - * RemovePendingNotification is a Windows stub that always returns nil. - * (macOS-specific) - * @param {string} $0 + * RemovePendingNotification removes a pending notification matching the unique identifier. + * @param {string} identifier * @returns {Promise & { cancel(): void }} */ -export function RemovePendingNotification($0) { - let $resultPromise = /** @type {any} */($Call.ByID(3872412470, $0)); +export function RemovePendingNotification(identifier) { + let $resultPromise = /** @type {any} */($Call.ByID(3872412470, identifier)); return $resultPromise; } /** - * RequestUserNotificationAuthorization is a Windows stub that always returns true, nil. - * (user authorization is macOS-specific) + * RequestUserNotificationAuthorization requests permission for notifications. * @returns {Promise & { cancel(): void }} */ export function RequestUserNotificationAuthorization() { @@ -93,8 +104,7 @@ export function RequestUserNotificationAuthorization() { } /** - * SendNotification sends a basic notification with a name, title, and body. All other options are ignored on Windows. - * (subtitle, category id, and data are only available on macOS) + * SendNotification sends a basic notification with a unique identifier, title, subtitle, and body. * @param {$models.NotificationOptions} options * @returns {Promise & { cancel(): void }} */ @@ -107,7 +117,6 @@ export function SendNotification(options) { * SendNotificationWithActions sends a notification with additional actions and inputs. * A NotificationCategory must be registered with RegisterNotificationCategory first. The `CategoryID` must match the registered category. * If a NotificationCategory is not registered a basic notification will be sent. - * (subtitle, category id, and data are only available on macOS) * @param {$models.NotificationOptions} options * @returns {Promise & { cancel(): void }} */ diff --git a/v3/examples/notifications/frontend/index.html b/v3/examples/notifications/frontend/index.html index bde7d3124..3c0d45f85 100644 --- a/v3/examples/notifications/frontend/index.html +++ b/v3/examples/notifications/frontend/index.html @@ -21,7 +21,7 @@ diff --git a/v3/examples/notifications/frontend/main.js b/v3/examples/notifications/frontend/main.js index 79fc8dcca..64624f34b 100644 --- a/v3/examples/notifications/frontend/main.js +++ b/v3/examples/notifications/frontend/main.js @@ -50,7 +50,6 @@ window.sendComplexNotification = async () => { } } -Events.On("notificationResponse", (response) => { - console.log(response) - notificationsElement.innerText += JSON.stringify(response.data[0].data); +Events.On("notification:response", (response) => { + notificationsElement.innerText = JSON.stringify(response.data[0]); }); \ No newline at end of file diff --git a/v3/examples/notifications/main.go b/v3/examples/notifications/main.go index dc9fec9c9..a1d34dd5e 100644 --- a/v3/examples/notifications/main.go +++ b/v3/examples/notifications/main.go @@ -12,29 +12,17 @@ import ( "github.com/wailsapp/wails/v3/pkg/services/notifications" ) -// Wails uses Go's `embed` package to embed the frontend files into the binary. -// Any files in the frontend/dist folder will be embedded into the binary and -// made available to the frontend. -// See https://pkg.go.dev/embed for more information. - //go:embed all:frontend/dist var assets embed.FS -// main function serves as the application's entry point. It initializes the application, creates a window, -// and starts a goroutine that emits a time-based event every second. It subsequently runs the application and -// logs any error that might occur. func main() { - NotificationService := notifications.New() - // Create a new Wails application by providing the necessary options. - // Variables 'Name' and 'Description' are for application metadata. - // 'Assets' configures the asset server with the 'FS' variable pointing to the frontend files. - // 'Bind' is a list of Go struct instances. The frontend has access to the methods of these instances. - // 'Mac' options tailor the application when running an macOS. + notificationService := notifications.New() + app := application.New(application.Options{ Name: "notifications_demo", Description: "A demo of using raw HTML & CSS", Services: []application.Service{ - application.NewService(NotificationService), + application.NewService(notificationService), }, Assets: application.AssetOptions{ Handler: application.AssetFileServerFS(assets), @@ -44,11 +32,6 @@ func main() { }, }) - // Create a new window with the necessary options. - // 'Title' is the title of the window. - // 'Mac' options tailor the window when running on macOS. - // 'BackgroundColour' is the background colour of the window. - // 'URL' is the URL that will be loaded into the webview. app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ Title: "Window 1", Mac: application.MacWindow{ @@ -60,19 +43,19 @@ func main() { URL: "/", }) - app.OnEvent("notificationResponse", func(event *application.CustomEvent) { - data, _ := json.Marshal(event.Data) - fmt.Printf("%s\n", string(data)) - }) - go func() { - granted, err := NotificationService.RequestUserNotificationAuthorization() + granted, err := notificationService.RequestUserNotificationAuthorization() if err != nil { log.Default().Printf("WARNING: %s\n", err) return } if granted { + notificationService.OnNotificationResponse(func(response notifications.NotificationResponse) { + data, _ := json.Marshal(response) + fmt.Printf("%s\n", string(data)) + app.EmitEvent("notification:response", response) + }) time.Sleep(time.Second * 2) var uuid1 string = "Wails Notification Demo" @@ -80,7 +63,7 @@ func main() { uuid1 = "uuid1" } - NotificationService.SendNotification(notifications.NotificationOptions{ + notificationService.SendNotification(notifications.NotificationOptions{ ID: uuid1, Title: "Title!", Body: "Body!", @@ -98,7 +81,7 @@ func main() { uuid2 = "uuid2" } - NotificationService.RegisterNotificationCategory(notifications.NotificationCategory{ + notificationService.RegisterNotificationCategory(notifications.NotificationCategory{ ID: "BACKEND_NOTIF", Actions: []notifications.NotificationAction{ {ID: "VIEW_ACTION", Title: "View"}, @@ -110,7 +93,7 @@ func main() { ReplyPlaceholder: "Reply to backend...", }) - NotificationService.SendNotificationWithActions(notifications.NotificationOptions{ + notificationService.SendNotificationWithActions(notifications.NotificationOptions{ ID: uuid2, Title: "Complex Backend Notification", Subtitle: "Should not show on Windows", @@ -125,10 +108,8 @@ func main() { } }() - // Run the application. This blocks until the application has been exited. err := app.Run() - // If an error occurred while running the application, log it and exit. if err != nil { log.Fatal(err) }