gosx-notifier is a Go framework for sending desktop notifications to OSX 10.8 or higher

gosx-notifier

A Go lib for sending desktop notifications to OSX Mountain Lion's (10.8 or higher REQUIRED) Notification Center.

GoDoc

Update 4/3/2014

On OSX 10.9 and above gosx-notifier now supports images and icons. Now with custom icon support

Synopsis

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 an osx app that allows terminal access to the sandboxed API. gosx-notifier embeds this app 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!

Dependencies:

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.

Installation and Requirements

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

Using the Command Line

notify "Wow! A notification!!!"

useful for knowing when long running commands finish

longRunningCommand && notify done!

Using the Code

It's a pretty straightforward API:

package main

import (
    "github.com/deckarep/gosx-notifier"
    "log"
)

func main() {
    //At a minimum specifiy a message to display to end-user.
    note := gosxnotifier.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 = gosxnotifier.Basso

    //Optionally, set a group which ensures only one notification is ever shown replacing previous notification of same group id.
    note.Group = "com.unique.yourapp.identifier"

    //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

    //Optionally, an app icon (10.9+ ONLY)
    note.AppIcon = "gopher.png"

    //Optionally, a content image (10.9+ ONLY)
    note.ContentImage = "gopher.png"

    //Then, push the notification
    err := note.Push()

    //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.Push()
}

//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
		} else {
			if res.StatusCode != 200 {
				ch <- "-" + site
			} else {
				ch <- "+" + site
			}
			res.Body.Close()
		}
		time.Sleep(30 * time.Second)
	}
}

Usage Ideas

  • Monitor your awesome server cluster and push notifications when something goes haywire (we've all been there)
  • Scrape Hacker News looking for articles of certain keywords and push a notification
  • Monitor your stock performance, push a notification, before you lose all your money
  • Hook it up to ifttt.com and push a notification when your motion-sensor at home goes off

Coming Soon

  • Remove ID

Licence

This project is dual licensed under any licensing defined by the underlying apps and MIT licensed for this version written in Go.

Bitdeli Badge

Owner
Ralph Caraveo III
- Love to solve challenging problems with code - Weapons of choice Go, Python, C# - Currently learning Rust
Ralph Caraveo III
Comments
  • gosx-notifier and portability?

    gosx-notifier and portability?

    I'm relatively new to Go, so I'm not sure if this is done or if it's doable.

    I'd like to include gosx-notifier in a project but I don't want to break cross platform support.

    I do something similar with the same library in a Ruby project I've been working on. There I'm able to see if terminal-notifier exists, and if it does, I use it. If not, I carry on like nothing was ever missing.

    It would be awesome to just have notifications work if I'm installed on OSX and then to have them just not show up if I'm on Ubuntu or Windows.

  • sample app: mem leak

    sample app: mem leak

    There's a memory leak the sample app in the README. In the pinger func, defer res.Body.Close() doesn't execute because the function doesn't return because of the loop. I would just close it before sleeping. Not pretty, but just a sample app.

    Thanks for the package. Simple API, stable, and extremely useful.

  • Bugfix: Readme example sigsegv

    Bugfix: Readme example sigsegv

    When the requested server is completely non-responsive, and err!=nil, res will be nil. The references to StatusCode and Body.Close() will cause a SIGSEGV. If err!=nil, don’t reference res.

    12:21:45:~/gowork/src/play/apistatus$ ./apistatus
    panic: runtime error: invalid memory address or nil pointer dereference
    [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x2357]
    
    goroutine 20 [running]:
    panic(0x2150e0, 0xc42000c0e0)
    	/usr/local/go/src/runtime/panic.go:500 +0x1a1
    main.pinger(0xc420074ae0, 0x25f58e, 0x26)
    	/Users/adam/gowork/src/play/apistatus/apistatus.go:51 +0x57
    created by main.main
    	/Users/adam/gowork/src/play/apistatus/apistatus.go:19 +0x99
    
  • updating README to contain a simple, working example

    updating README to contain a simple, working example

    The current README code is written as if it's internal to the package, not referencing the package that it lives in when using package functions/constants.

  • Embed the terminal-notifier so it's statically linked to the app and deploy-able to other environments.

    Embed the terminal-notifier so it's statically linked to the app and deploy-able to other environments.

    Currently, the terminal-notifier is found by being in the GOPATH src directory of the project. If you deploy a binary using this package, the terminal-notifier dependency will also need to be moved over and setup to be found. This can lead to trouble and confusion. The solution is to embed the terminal-notifier statically into the package such that it can be utilized in any environment.

  • Update terminal-notifier to 2.0.0

    Update terminal-notifier to 2.0.0

    Under the new OS X system, it seems that the old version of the terminal-notifier does not support the Link field. I have seen other people feedback similar questions. #13 I updated the terminal-notifier in the code to their latest version 2.0.0. The binary file use zip and gz compressed. The local test is ok.

  • add

    add "execute" option

    I'd like to use this to automatically launch regular [zoom] meetings that I have when I click it. Sadly, the external protocol in the link zoommtg:// doesn't work properly, so I'd have to rely on using open. Having note.Execute would be very handy for this.

A Go (golang) Custom Flutter Engine Embedder for desktop
A Go (golang) Custom Flutter Engine Embedder for desktop

Go Flutter desktop embedder ⚠️ Warning: this project has been moved to its own organization. Please take a look at its new location: github.com/go-flu

Jul 23, 2022
Create desktop apps using Go and Web Technologies.
Create desktop apps using Go and Web Technologies.

Build desktop applications using Go & Web Technologies. The traditional method of providing web interfaces to Go programs is via a built-in web server

Dec 29, 2022
A full desktop environment for Linux/Unix using Fyne
A full desktop environment for Linux/Unix using Fyne

About FyneDesk is an easy to use Linux/Unix desktop environment following material design. It is build using the Fyne toolkit and is designed to be ea

Dec 31, 2022
An example desktop system tray application that can launch HTML5 windows. Go source with a build process for Windows, Mac and Linux.

ExampleTrayGUI An example cross-platform (Mac, Windows, Linux) system tray application that can launch HTML5 windows, developed in Go including functi

Dec 3, 2022
An example desktop system tray application that can launch HTML5 windows. Go source with a build process for Windows, Mac and Linux.

ExampleTrayGUI An example cross-platform (Mac, Windows, Linux) system tray application that can launch HTML5 windows, developed in Go including functi

Dec 3, 2022
An example desktop system tray application that can launch HTML5 windows. Go source with a build process for Windows, Mac and Linux.

An example cross-platform (Mac, Windows, Linux) system tray application that can launch HTML5 windows, developed in Go including functional build process. This repository is intended as a quick reference to help others start similar projects using the referenced libraries and will not be actively maintained.

Dec 3, 2022
A unified graphical user experience toolkit for Go desktop applications

Unison A unified graphical user experience toolkit for Go desktop applications. macOS, Windows, and Linux are supported. Required setup Unison is buil

Dec 20, 2022
An example desktop system tray application that can launch HTML5 windows. Go source with a build process for Windows, Mac and Linux.

ExampleTrayGUI An example cross-platform (Mac, Windows, Linux) system tray application that can launch HTML5 windows, developed in Go including functi

Dec 3, 2022
Example Go desktop app using HTML

webview-example This repo contains an example/starter for building a webview app

Jun 14, 2022
Ps4gdb desktop - PS4GDB consists of two components

PS4GDB PS4GDB consists of two components. The first component is the gdbstub run

Oct 4, 2022
Native Go (golang) Graphical Interface system (2D and 3D), built on GoKi tree framework
Native Go (golang) Graphical Interface system (2D and 3D), built on GoKi tree framework

GoGi is part of the GoKi Go language (golang) full strength tree structure system (ki = 木 = tree in Japanese) package gi is a scenegraph-based 2D and

Dec 25, 2022
Windows GUI framework for Go.

gform is an easy to use Windows GUI toolkit for Go It provides two approaches to create UI. 1. Pure code. gform.Init() mainWindow := gform.NewForm(ni

Jan 1, 2023
Cross platform rapid GUI framework for golang based on Dear ImGui.
Cross platform rapid GUI framework for golang based on Dear ImGui.

giu Cross platform rapid GUI framework for golang based on Dear ImGui and the great golang binding imgui-go. Any contribution (features, widgets, tuto

Dec 28, 2022
domui: DOM UI framework for Go

domui: DOM UI framework for Go

Jul 23, 2022
Pglet - Web UI framework for backend developers
Pglet - Web UI framework for backend developers

Pglet - Web UI framework for backend developers

Nov 15, 2022
Cross-Platform GUI Framework for Go

⚠️ I'm currently working on this project as part of my master's thesis at the Berlin University of Applied Sciences and Technology. It is under active

Oct 31, 2022
cross-platform library for sending desktop notifications

Golang-Toast cross-platform library for sending desktop notifications Installation go get

Nov 24, 2022
May 11, 2023
⚡ HTTP/2 Apple Push Notification Service (APNs) push provider for Go — Send push notifications to iOS, tvOS, Safari and OSX apps, using the APNs HTTP/2 protocol.

APNS/2 APNS/2 is a go package designed for simple, flexible and fast Apple Push Notifications on iOS, OSX and Safari using the new HTTP/2 Push provide

Jan 1, 2023
ntfy is a super simple pub-sub notification service. It allows you to send desktop notifications via scripts.

ntfy ntfy (pronounce: notify) is a super simple pub-sub notification service. It allows you to send desktop and (soon) phone notifications via scripts

Jan 9, 2023