Add notification center delegate, some clean-ups.

This commit is contained in:
Greg 2020-07-03 16:34:29 -04:00
parent 4369b7f8c6
commit 859438b1ff
3 changed files with 31 additions and 8 deletions

View File

@ -20,9 +20,6 @@ import (
"gioui.org/font/gofont" "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() { func main() {
go func() { go func() {
w := app.NewWindow() w := app.NewWindow()

View File

@ -11,8 +11,30 @@ package macos
@import Foundation; @import Foundation;
@import UserNotifications; @import UserNotifications;
@interface UNDelegate : NSObject <UNUserNotificationCenterDelegate>
{ }
- (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; UNUserNotificationCenter *nc;
BOOL enabled; BOOL enabled;
UNDelegate *del;
void setup() { void setup() {
@autoreleasepool { @autoreleasepool {
@ -26,8 +48,10 @@ void setup() {
NSLog(@"Bundle ID: %@", main.bundleIdentifier); NSLog(@"Bundle ID: %@", main.bundleIdentifier);
NSLog(@"Getting notification center"); NSLog(@"Getting notification center");
nc = [UNUserNotificationCenter currentNotificationCenter]; nc = [UNUserNotificationCenter currentNotificationCenter];
del = [[UNDelegate alloc] init];
nc.delegate = del;
NSLog(@"Requesting authorization"); 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(@"Granted = %s", granted?"true":"false");
NSLog(@"Error = %@", error); NSLog(@"Error = %@", error);
enabled = granted; enabled = granted;
@ -46,11 +70,12 @@ notify(char *id, char *title, char *content) {
UNMutableNotificationContent *note = [[UNMutableNotificationContent alloc] init]; UNMutableNotificationContent *note = [[UNMutableNotificationContent alloc] init];
note.title = [[NSString alloc] initWithUTF8String: title]; note.title = [[NSString alloc] initWithUTF8String: title];
note.body = [[NSString alloc] initWithUTF8String: content]; note.body = [[NSString alloc] initWithUTF8String: content];
NSString *identifier = [[NSString alloc] initWithUTF8String: id]; NSString *identifier = [[NSUUID UUID] UUIDString];
NSLog(@"Creating request"); NSLog(@"Creating request");
UNNotificationRequest *req = [UNNotificationRequest requestWithIdentifier:identifier content: note trigger:nil]; 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"); NSLog(@"Adding notification request");
[nc addNotificationRequest:req withCompletionHandler: ^(NSError *error) { [nc addNotificationRequest:req withCompletionHandler: ^(NSError *error) {
NSLog(@"added notification. Error: %@", error); NSLog(@"added notification. Error: %@", error);

View File

@ -1,3 +1,5 @@
//+build macos
package niotify package niotify
import ( import (
@ -10,14 +12,13 @@ type macosManager struct {
func newManager() (Manager, error) { func newManager() (Manager, error) {
c := macos.NewNotificationChannel("Gio App") c := macos.NewNotificationChannel("Gio App")
return Manager{ return Manager{
&macosManager{ channel: c }, &macosManager{ channel: c },
}, nil }, nil
} }
func (a *macosManager) CreateNotification(title, text string) (*Notification, error) { func (a *macosManager) CreateNotification(title, text string) (*Notification, error) {
notification, err := a.channel.Send(title, text) notification, err := a.channel.Send(title, text)
if err != nil { if err != nil {
return nil, err return nil, err