5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-19 02:19:31 +08:00
This commit is contained in:
Zach Botterman 2025-02-26 09:32:32 -08:00
parent db2cab7b7c
commit e216b3e513
3 changed files with 24 additions and 11 deletions

View File

@ -212,6 +212,11 @@ func (ns *Service) RemoveNotification(identifier string) error {
return nil return nil
} }
// SetIcon is a macOS stub. The application icon is used automatically.
func (ns *Service) SetIcon(_ []byte) {
return
}
//export didReceiveNotificationResponse //export didReceiveNotificationResponse
func didReceiveNotificationResponse(jsonPayload *C.char) { func didReceiveNotificationResponse(jsonPayload *C.char) {
payload := C.GoString(jsonPayload) payload := C.GoString(jsonPayload)
@ -221,7 +226,7 @@ func didReceiveNotificationResponse(jsonPayload *C.char) {
return return
} }
if response.ActionIdentifier == "com.apple.UNNotificationDefaultActionIdentifier" { if response.ActionIdentifier == AppleDefaultActionIdentifier {
response.ActionIdentifier = DefaultActionIdentifier response.ActionIdentifier = DefaultActionIdentifier
} }

View File

@ -1,5 +1,7 @@
//go:build linux //go:build linux
// WIP - WILL NOT WORK PROPERLY
package notifications package notifications
import ( import (

View File

@ -4,6 +4,7 @@ package notifications
import ( import (
"context" "context"
_ "embed"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -19,6 +20,7 @@ import (
var NotificationLock sync.RWMutex var NotificationLock sync.RWMutex
var NotificationCategories = make(map[string]NotificationCategory) var NotificationCategories = make(map[string]NotificationCategory)
var Icon []byte
// NotificationPayload combines the action ID and user data into a single structure // NotificationPayload combines the action ID and user data into a single structure
type NotificationPayload struct { type NotificationPayload struct {
@ -101,8 +103,10 @@ func (ns *Service) CheckNotificationAuthorization() bool {
// SendNotification sends a basic notification with a name, title, and body. All other options are ignored on Windows. // SendNotification sends a basic notification with a name, title, and body. All other options are ignored on Windows.
// (subtitle and category id are only available on macOS) // (subtitle and category id are only available on macOS)
func (ns *Service) SendNotification(options NotificationOptions) error { func (ns *Service) SendNotification(options NotificationOptions) error {
if err := saveIconToDir(); err != nil { if len(Icon) > 0 {
fmt.Printf("Error saving icon: %v\n", err) if err := saveIconToDir(); err != nil {
fmt.Printf("Error saving icon: %v\n", err)
}
} }
n := toast.Notification{ n := toast.Notification{
@ -126,8 +130,10 @@ func (ns *Service) SendNotification(options NotificationOptions) error {
// If a NotificationCategory is not registered a basic notification will be sent. // If a NotificationCategory is not registered a basic notification will be sent.
// (subtitle and category id are only available on macOS) // (subtitle and category id are only available on macOS)
func (ns *Service) SendNotificationWithActions(options NotificationOptions) error { func (ns *Service) SendNotificationWithActions(options NotificationOptions) error {
if err := saveIconToDir(); err != nil { if len(Icon) > 0 {
fmt.Printf("Error saving icon: %v\n", err) if err := saveIconToDir(); err != nil {
fmt.Printf("Error saving icon: %v\n", err)
}
} }
NotificationLock.RLock() NotificationLock.RLock()
@ -230,6 +236,11 @@ func (ns *Service) RemoveNotification(identifier string) error {
return nil return nil
} }
// SetIcon sets the notifications icon.
func (ns *Service) SetIcon(icon []byte) {
Icon = icon
}
// encodePayload combines an action ID and user data into a single encoded string // encodePayload combines an action ID and user data into a single encoded string
func encodePayload(actionID string, data map[string]interface{}) (string, error) { func encodePayload(actionID string, data map[string]interface{}) (string, error) {
payload := NotificationPayload{ payload := NotificationPayload{
@ -278,11 +289,6 @@ func parseNotificationResponse(response string) (action string, data string) {
func saveIconToDir() error { func saveIconToDir() error {
options := application.Get().Config() options := application.Get().Config()
appName := options.Name appName := options.Name
icon := options.Icon
if len(icon) == 0 {
return fmt.Errorf("failed to retrieve icon from application")
}
guid, err := getGUID(appName) guid, err := getGUID(appName)
if err != nil { if err != nil {
@ -291,7 +297,7 @@ func saveIconToDir() error {
iconPath := filepath.Join(os.TempDir(), appName+guid+".png") iconPath := filepath.Join(os.TempDir(), appName+guid+".png")
return os.WriteFile(iconPath, icon, 0644) return os.WriteFile(iconPath, Icon, 0644)
} }
func saveCategoriesToRegistry() error { func saveCategoriesToRegistry() error {