-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathnotification.go
More file actions
79 lines (66 loc) · 2.44 KB
/
Copy pathnotification.go
File metadata and controls
79 lines (66 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package menuet
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa -framework UserNotifications
#import <Cocoa/Cocoa.h>
#ifndef __NOTIFICATION_H_H__
#import "notification.h"
#endif
void showNotification(const char *jsonString);
const char *notificationAuthorizationStatus(void);
*/
import "C"
import (
"encoding/json"
"log"
"unsafe"
)
// Notification represents a macOS user notification.
type Notification struct {
// The basic text of the notification
Title string
Subtitle string
Message string
// These add an optional action button, configure dismiss behavior, and add an in-line reply.
// Note: on macOS 11+, CloseButton still causes the dismiss action to trigger the
// NotificationResponder callback, but custom button text is not supported by the
// UserNotifications framework — the system default text is used instead.
ActionButton string
CloseButton string
ResponsePlaceholder string
// Duplicate identifiers do not re-display, but instead update the notification center
Identifier string
// If true, the notification is shown, but then deleted from the notification center
RemoveFromNotificationCenter bool
// Silent posts the notification with no sound. For apps that notify on
// routine background events, a sound per event is noise the user did not
// ask for — the banner is the message.
Silent bool
}
func runningInAppBundle() bool {
_, bundlePath := appPath()
return bundlePath != ""
}
// Notification shows a notification to the user. Note that you have to be part of a proper application bundle for them to show up.
func (a *Application) Notification(notification Notification) {
if !runningInAppBundle() {
log.Printf("Warning: notifications won't show up unless running inside an application bundle")
}
b, err := json.Marshal(notification)
if err != nil {
log.Printf("Marshal: %v", err)
return
}
cstr := C.CString(string(b))
C.showNotification(cstr)
C.free(unsafe.Pointer(cstr))
}
// NotificationAuthorization reports whether this app may actually deliver
// notifications: "authorized", "provisional", "denied", "notDetermined", or
// "unknown" (query timed out). Apps that alert on important events should
// check this and surface a visible warning when denied — Notification()
// silently no-ops in that state, which reads as "everything is fine" exactly
// when it isn't.
func (a *Application) NotificationAuthorization() string {
return C.GoString(C.notificationAuthorizationStatus())
}