changeset 2106:50196f67a129

Mac: Fixed the clicked callback for notifications on Mojave 10.14+ UNUserNotificationCenter.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 16 Jun 2020 04:45:30 +0000
parents 10c22853b479
children b71f46cd58b9
files mac/dw.m
diffstat 1 files changed, 48 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mac/dw.m	Tue Jun 16 02:42:01 2020 +0000
+++ b/mac/dw.m	Tue Jun 16 04:45:30 2020 +0000
@@ -3285,6 +3285,45 @@
 -(void)dealloc { UserData *root = userdata; _remove_userdata(&root, NULL, TRUE); dw_signal_disconnect_by_window(self); [super dealloc]; }
 @end
 
+#ifdef BUILDING_FOR_MOJAVE
+API_AVAILABLE(macos(10.14))
+@interface DWUserNotificationCenterDelegate : NSObject <UNUserNotificationCenterDelegate>
+@end
+
+@implementation DWUserNotificationCenterDelegate
+//Called when a notification is delivered to a foreground app.
+-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(macos(10.14))
+{
+    completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
+}
+//Called to let your app know which action was selected by the user for a given notification.
+-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler API_AVAILABLE(macos(10.14))
+{
+    NSScanner *objScanner = [NSScanner scannerWithString:response.notification.request.identifier];
+    unsigned long long handle;
+    HWND notification;
+
+    // Skip the dw-notification- prefix
+    [objScanner scanString:@"dw-notification-" intoString:nil];
+    [objScanner scanUnsignedLongLong:&handle];
+    notification = DW_UINT_TO_POINTER(handle);
+    
+    if ([response.actionIdentifier isEqualToString:UNNotificationDismissActionIdentifier])
+    {
+        // The user dismissed the notification without taking action.
+        dw_signal_disconnect_by_window(notification);
+    }
+    else if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier])
+    {
+        // The user launched the app.
+        _event_handler(notification, nil, 8);
+        dw_signal_disconnect_by_window(notification);
+    }
+    completionHandler();
+}
+@end
+#endif
+
 /* Subclass for a MDI type
  * This is just a box for display purposes... but it is a
  * unique class so it can be identified when creating windows.
@@ -10829,15 +10868,12 @@
         {
             if([[NSBundle mainBundle] bundleIdentifier] != nil)
             {
-                UNMutableNotificationContent *content = (UNMutableNotificationContent *)notification;
+                UNMutableNotificationContent* content = (UNMutableNotificationContent *)notification;
                 NSString *notid = [NSString stringWithFormat:@"dw-notification-%llu", (unsigned long long)notification];
-                UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:notid
-                content:content trigger:nil];
+                UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:notid content:content trigger:nil];
 
                 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
-                [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
-                        _event_handler(notification, nil, 8);
-                }];
+                [center addNotificationRequest:request withCompletionHandler:nil];
             }
         }
         else
@@ -12140,7 +12176,12 @@
             {
                 [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert|UNAuthorizationOptionSound
                         completionHandler:^(BOOL granted, NSError * _Nullable error) {
-                            if (!granted) {
+                            if (granted)
+                            {
+                                center.delegate = [[DWUserNotificationCenterDelegate alloc] init];
+                            }
+                            else
+                            {
                                 NSLog(@"WARNING: Unable to get notification permission. %@", error.localizedDescription);
                             }
                 }];