From 859438b1ff461f8a65913b3a3c863eac2ba521a5 Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 3 Jul 2020 16:34:29 -0400 Subject: [PATCH] Add notification center delegate, some clean-ups. --- example/hello.go | 3 --- macos/notify_macos.go | 31 ++++++++++++++++++++++++++++--- niotify_macos.go | 5 +++-- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/example/hello.go b/example/hello.go index 12fa7c3..1071403 100644 --- a/example/hello.go +++ b/example/hello.go @@ -20,9 +20,6 @@ import ( "gioui.org/font/gofont" ) -//go:generate javac -target 1.8 -source 1.8 -bootclasspath $ANDROID_HOME/platforms/android-29/android.jar ../android/NotificationHelper.java -//go:generate jar cf NotificationHelper.jar ../android/NotificationHelper.class - func main() { go func() { w := app.NewWindow() diff --git a/macos/notify_macos.go b/macos/notify_macos.go index 973f5b1..704605f 100644 --- a/macos/notify_macos.go +++ b/macos/notify_macos.go @@ -11,8 +11,30 @@ package macos @import Foundation; @import UserNotifications; +@interface UNDelegate : NSObject +{ } +- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler; +- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler; +- (void)userNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification; +@end + +@implementation UNDelegate +- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { + NSLog(@"didReceiveNotificationResponse"); +} + +- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { + NSLog(@"willPresentNotification"); +} + +- (void)userNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification { + NSLog(@"openSettingsForNotification"); +} +@end + UNUserNotificationCenter *nc; BOOL enabled; +UNDelegate *del; void setup() { @autoreleasepool { @@ -26,8 +48,10 @@ void setup() { NSLog(@"Bundle ID: %@", main.bundleIdentifier); NSLog(@"Getting notification center"); nc = [UNUserNotificationCenter currentNotificationCenter]; + del = [[UNDelegate alloc] init]; + nc.delegate = del; NSLog(@"Requesting authorization"); - [nc requestAuthorizationWithOptions: UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler: ^(BOOL granted, NSError *error){ + [nc requestAuthorizationWithOptions: UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCriticalAlert completionHandler: ^(BOOL granted, NSError *error){ NSLog(@"Granted = %s", granted?"true":"false"); NSLog(@"Error = %@", error); enabled = granted; @@ -46,11 +70,12 @@ notify(char *id, char *title, char *content) { UNMutableNotificationContent *note = [[UNMutableNotificationContent alloc] init]; note.title = [[NSString alloc] initWithUTF8String: title]; note.body = [[NSString alloc] initWithUTF8String: content]; - NSString *identifier = [[NSString alloc] initWithUTF8String: id]; + NSString *identifier = [[NSUUID UUID] UUIDString]; NSLog(@"Creating request"); UNNotificationRequest *req = [UNNotificationRequest requestWithIdentifier:identifier content: note trigger:nil]; - ret = req.identifier; // FIXME: need to call retain? + ret = req.identifier; + [ret retain]; NSLog(@"Adding notification request"); [nc addNotificationRequest:req withCompletionHandler: ^(NSError *error) { NSLog(@"added notification. Error: %@", error); diff --git a/niotify_macos.go b/niotify_macos.go index f5a1dd0..48b2260 100644 --- a/niotify_macos.go +++ b/niotify_macos.go @@ -1,3 +1,5 @@ +//+build macos + package niotify import ( @@ -10,14 +12,13 @@ type macosManager struct { func newManager() (Manager, error) { c := macos.NewNotificationChannel("Gio App") - + return Manager{ &macosManager{ channel: c }, }, nil } func (a *macosManager) CreateNotification(title, text string) (*Notification, error) { - notification, err := a.channel.Send(title, text) if err != nil { return nil, err