Skip to content

Commit

Permalink
feat: allow immediate MacOS notifications (#16060)
Browse files Browse the repository at this point in the history
* feat: allow immediate MacOS notifications

* fix args->GetNext

* update docs/api/system-preferences.md

Co-Authored-By: codebytere <codebytere@github.com>

* address feedback from @ckerr's review
  • Loading branch information
codebytere committed Dec 14, 2018
1 parent c7aa747 commit 280f9bf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
6 changes: 2 additions & 4 deletions atom/browser/api/atom_api_system_preferences.h
Expand Up @@ -68,7 +68,8 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>
base::Callback<void(const std::string&, const base::DictionaryValue&)>;

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);
Expand Down Expand Up @@ -113,9 +114,6 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>
~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);
Expand Down
50 changes: 21 additions & 29 deletions atom/browser/api/atom_api_system_preferences_mac.mm
Expand Up @@ -106,10 +106,18 @@ AVMediaType ParseMediaType(const std::string& media_type) {

} // 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(
Expand All @@ -126,7 +134,10 @@ AVMediaType ParseMediaType(const std::string& media_type) {
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(
Expand All @@ -142,7 +153,11 @@ AVMediaType ParseMediaType(const std::string& media_type) {
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(
Expand All @@ -156,29 +171,6 @@ AVMediaType ParseMediaType(const std::string& media_type) {
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,
Expand Down
5 changes: 3 additions & 2 deletions docs/api/system-preferences.md
Expand Up @@ -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.
Expand Down Expand Up @@ -342,4 +343,4 @@ Returns `Promise<Boolean>` - 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.
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.

0 comments on commit 280f9bf

Please sign in to comment.