aboutsummaryrefslogtreecommitdiffstats
path: root/notifier_darwin.go
blob: b075f1c2a407fa515ee29b7a62f92106bad2215f (plain)
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
// 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, CFStringRef groupID, CFStringRef bundleID, CFStringRef actionButtonTitle, CFStringRef otherButtonTitle);
*/
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))
	}

	C.deliverNotification(titleRef, nil, messageRef, appIconURLStringRef, bundleIDRef, bundleIDRef, nil, nil)

	return nil
}