forked from keybase/go-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier_darwin.go
61 lines (51 loc) · 1.78 KB
/
notifier_darwin.go
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
// Copyright 2016 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package notifier
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa -sectcreate __TEXT __info_plist Info.plist
#import <Cocoa/Cocoa.h>
extern CFStringRef deliverNotification(CFStringRef title, CFStringRef subtitle, CFStringRef message, CFStringRef appIconURLString, CFArrayRef actions, CFStringRef groupID, CFStringRef bundleID, NSTimeInterval timeout, bool debug);
*/
import "C"
import "fmt"
type darwinNotifier struct{}
// NewNotifier constructs notifier for Windows
func NewNotifier() (Notifier, error) {
return &darwinNotifier{}, nil
}
// DeliverNotification sends a notification
func (n darwinNotifier) DeliverNotification(notification Notification) error {
titleRef, err := StringToCFString(notification.Title)
if err != nil {
return err
}
defer Release(C.CFTypeRef(titleRef))
messageRef, err := StringToCFString(notification.Message)
if err != nil {
return err
}
defer Release(C.CFTypeRef(messageRef))
var bundleIDRef C.CFStringRef
if notification.BundleID != "" {
bundleIDRef, err = StringToCFString(notification.BundleID)
if err != nil {
return err
}
defer Release(C.CFTypeRef(bundleIDRef))
}
var appIconURLStringRef C.CFStringRef
if notification.ImagePath != "" {
appIconURLString := fmt.Sprintf("file://%s", notification.ImagePath)
appIconURLStringRef, err = StringToCFString(appIconURLString)
if err != nil {
return err
}
defer Release(C.CFTypeRef(appIconURLStringRef))
}
actionsRef := StringsToCFArray(notification.Actions)
defer Release(C.CFTypeRef(actionsRef))
C.deliverNotification(titleRef, nil, messageRef, appIconURLStringRef, actionsRef,
bundleIDRef, nil, C.NSTimeInterval(notification.Timeout), false)
return nil
}