Files
electron/shell/browser/notifications/notification.h
Keeley Hammond 2fbd11d978 feat: add Notification.getHistory() for macOS (#50325)
* feat: add `Notification.getHistory()` static method (macOS)

Add `Notification.getHistory()` which returns a `Promise<Notification[]>`
of all delivered notifications still present in Notification Center.

Each returned Notification is a live object connected to the corresponding
delivered notification — interaction events (click, reply, action, close)
will fire on these objects, enabling apps to re-attach event handlers after
a restart.

Key implementation details:
- Queries UNUserNotificationCenter's getDeliveredNotifications API
- Creates live Notification objects with populated id, groupId, title,
  subtitle, and body properties from what macOS provides
- Registers each object with the presenter via Restore() so the
  NotificationCenterDelegate routes events correctly
- Restored notifications use is_restored_ flag to prevent removal from
  Notification Center when the JS object is garbage collected
- Requires code-signed builds (unsigned builds resolve with empty array)

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>

* test: fix typecheck

* fix: avoid dangling presenter pointer in GetHistory callback

* fix: document show() behavior

Notifications returned by getHistory() now set is_restored_ so that Dismiss() skips removal from Notification Center on GC. Calling show() on a restored notification removes the original from NC and posts a new one.

* fix: address code review feedback

* test: fix oxfmt linting

* docs: update docs/api/notification.md

Co-authored-by: Erick Zhao <erick@hotmail.ca>

---------

Co-authored-by: Claude <svc-devxp-claude@slack-corp.com>
Co-authored-by: Erick Zhao <erick@hotmail.ca>
2026-04-16 16:49:10 -07:00

141 lines
4.2 KiB
C++

// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ELECTRON_SHELL_BROWSER_NOTIFICATIONS_NOTIFICATION_H_
#define ELECTRON_SHELL_BROWSER_NOTIFICATIONS_NOTIFICATION_H_
#include <string>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "url/gurl.h"
namespace electron {
extern const bool debug_notifications;
class NotificationDelegate;
class NotificationPresenter;
struct NotificationAction {
std::u16string type;
std::u16string text;
std::vector<std::u16string> items;
NotificationAction();
~NotificationAction();
NotificationAction(const NotificationAction&);
NotificationAction& operator=(const NotificationAction&);
NotificationAction(NotificationAction&&) noexcept;
NotificationAction& operator=(NotificationAction&&) noexcept;
};
struct NotificationOptions {
std::u16string title;
std::u16string subtitle;
std::u16string msg;
std::string tag;
bool silent;
GURL icon_url;
SkBitmap icon;
bool has_reply;
std::u16string timeout_type;
std::u16string reply_placeholder;
std::u16string sound;
std::u16string urgency; // Linux/Windows
std::vector<NotificationAction> actions;
std::u16string close_button_text;
std::u16string toast_xml;
std::string group_id;
std::u16string group_title;
NotificationOptions();
NotificationOptions(const NotificationOptions&);
NotificationOptions& operator=(const NotificationOptions&);
NotificationOptions(NotificationOptions&&);
NotificationOptions& operator=(NotificationOptions&&);
~NotificationOptions();
};
struct NotificationInfo {
std::string id;
std::string title;
std::string subtitle;
std::string body;
std::string group_id;
NotificationInfo();
~NotificationInfo();
NotificationInfo(const NotificationInfo&);
NotificationInfo& operator=(const NotificationInfo&);
NotificationInfo(NotificationInfo&&) noexcept;
NotificationInfo& operator=(NotificationInfo&&) noexcept;
};
class Notification {
public:
virtual ~Notification();
// Shows the notification.
virtual void Show(const NotificationOptions& options) = 0;
// Dismisses the notification. On some platforms this will result in full
// removal and destruction of the notification, but if the initial dismissal
// does not fully get rid of the notification it will be destroyed in Remove.
virtual void Dismiss() = 0;
// Removes the notification if it was not fully removed during dismissal,
// as can happen on some platforms including Windows.
virtual void Remove() {}
// Restores a previously delivered notification for event handling without
// re-showing it. Sets up platform state so interaction events (click, reply,
// etc.) route to this object.
virtual void Restore() {}
// Should be called by derived classes.
void NotificationClicked();
void NotificationDismissed(bool should_destroy = true,
const std::string& close_reason = "");
void NotificationFailed(const std::string& error = "");
// delete this.
void Destroy();
base::WeakPtr<Notification> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
void set_delegate(NotificationDelegate* delegate) { delegate_ = delegate; }
void set_notification_id(const std::string& id) { notification_id_ = id; }
void set_is_dismissed(bool dismissed) { is_dismissed_ = dismissed; }
NotificationDelegate* delegate() const { return delegate_; }
NotificationPresenter* presenter() const { return presenter_; }
const std::string& notification_id() const { return notification_id_; }
bool is_dismissed() const { return is_dismissed_; }
// disable copy
Notification(const Notification&) = delete;
Notification& operator=(const Notification&) = delete;
protected:
Notification(NotificationDelegate* delegate,
NotificationPresenter* presenter);
private:
raw_ptr<NotificationDelegate> delegate_;
raw_ptr<NotificationPresenter> presenter_;
std::string notification_id_;
bool is_dismissed_ = false;
base::WeakPtrFactory<Notification> weak_factory_{this};
};
} // namespace electron
#endif // ELECTRON_SHELL_BROWSER_NOTIFICATIONS_NOTIFICATION_H_