Add notification center delegate, some clean-ups.
This commit is contained in:
+28
-3
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user