Browse Source

Add notification center delegate, some clean-ups.

master
Greg 1 year ago
parent
commit
859438b1ff
  1. 3
      example/hello.go
  2. 31
      macos/notify_macos.go
  3. 5
      niotify_macos.go

3
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()

31
macos/notify_macos.go

@ -11,8 +11,30 @@ package macos
@import Foundation;
@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;
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);

5
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

Loading…
Cancel
Save