From c1463f9799f99ffc4aeebd9ca6ee173e3c2737f6 Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Fri, 29 Mar 2013 08:59:21 -0400 Subject: [PATCH] Don't fire the `show` event until the notification is actually shown --- .../browser/notification_presenter_mac.mm | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/brightray/browser/notification_presenter_mac.mm b/brightray/browser/notification_presenter_mac.mm index a8e07fbfeaf1..a52da6342035 100644 --- a/brightray/browser/notification_presenter_mac.mm +++ b/brightray/browser/notification_presenter_mac.mm @@ -13,6 +13,16 @@ namespace brightray { namespace { +struct NotificationID { + int render_process_id; + int render_view_id; + int notification_id; +}; + +NSString * const kRenderProcessIDKey = @"RenderProcessID"; +NSString * const kRenderViewIDKey = @"RenderViewID"; +NSString * const kNotificationIDKey = @"NotificationID"; + scoped_nsobject CreateUserNotification( const content::ShowDesktopNotificationHostMsgParams& params, int render_process_id, @@ -20,9 +30,23 @@ scoped_nsobject CreateUserNotification( auto notification = [[NSUserNotification alloc] init]; notification.title = base::SysUTF16ToNSString(params.title); notification.informativeText = base::SysUTF16ToNSString(params.body); + notification.userInfo = @{ + kRenderProcessIDKey: @(render_process_id), + kRenderViewIDKey: @(render_view_id), + kNotificationIDKey: @(params.notification_id), + }; + return scoped_nsobject(notification); } +NotificationID GetID(NSUserNotification* notification) { + NotificationID ID; + ID.render_process_id = [[notification.userInfo objectForKey:kRenderProcessIDKey] intValue]; + ID.render_view_id = [[notification.userInfo objectForKey:kRenderViewIDKey] intValue]; + ID.notification_id = [[notification.userInfo objectForKey:kNotificationIDKey] intValue]; + return ID; +} + } NotificationPresenter* NotificationPresenter::Create() { @@ -43,18 +67,22 @@ void NotificationPresenterMac::ShowNotification( int render_view_id) { auto notification = CreateUserNotification(params, render_process_id, render_view_id); [NSUserNotificationCenter.defaultUserNotificationCenter deliverNotification:notification]; - - auto host = content::RenderViewHost::FromID(render_process_id, render_view_id); - if (!host) - return; - - host->DesktopNotificationPostDisplay(params.notification_id); } } @implementation BRYUserNotificationCenterDelegate +- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification { + auto ID = brightray::GetID(notification); + + auto host = content::RenderViewHost::FromID(ID.render_process_id, ID.render_view_id); + if (!host) + return; + + host->DesktopNotificationPostDisplay(ID.notification_id); +} + - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification { // Display notifications even if the app is active. return YES;