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:
parent
c7aa747891
commit
280f9bf49c
3 changed files with 26 additions and 35 deletions
|
@ -68,7 +68,8 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>
|
||||||
base::Callback<void(const std::string&, const base::DictionaryValue&)>;
|
base::Callback<void(const std::string&, const base::DictionaryValue&)>;
|
||||||
|
|
||||||
void PostNotification(const std::string& name,
|
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,
|
int SubscribeNotification(const std::string& name,
|
||||||
const NotificationCallback& callback);
|
const NotificationCallback& callback);
|
||||||
void UnsubscribeNotification(int id);
|
void UnsubscribeNotification(int id);
|
||||||
|
@ -113,9 +114,6 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>
|
||||||
~SystemPreferences() override;
|
~SystemPreferences() override;
|
||||||
|
|
||||||
#if defined(OS_MACOSX)
|
#if defined(OS_MACOSX)
|
||||||
void DoPostNotification(const std::string& name,
|
|
||||||
const base::DictionaryValue& user_info,
|
|
||||||
NotificationCenterKind kind);
|
|
||||||
int DoSubscribeNotification(const std::string& name,
|
int DoSubscribeNotification(const std::string& name,
|
||||||
const NotificationCallback& callback,
|
const NotificationCallback& callback,
|
||||||
NotificationCenterKind kind);
|
NotificationCenterKind kind);
|
||||||
|
|
|
@ -106,10 +106,18 @@ std::string ConvertAuthorizationStatus(AVAuthorizationStatusMac status) {
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void SystemPreferences::PostNotification(
|
void SystemPreferences::PostNotification(const std::string& name,
|
||||||
const std::string& name,
|
const base::DictionaryValue& user_info,
|
||||||
const base::DictionaryValue& user_info) {
|
mate::Arguments* args) {
|
||||||
DoPostNotification(name, user_info, kNSDistributedNotificationCenter);
|
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(
|
int SystemPreferences::SubscribeNotification(
|
||||||
|
@ -126,7 +134,10 @@ void SystemPreferences::UnsubscribeNotification(int request_id) {
|
||||||
void SystemPreferences::PostLocalNotification(
|
void SystemPreferences::PostLocalNotification(
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const base::DictionaryValue& user_info) {
|
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(
|
int SystemPreferences::SubscribeLocalNotification(
|
||||||
|
@ -142,7 +153,11 @@ void SystemPreferences::UnsubscribeLocalNotification(int request_id) {
|
||||||
void SystemPreferences::PostWorkspaceNotification(
|
void SystemPreferences::PostWorkspaceNotification(
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const base::DictionaryValue& user_info) {
|
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(
|
int SystemPreferences::SubscribeWorkspaceNotification(
|
||||||
|
@ -156,29 +171,6 @@ void SystemPreferences::UnsubscribeWorkspaceNotification(int request_id) {
|
||||||
DoUnsubscribeNotification(request_id, kNSWorkspaceNotificationCenter);
|
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(
|
int SystemPreferences::DoSubscribeNotification(
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const NotificationCallback& callback,
|
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.
|
Returns `Boolean` - Whether the Swipe between pages setting is on.
|
||||||
|
|
||||||
### `systemPreferences.postNotification(event, userInfo)` _macOS_
|
### `systemPreferences.postNotification(event, userInfo[, deliverImmediately])` _macOS_
|
||||||
|
|
||||||
* `event` String
|
* `event` String
|
||||||
* `userInfo` Object
|
* `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
|
Posts `event` as native notifications of macOS. The `userInfo` is an Object
|
||||||
that contains the user information dictionary sent along with the notification.
|
that contains the user information dictionary sent along with the notification.
|
||||||
|
|
Loading…
Reference in a new issue