Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow immediate MacOS notifications #16060

Merged
merged 4 commits into from Dec 14, 2018
Merged
Changes from 3 commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -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);
@@ -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);
This conversation was marked as resolved by codebytere

This comment has been minimized.

Copy link
@nornagon

nornagon Dec 14, 2018

Contributor

can we delete the NotificationCenterKind enum now?

This comment has been minimized.

Copy link
@codebytere

codebytere Dec 14, 2018

Author Member

no, we need it for DoSubscribeNotification and DoUnsubscribeNotification

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 @@ 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(
@@ -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(
@@ -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)];
}

This conversation was marked as resolved by codebytere

This comment has been minimized.

Copy link
@ckerr

ckerr Dec 14, 2018

Member

This function declaration should be removed from atom_api_system_preferences.h too

int SystemPreferences::DoSubscribeNotification(
const std::string& name,
const NotificationCallback& callback,
@@ -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 regardless of whether or not the subscribing app is active or not. Defaults to `false`. See [NSDistributedNotificationCenter documentation](https://developer.apple.com/documentation/foundation/nsdistributednotificationcenter/1418360-postnotificationname) for details.
This conversation was marked as resolved by codebytere

This comment has been minimized.

Copy link
@ckerr

ckerr Dec 14, 2018

Member

(nit) "regardless of", "whether or not", and "active or not" all say the same thing.

- immediately regardless of whether or not the subscribing app is active or not
+ 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<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.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.