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
|
// 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
#import <Cocoa/Cocoa.h>
extern CFStringRef deliverNotification(CFStringRef title, CFStringRef subtitle, CFStringRef message, CFStringRef appIconURLString, CFArrayRef actions, CFStringRef groupID, CFStringRef bundleID);
*/
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))
}
actions := []C.CFTypeRef{}
for _, action := range notification.Actions {
actionRef, err := StringToCFString(action)
if err != nil {
return err
}
defer Release(C.CFTypeRef(actionRef))
actions = append(actions, C.CFTypeRef(actionRef))
}
actionsRef := ArrayToCFArray(actions)
defer Release(C.CFTypeRef(actionsRef))
C.deliverNotification(titleRef, nil, messageRef, appIconURLStringRef, actionsRef, bundleIDRef, bundleIDRef)
return nil
}
|