Use NotificationPresenter - macOS
This commit is contained in:
parent
193c561815
commit
058bdfbced
17 changed files with 93 additions and 80 deletions
|
@ -7,89 +7,48 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
#include "atom/browser/browser.h"
|
||||
#include "atom/browser/ui/notification_delegate_adapter.h"
|
||||
#include "base/mac/mac_util.h"
|
||||
#include "base/mac/scoped_nsobject.h"
|
||||
#include "base/strings/sys_string_conversions.h"
|
||||
|
||||
std::map<int, base::scoped_nsobject<NSUserNotification>> native_notifications_;
|
||||
|
||||
@interface AtomNotificationCenter : NSObject<NSUserNotificationCenterDelegate> {
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation AtomNotificationCenter
|
||||
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
|
||||
shouldPresentNotification:(NSUserNotification *)notification {
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)userNotificationCenter:(NSUserNotificationCenter *)center
|
||||
didActivateNotification:(NSUserNotification *)notification {
|
||||
int n_id = [[notification.userInfo objectForKey:@"id"] intValue];
|
||||
if (atom::api::Notification::HasID(n_id)) {
|
||||
auto atomNotification = atom::api::Notification::FromID(n_id);
|
||||
|
||||
if (notification.activationType == NSUserNotificationActivationTypeReplied){
|
||||
atomNotification->OnReplied([notification.response.string UTF8String]);
|
||||
} else {
|
||||
atomNotification->OnClicked();
|
||||
}
|
||||
}
|
||||
}
|
||||
@end
|
||||
#include "brightray/browser/notification.h"
|
||||
#include "brightray/browser/notification_presenter.h"
|
||||
#include "brightray/browser/mac/notification_presenter_mac.h"
|
||||
#include "third_party/skia/include/core/SkBitmap.h"
|
||||
#include "ui/gfx/codec/png_codec.h"
|
||||
#include "url/gurl.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
AtomNotificationCenter* del = [[AtomNotificationCenter alloc] init];
|
||||
bool set_del_ = false;
|
||||
brightray::NotificationPresenterMac* presenter;
|
||||
|
||||
void Notification::Show() {
|
||||
base::scoped_nsobject<NSUserNotification> notification_ = native_notifications_[id_];
|
||||
[NSUserNotificationCenter.defaultUserNotificationCenter
|
||||
deliverNotification:notification_];
|
||||
OnShown();
|
||||
}
|
||||
|
||||
void Notification::OnInitialProps() {
|
||||
if (!set_del_) {
|
||||
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:del];
|
||||
set_del_ = true;
|
||||
SkBitmap image = *(new SkBitmap);
|
||||
if (has_icon_) {
|
||||
image = *(icon_.ToSkBitmap());
|
||||
}
|
||||
|
||||
base::scoped_nsobject<NSUserNotification> notification_;
|
||||
notification_.reset([[NSUserNotification alloc] init]);
|
||||
std::unique_ptr<AtomNotificationDelegateAdapter> adapter(
|
||||
new AtomNotificationDelegateAdapter(this));
|
||||
auto notif = presenter->CreateNotification(adapter.get());
|
||||
if (notif) {
|
||||
ignore_result(adapter.release()); // it will release itself automatically.
|
||||
GURL nullUrl = *(new GURL);
|
||||
notif->Show(title_, body_, "", nullUrl, image, silent_, has_reply_, reply_placeholder_);
|
||||
}
|
||||
}
|
||||
|
||||
native_notifications_[id_] = notification_;
|
||||
|
||||
NotifyPropsUpdated();
|
||||
bool initialized_ = false;
|
||||
void Notification::OnInitialProps() {
|
||||
if (!initialized_) {
|
||||
presenter = new brightray::NotificationPresenterMac;
|
||||
initialized_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Notification::NotifyPropsUpdated() {
|
||||
base::scoped_nsobject<NSUserNotification> notification_ = native_notifications_[id_];
|
||||
|
||||
[notification_ setTitle:base::SysUTF16ToNSString(title_)];
|
||||
[notification_ setInformativeText:base::SysUTF16ToNSString(body_)];
|
||||
|
||||
NSDictionary * userInfo = [NSMutableDictionary dictionary];
|
||||
[userInfo setValue:[NSNumber numberWithInt:id_] forKey:@"id"];
|
||||
[notification_ setUserInfo:userInfo];
|
||||
|
||||
if ([notification_ respondsToSelector:@selector(setContentImage:)] && has_icon_) {
|
||||
[notification_ setContentImage:icon_.AsNSImage()];
|
||||
}
|
||||
|
||||
if (has_reply_) {
|
||||
[notification_ setResponsePlaceholder:base::SysUTF16ToNSString(reply_placeholder_)];
|
||||
[notification_ setHasReplyButton:true];
|
||||
}
|
||||
|
||||
if (silent_) {
|
||||
[notification_ setSoundName:nil];
|
||||
} else {
|
||||
[notification_ setSoundName:NSUserNotificationDefaultSoundName];
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
|
|
@ -20,6 +20,9 @@ void AtomNotificationDelegateAdapter::NotificationClosed() {}
|
|||
void AtomNotificationDelegateAdapter::NotificationClick() {
|
||||
observer_->OnClicked();
|
||||
}
|
||||
void AtomNotificationDelegateAdapter::NotificationReplied(std::string reply) {
|
||||
observer_->OnReplied(reply);
|
||||
}
|
||||
void AtomNotificationDelegateAdapter::NotificationDestroyed() {}
|
||||
void AtomNotificationDelegateAdapter::NotificationFailed() {}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ class AtomNotificationDelegateAdapter : public brightray::NotificationDelegate {
|
|||
void NotificationClick();
|
||||
void NotificationDestroyed();
|
||||
void NotificationFailed();
|
||||
void NotificationReplied(std::string reply);
|
||||
};
|
||||
|
||||
} // namespace atom
|
||||
|
|
|
@ -88,7 +88,9 @@ void LibnotifyNotification::Show(const base::string16& title,
|
|||
const std::string& tag,
|
||||
const GURL& icon_url,
|
||||
const SkBitmap& icon,
|
||||
const bool silent) {
|
||||
const bool silent,
|
||||
const bool has_reply,
|
||||
const base::string16 reply_placeholder) {
|
||||
notification_ = libnotify_loader_.notify_notification_new(
|
||||
base::UTF16ToUTF8(title).c_str(),
|
||||
base::UTF16ToUTF8(body).c_str(),
|
||||
|
|
|
@ -27,7 +27,9 @@ class LibnotifyNotification : public Notification {
|
|||
const std::string& tag,
|
||||
const GURL& icon_url,
|
||||
const SkBitmap& icon,
|
||||
const bool silent) override;
|
||||
const bool silent,
|
||||
const bool has_reply,
|
||||
const base::string16 reply_placeholder) override;
|
||||
void Dismiss() override;
|
||||
|
||||
private:
|
||||
|
|
|
@ -26,10 +26,13 @@ class CocoaNotification : public Notification {
|
|||
const std::string& tag,
|
||||
const GURL& icon_url,
|
||||
const SkBitmap& icon,
|
||||
const bool silent) override;
|
||||
const bool silent,
|
||||
const bool hasReply,
|
||||
const base::string16 replyPlaceholder) override;
|
||||
void Dismiss() override;
|
||||
|
||||
void NotificationDisplayed();
|
||||
void NotificationReplied(std::string reply);
|
||||
|
||||
NSUserNotification* notification() const { return notification_; }
|
||||
|
||||
|
|
|
@ -28,7 +28,9 @@ void CocoaNotification::Show(const base::string16& title,
|
|||
const std::string& tag,
|
||||
const GURL& icon_url,
|
||||
const SkBitmap& icon,
|
||||
const bool silent) {
|
||||
const bool silent,
|
||||
const bool has_reply,
|
||||
const base::string16 reply_placeholder) {
|
||||
notification_.reset([[NSUserNotification alloc] init]);
|
||||
[notification_ setTitle:base::SysUTF16ToNSString(title)];
|
||||
[notification_ setInformativeText:base::SysUTF16ToNSString(body)];
|
||||
|
@ -46,6 +48,11 @@ void CocoaNotification::Show(const base::string16& title,
|
|||
[notification_ setSoundName:NSUserNotificationDefaultSoundName];
|
||||
}
|
||||
|
||||
if (has_reply) {
|
||||
[notification_ setResponsePlaceholder:base::SysUTF16ToNSString(reply_placeholder)];
|
||||
[notification_ setHasReplyButton:true];
|
||||
}
|
||||
|
||||
[NSUserNotificationCenter.defaultUserNotificationCenter
|
||||
deliverNotification:notification_];
|
||||
}
|
||||
|
@ -61,4 +68,8 @@ void CocoaNotification::NotificationDisplayed() {
|
|||
delegate()->NotificationDisplayed();
|
||||
}
|
||||
|
||||
void CocoaNotification::NotificationReplied(const std::string reply) {
|
||||
delegate()->NotificationReplied(reply);
|
||||
}
|
||||
|
||||
} // namespace brightray
|
||||
|
|
|
@ -21,15 +21,21 @@
|
|||
- (void)userNotificationCenter:(NSUserNotificationCenter*)center
|
||||
didDeliverNotification:(NSUserNotification*)notif {
|
||||
auto notification = presenter_->GetNotification(notif);
|
||||
if (notification)
|
||||
if (notification)
|
||||
notification->NotificationDisplayed();
|
||||
}
|
||||
|
||||
- (void)userNotificationCenter:(NSUserNotificationCenter*)center
|
||||
didActivateNotification:(NSUserNotification *)notif {
|
||||
auto notification = presenter_->GetNotification(notif);
|
||||
if (notification)
|
||||
if (notification) {
|
||||
notification->NotificationClicked();
|
||||
if (notif.activationType == NSUserNotificationActivationTypeReplied){
|
||||
notification->NotificationReplied([notif.response.string UTF8String]);
|
||||
} else {
|
||||
notification->NotificationClicked();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center
|
||||
|
|
|
@ -26,7 +26,9 @@ class Notification {
|
|||
const std::string& tag,
|
||||
const GURL& icon_url,
|
||||
const SkBitmap& icon,
|
||||
const bool silent) = 0;
|
||||
const bool silent,
|
||||
const bool hasReply,
|
||||
const base::string16 replyPlaceholder) = 0;
|
||||
// Closes the notification, this instance will be destroyed after the
|
||||
// notification gets closed.
|
||||
virtual void Dismiss() = 0;
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#define BRIGHTRAY_BROWSER_NOTIFICATION_DELEGATE_H_
|
||||
|
||||
#include "content/public/browser/desktop_notification_delegate.h"
|
||||
#include <string>
|
||||
|
||||
namespace brightray {
|
||||
|
||||
|
@ -16,6 +17,9 @@ class NotificationDelegate : public content::DesktopNotificationDelegate {
|
|||
|
||||
// Failed to send the notification.
|
||||
virtual void NotificationFailed() {}
|
||||
|
||||
// Notification was replied to
|
||||
virtual void NotificationReplied(std::string reply) {}
|
||||
};
|
||||
|
||||
} // namespace brightray
|
||||
|
|
|
@ -30,4 +30,8 @@ void NotificationDelegateAdapter::NotificationClick() {
|
|||
delegate_->NotificationClick();
|
||||
}
|
||||
|
||||
// void NotificationDelegateAdapter::NotificationReplied(std::string reply) {
|
||||
// delegate_->NotificationReplied(reply);
|
||||
// }
|
||||
|
||||
} // namespace brightray
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include "brightray/browser/platform_notification_service.h"
|
||||
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
#include "brightray/browser/browser_client.h"
|
||||
#include "brightray/browser/notification.h"
|
||||
#include "brightray/browser/notification_delegate_adapter.h"
|
||||
|
@ -30,7 +31,7 @@ void OnWebNotificationAllowed(base::WeakPtr<Notification> notification,
|
|||
return;
|
||||
if (allowed)
|
||||
notification->Show(data.title, data.body, data.tag, data.icon, icon,
|
||||
audio_muted ? true : data.silent);
|
||||
audio_muted ? true : data.silent, false, base::UTF8ToUTF16(""));
|
||||
else
|
||||
notification->Destroy();
|
||||
}
|
||||
|
|
|
@ -12,7 +12,8 @@ namespace brightray {
|
|||
void Win32Notification::Show(
|
||||
const base::string16& title, const base::string16& msg,
|
||||
const std::string& tag, const GURL& icon_url,
|
||||
const SkBitmap& icon, const bool silent) {
|
||||
const SkBitmap& icon, const bool silent,
|
||||
const bool has_reply, const base::string16 reply_placeholder) {
|
||||
auto presenter = static_cast<NotificationPresenterWin7*>(this->presenter());
|
||||
if (!presenter) return;
|
||||
|
||||
|
|
|
@ -12,7 +12,8 @@ class Win32Notification : public brightray::Notification {
|
|||
}
|
||||
void Show(const base::string16& title, const base::string16& msg,
|
||||
const std::string& tag, const GURL& icon_url,
|
||||
const SkBitmap& icon, const bool silent) override;
|
||||
const SkBitmap& icon, const bool silent,
|
||||
const bool has_reply, const base::string16 reply_placeholder) override;
|
||||
void Dismiss() override;
|
||||
|
||||
const DesktopNotificationController::Notification& GetRef() const {
|
||||
|
|
|
@ -89,7 +89,9 @@ void WindowsToastNotification::Show(const base::string16& title,
|
|||
const std::string& tag,
|
||||
const GURL& icon_url,
|
||||
const SkBitmap& icon,
|
||||
const bool silent) {
|
||||
const bool silent,
|
||||
const bool hasReply,
|
||||
const base::string16 replyPlaceholder) {
|
||||
auto presenter_win = static_cast<NotificationPresenterWin*>(presenter());
|
||||
std::wstring icon_path = presenter_win->SaveIconToFilesystem(icon, icon_url);
|
||||
|
||||
|
|
|
@ -55,7 +55,9 @@ class WindowsToastNotification : public Notification {
|
|||
const std::string& tag,
|
||||
const GURL& icon_url,
|
||||
const SkBitmap& icon,
|
||||
const bool silent) override;
|
||||
const bool silent,
|
||||
const bool has_reply,
|
||||
const base::string16 reply_placeholder) override;
|
||||
void Dismiss() override;
|
||||
|
||||
private:
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const {app, BrowserWindow} = require('electron')
|
||||
const {app, BrowserWindow, Notification} = require('electron')
|
||||
const path = require('path')
|
||||
|
||||
let mainWindow = null
|
||||
|
@ -27,5 +27,14 @@ exports.load = (appUrl) => {
|
|||
mainWindow = new BrowserWindow(options)
|
||||
mainWindow.loadURL(appUrl)
|
||||
mainWindow.focus()
|
||||
|
||||
const n = new Notification({
|
||||
title: 'Foo',
|
||||
body: 'Bar',
|
||||
hasReply: true,
|
||||
replyPlaceholder: 'foo'
|
||||
});
|
||||
n.on('reply', (...args) => console.log(args));
|
||||
n.show();
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue