A Go lib for sending notifications to OSX Mountain Lion's (10.8 or higher REQUIRED) Notification Center.
OSX Mountain Lion comes packaged with a built-in notification center. For whatever reason, Apple sandboxed the notification center API to apps hosted in its App Store. The end result? A potentially useful API shackled to Apple's ecosystem.
Thankfully, Eloy Durán put together a set of sweet osx apps that allow terminal access to the sandboxed API. gosx-notifier wraps these apps with a simple interface to the closed API.
It's not perfect, and the implementor will quickly notice its limitations. However, it's a start and any pull requests are accepted and encouraged!
node-osx-notifier This version is designed to be a node.js server where you can fire notifications by making REST requests. The Go version is a static API for Go designed to be used within a Go app as needed.
There are none! If you utilize this package and create a binary executable it will auto-magically install the terminal-notifier component into a temp directory of the server. This is possible because in this latest version the terminal-notifier binary is now statically embedded into the Go source files.
The following command will install the notification api for Go along with the binaries. Also, utilizing this lib requires OSX 10.8 or higher. It will simply not work on lower versions of OSX.
go get github.com/deckarep/gosx-notifier
It's a pretty straightforward API:
//At a minimum specifiy a message to display to end-user.
note := NewNotification("Check your Apple Stock!")
//Optionally, set a title
note.Title = "It's money making time 💰"
//Optionally, set a subtitle
note.Subtitle = "My subtitle"
//Optionally, set a sound from a predefined set.
note.Sound = Basso
//Optionally, set a sender (Notification will now use the Safari icon)
note.Sender = "com.apple.Safari"
//Optionally, specifiy a url or bundleid to open should the notification be clicked.
note.Link = "http://www.yahoo.com" //or BundleID like: com.apple.Terminal
//Then, fire off the notification
err := note.SendNotification()
//If necessary, check error
if err != nil {
log.Println("Uh oh!")
}
Sample App: Desktop Pinger Notification - monitors your websites and will notifiy you when a website is down.
package main
import (
"github.com/deckarep/gosx-notifier"
"net/http"
"strings"
"time"
)
//a slice of string sites that you are interested in watching
var sites []string = []string{
"http://www.yahoo.com",
"http://www.google.com",
"http://www.bing.com"}
func main() {
ch := make(chan string)
for _, s := range sites {
go pinger(ch, s)
}
for {
select {
case result := <-ch:
if strings.HasPrefix(result, "-") {
s := strings.Trim(result, "-")
showNotification("Urgent, can't ping website: " + s)
}
}
}
}
func showNotification(message string) {
note := gosxnotifier.NewNotification(message)
note.Title = "Site Down"
note.Sound = gosxnotifier.Default
note.SendNotification()
}
//Prefixing a site with a + means it's up, while - means it's down
func pinger(ch chan string, site string) {
for {
res, err := http.Get(site)
if err != nil {
ch <- "-" + site
}
if res != nil && res.Body != nil {
defer res.Body.Close()
if res.StatusCode != 200 {
ch <- "-" + site
} else {
ch <- "+" + site
}
}
time.Sleep(30 * time.Second)
}
}
- Monitor your awesome server cluster and pop notifications when something goes haywire (we've all been there)
- Scrape Hacker News looking for articles of certain keywords and pop a notification
- Monitor your stock performance, pop a notification, before you lose all your money
- Hook it up to ifttt.com and pop a notification when your motion-sensor at home goes off
- Ping your Redis cluster for good behavior and pop a notification when a developer is storing JSON in Redis strings because they still think Redis is the same as Memcached.
- Group ID
- Remove ID
This project is dual licensed under any licensing defined by the underlying apps and MIT licensed for this version written in Go.