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"
)
//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()

View File

@ -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);

View File

@ -1,3 +1,5 @@
//+build macos
package niotify
import (
@ -17,7 +19,6 @@ func newManager() (Manager, error) {
}
func (a *macosManager) CreateNotification(title, text string) (*Notification, error) {
notification, err := a.channel.Send(title, text)
if err != nil {
return nil, err