From 280f9bf49cf6ecaa9492c2984fc0b1ba71e99899 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Fri, 14 Dec 2018 13:46:46 -0800 Subject: [PATCH] feat: allow immediate MacOS notifications (#16060) * feat: allow immediate MacOS notifications * fix args->GetNext * update docs/api/system-preferences.md Co-Authored-By: codebytere * address feedback from @ckerr's review --- .../browser/api/atom_api_system_preferences.h | 6 +-- .../api/atom_api_system_preferences_mac.mm | 50 ++++++++----------- docs/api/system-preferences.md | 5 +- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/atom/browser/api/atom_api_system_preferences.h b/atom/browser/api/atom_api_system_preferences.h index 6cb36cac13..b83d6574da 100644 --- a/atom/browser/api/atom_api_system_preferences.h +++ b/atom/browser/api/atom_api_system_preferences.h @@ -68,7 +68,8 @@ class SystemPreferences : public mate::EventEmitter base::Callback; void PostNotification(const std::string& name, - const base::DictionaryValue& user_info); + const base::DictionaryValue& user_info, + mate::Arguments* args); int SubscribeNotification(const std::string& name, const NotificationCallback& callback); void UnsubscribeNotification(int id); @@ -113,9 +114,6 @@ class SystemPreferences : public mate::EventEmitter ~SystemPreferences() override; #if defined(OS_MACOSX) - void DoPostNotification(const std::string& name, - const base::DictionaryValue& user_info, - NotificationCenterKind kind); int DoSubscribeNotification(const std::string& name, const NotificationCallback& callback, NotificationCenterKind kind); diff --git a/atom/browser/api/atom_api_system_preferences_mac.mm b/atom/browser/api/atom_api_system_preferences_mac.mm index 55c8f6efb5..25dbc61ec6 100644 --- a/atom/browser/api/atom_api_system_preferences_mac.mm +++ b/atom/browser/api/atom_api_system_preferences_mac.mm @@ -106,10 +106,18 @@ std::string ConvertAuthorizationStatus(AVAuthorizationStatusMac status) { } // namespace -void SystemPreferences::PostNotification( - const std::string& name, - const base::DictionaryValue& user_info) { - DoPostNotification(name, user_info, kNSDistributedNotificationCenter); +void SystemPreferences::PostNotification(const std::string& name, + const base::DictionaryValue& user_info, + mate::Arguments* args) { + bool immediate = false; + args->GetNext(&immediate); + + NSDistributedNotificationCenter* center = + [NSDistributedNotificationCenter defaultCenter]; + [center postNotificationName:base::SysUTF8ToNSString(name) + object:nil + userInfo:DictionaryValueToNSDictionary(user_info) + deliverImmediately:immediate]; } int SystemPreferences::SubscribeNotification( @@ -126,7 +134,10 @@ void SystemPreferences::UnsubscribeNotification(int request_id) { void SystemPreferences::PostLocalNotification( const std::string& name, const base::DictionaryValue& user_info) { - DoPostNotification(name, user_info, kNSNotificationCenter); + NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; + [center postNotificationName:base::SysUTF8ToNSString(name) + object:nil + userInfo:DictionaryValueToNSDictionary(user_info)]; } int SystemPreferences::SubscribeLocalNotification( @@ -142,7 +153,11 @@ void SystemPreferences::UnsubscribeLocalNotification(int request_id) { void SystemPreferences::PostWorkspaceNotification( const std::string& name, const base::DictionaryValue& user_info) { - DoPostNotification(name, user_info, kNSWorkspaceNotificationCenter); + NSNotificationCenter* center = + [[NSWorkspace sharedWorkspace] notificationCenter]; + [center postNotificationName:base::SysUTF8ToNSString(name) + object:nil + userInfo:DictionaryValueToNSDictionary(user_info)]; } int SystemPreferences::SubscribeWorkspaceNotification( @@ -156,29 +171,6 @@ void SystemPreferences::UnsubscribeWorkspaceNotification(int request_id) { DoUnsubscribeNotification(request_id, kNSWorkspaceNotificationCenter); } -void SystemPreferences::DoPostNotification( - const std::string& name, - const base::DictionaryValue& user_info, - NotificationCenterKind kind) { - NSNotificationCenter* center; - switch (kind) { - case kNSDistributedNotificationCenter: - center = [NSDistributedNotificationCenter defaultCenter]; - break; - case kNSNotificationCenter: - center = [NSNotificationCenter defaultCenter]; - break; - case kNSWorkspaceNotificationCenter: - center = [[NSWorkspace sharedWorkspace] notificationCenter]; - break; - default: - break; - } - [center postNotificationName:base::SysUTF8ToNSString(name) - object:nil - userInfo:DictionaryValueToNSDictionary(user_info)]; -} - int SystemPreferences::DoSubscribeNotification( const std::string& name, const NotificationCallback& callback, diff --git a/docs/api/system-preferences.md b/docs/api/system-preferences.md index 5cd5864fde..51558ccc4a 100644 --- a/docs/api/system-preferences.md +++ b/docs/api/system-preferences.md @@ -59,10 +59,11 @@ Returns `Boolean` - Whether the system is in Dark Mode. Returns `Boolean` - Whether the Swipe between pages setting is on. -### `systemPreferences.postNotification(event, userInfo)` _macOS_ +### `systemPreferences.postNotification(event, userInfo[, deliverImmediately])` _macOS_ * `event` String * `userInfo` Object +* `deliverImmediately` Boolean (optional) - `true` to post notifications immediately even when the subscribing app is inactive. Posts `event` as native notifications of macOS. The `userInfo` is an Object that contains the user information dictionary sent along with the notification. @@ -342,4 +343,4 @@ Returns `Promise` - A promise that resolves with `true` if consent was **Important:** In order to properly leverage this API, you [must set](https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/requesting_authorization_for_media_capture_on_macos?language=objc) the `NSMicrophoneUsageDescription` and `NSCameraUsageDescription` strings in your app's `Info.plist` file. The values for these keys will be used to populate the permission dialogs so that the user will be properly informed as to the purpose of the permission request. See [Electron Application Distribution](https://electronjs.org/docs/tutorial/application-distribution#macos) for more information about how to set these in the context of Electron. -This user consent was not required until macOS 10.14 Mojave, so this method will always return `true` if your system is running 10.13 High Sierra or lower. \ No newline at end of file +This user consent was not required until macOS 10.14 Mojave, so this method will always return `true` if your system is running 10.13 High Sierra or lower.