feat: allow immediate MacOS notifications (#16060)

* 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
This commit is contained in:
Shelley Vohr 2018-12-14 13:46:46 -08:00 committed by GitHub
parent c7aa747891
commit 280f9bf49c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 35 deletions

View file

@ -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);
@ -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);

View file

@ -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,

View file

@ -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.