9ea938c228a0967c1c1ebfea09a8feb41f2ea3e6 — Chris Waldon 2 years ago 82e69ef notify-new-api
notify: delay and synchronize default notifier creation

The default notifier instance used by the Push function was being
created with an init() function. This worked on most platforms, but
not on Android. To create a notifier on Android requires having a
JVM handle, and that isn't available until after application
startup. To handle this, I've configured the push function to
initialize the default notifier instance on first use, and to
synchronize access to it via a mutex to protect against multiple
goroutines trying to use Push simultaneously.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
1 files changed, 12 insertions(+), 4 deletions(-)

M notify/notification_manager.go
M notify/notification_manager.go => notify/notification_manager.go +12 -4
@@ 9,13 9,13 @@
//     notification.Cancel()
package notify

import "sync"

// impl is a package global notifier initialized to the current platform
// implementation.
var impl Notifier

func init() {
	impl, _ = newNotifier()
var implErr error
var implLock sync.Mutex

// Notifier provides methods for creating and managing notifications.
type Notifier interface {

@@ 49,5 49,13 @@ func (noop) Cancel() error {

// Push a notification to the OS.
func Push(title, text string) (Notification, error) {
    defer implLock.Unlock()
    if impl == nil && implErr == nil {
            impl, implErr = newNotifier()
    if implErr != nil {
        return nil, implErr
	return impl.CreateNotification(title, text)