Merge pull request #214 from electron/fix-notification

Fix notification not showing on Windows
This commit is contained in:
Cheng Zhao 2016-04-15 16:34:43 +09:00
commit 08b6fe5229
9 changed files with 43 additions and 60 deletions

View file

@ -136,19 +136,12 @@ void LibnotifyNotification::Dismiss() {
void LibnotifyNotification::OnNotificationClosed( void LibnotifyNotification::OnNotificationClosed(
NotifyNotification* notification) { NotifyNotification* notification) {
delegate()->NotificationClosed(); NotificationDismissed();
Destroy();
} }
void LibnotifyNotification::OnNotificationView( void LibnotifyNotification::OnNotificationView(
NotifyNotification* notification, char* action) { NotifyNotification* notification, char* action) {
delegate()->NotificationClick(); NotificationClicked();
Destroy();
}
void LibnotifyNotification::NotificationFailed() {
delegate()->NotificationFailed();
Destroy();
} }
} // namespace brightray } // namespace brightray

View file

@ -34,8 +34,6 @@ class LibnotifyNotification : public Notification {
CHROMEG_CALLBACK_1(LibnotifyNotification, void, OnNotificationView, CHROMEG_CALLBACK_1(LibnotifyNotification, void, OnNotificationView,
NotifyNotification*, char*); NotifyNotification*, char*);
void NotificationFailed();
NotifyNotification* notification_; NotifyNotification* notification_;
DISALLOW_COPY_AND_ASSIGN(LibnotifyNotification); DISALLOW_COPY_AND_ASSIGN(LibnotifyNotification);

View file

@ -28,8 +28,7 @@ class CocoaNotification : public Notification {
const bool silent) override; const bool silent) override;
void Dismiss() override; void Dismiss() override;
void NotifyDisplayed(); void NotificationDisplayed();
void NotifyClick();
NSUserNotification* notification() const { return notification_; } NSUserNotification* notification() const { return notification_; }

View file

@ -58,17 +58,11 @@ void CocoaNotification::Show(const base::string16& title,
void CocoaNotification::Dismiss() { void CocoaNotification::Dismiss() {
[NSUserNotificationCenter.defaultUserNotificationCenter [NSUserNotificationCenter.defaultUserNotificationCenter
removeDeliveredNotification:notification_]; removeDeliveredNotification:notification_];
delegate()->NotificationClosed(); NotificationDismissed();
Destroy();
} }
void CocoaNotification::NotifyDisplayed() { void CocoaNotification::NotificationDisplayed() {
delegate()->NotificationDisplayed(); delegate()->NotificationDisplayed();
} }
void CocoaNotification::NotifyClick() {
delegate()->NotificationClick();
Destroy();
}
} // namespace brightray } // namespace brightray

View file

@ -22,14 +22,14 @@
didDeliverNotification:(NSUserNotification*)notif { didDeliverNotification:(NSUserNotification*)notif {
auto notification = presenter_->GetNotification(notif); auto notification = presenter_->GetNotification(notif);
if (notification) if (notification)
notification->NotifyDisplayed(); notification->NotificationDisplayed();
} }
- (void)userNotificationCenter:(NSUserNotificationCenter*)center - (void)userNotificationCenter:(NSUserNotificationCenter*)center
didActivateNotification:(NSUserNotification *)notif { didActivateNotification:(NSUserNotification *)notif {
auto notification = presenter_->GetNotification(notif); auto notification = presenter_->GetNotification(notif);
if (notification) if (notification)
notification->NotifyClick(); notification->NotificationClicked();
} }
- (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center

View file

@ -20,6 +20,21 @@ Notification::~Notification() {
delegate()->NotificationDestroyed(); delegate()->NotificationDestroyed();
} }
void Notification::NotificationClicked() {
delegate()->NotificationClick();
Destroy();
}
void Notification::NotificationDismissed() {
delegate()->NotificationClosed();
Destroy();
}
void Notification::NotificationFailed() {
delegate()->NotificationFailed();
Destroy();
}
void Notification::Destroy() { void Notification::Destroy() {
presenter()->RemoveNotification(this); presenter()->RemoveNotification(this);
} }

View file

@ -29,6 +29,11 @@ class Notification {
// notification gets closed. // notification gets closed.
virtual void Dismiss() = 0; virtual void Dismiss() = 0;
// Should be called by derived classes.
void NotificationClicked();
void NotificationDismissed();
void NotificationFailed();
base::WeakPtr<Notification> GetWeakPtr() { base::WeakPtr<Notification> GetWeakPtr() {
return weak_factory_.GetWeakPtr(); return weak_factory_.GetWeakPtr();
} }

View file

@ -12,6 +12,7 @@
#include "browser/win/scoped_hstring.h" #include "browser/win/scoped_hstring.h"
#include "browser/win/notification_presenter_win.h" #include "browser/win/notification_presenter_win.h"
#include "common/application_info.h" #include "common/application_info.h"
#include "content/public/browser/browser_thread.h"
using namespace ABI::Windows::Data::Xml::Dom; using namespace ABI::Windows::Data::Xml::Dom;
@ -90,16 +91,7 @@ void WindowsToastNotification::Show(
const bool silent) { const bool silent) {
auto presenter_win = static_cast<NotificationPresenterWin*>(presenter()); auto presenter_win = static_cast<NotificationPresenterWin*>(presenter());
std::wstring icon_path = presenter_win->SaveIconToFilesystem(icon, icon_url); std::wstring icon_path = presenter_win->SaveIconToFilesystem(icon, icon_url);
// Ask Windows for the current notification settings
// If not allowed, we return here (0 = enabled)
ABI::Windows::UI::Notifications::NotificationSetting setting;
toast_notifier_->get_Setting(&setting);
if (setting != 0) {
NotificationFailed();
return;
}
ComPtr<IXmlDocument> toast_xml; ComPtr<IXmlDocument> toast_xml;
if(FAILED(GetToastXml(toast_manager_.Get(), title, msg, icon_path, silent, &toast_xml))) { if(FAILED(GetToastXml(toast_manager_.Get(), title, msg, icon_path, silent, &toast_xml))) {
NotificationFailed(); NotificationFailed();
@ -126,7 +118,7 @@ void WindowsToastNotification::Show(
return; return;
} }
if (FAILED(SetupCallbacks(toast_notification_.Get()))) { if (!SetupCallbacks(toast_notification_.Get())) {
NotificationFailed(); NotificationFailed();
return; return;
} }
@ -143,21 +135,6 @@ void WindowsToastNotification::Dismiss() {
toast_notifier_->Hide(toast_notification_.Get()); toast_notifier_->Hide(toast_notification_.Get());
} }
void WindowsToastNotification::NotificationClicked() {
delegate()->NotificationClick();
Destroy();
}
void WindowsToastNotification::NotificationDismissed() {
delegate()->NotificationClosed();
Destroy();
}
void WindowsToastNotification::NotificationFailed() {
delegate()->NotificationFailed();
Destroy();
}
bool WindowsToastNotification::GetToastXml( bool WindowsToastNotification::GetToastXml(
ABI::Windows::UI::Notifications::IToastNotificationManagerStatics* toastManager, ABI::Windows::UI::Notifications::IToastNotificationManagerStatics* toastManager,
const std::wstring& title, const std::wstring& title,
@ -399,8 +376,8 @@ bool WindowsToastNotification::RemoveCallbacks(
/* /*
/ Toast Event Handler / Toast Event Handler
*/ */
ToastEventHandler::ToastEventHandler(WindowsToastNotification* notification) ToastEventHandler::ToastEventHandler(Notification* notification)
: notification_(notification) { : notification_(notification->GetWeakPtr()) {
} }
ToastEventHandler::~ToastEventHandler() { ToastEventHandler::~ToastEventHandler() {
@ -408,21 +385,27 @@ ToastEventHandler::~ToastEventHandler() {
IFACEMETHODIMP ToastEventHandler::Invoke( IFACEMETHODIMP ToastEventHandler::Invoke(
ABI::Windows::UI::Notifications::IToastNotification* sender, IInspectable* args) { ABI::Windows::UI::Notifications::IToastNotification* sender, IInspectable* args) {
notification_->NotificationClicked(); content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&Notification::NotificationClicked, notification_));
return S_OK; return S_OK;
} }
IFACEMETHODIMP ToastEventHandler::Invoke( IFACEMETHODIMP ToastEventHandler::Invoke(
ABI::Windows::UI::Notifications::IToastNotification* sender, ABI::Windows::UI::Notifications::IToastNotification* sender,
ABI::Windows::UI::Notifications::IToastDismissedEventArgs* e) { ABI::Windows::UI::Notifications::IToastDismissedEventArgs* e) {
notification_->NotificationDismissed(); content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&Notification::NotificationDismissed, notification_));
return S_OK; return S_OK;
} }
IFACEMETHODIMP ToastEventHandler::Invoke( IFACEMETHODIMP ToastEventHandler::Invoke(
ABI::Windows::UI::Notifications::IToastNotification* sender, ABI::Windows::UI::Notifications::IToastNotification* sender,
ABI::Windows::UI::Notifications::IToastFailedEventArgs* e) { ABI::Windows::UI::Notifications::IToastFailedEventArgs* e) {
notification_->NotificationFailed(); content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&Notification::NotificationFailed, notification_));
return S_OK; return S_OK;
} }

View file

@ -50,10 +50,6 @@ class WindowsToastNotification : public Notification {
private: private:
friend class ToastEventHandler; friend class ToastEventHandler;
void NotificationClicked();
void NotificationDismissed();
void NotificationFailed();
bool GetToastXml(ABI::Windows::UI::Notifications::IToastNotificationManagerStatics* toastManager, bool GetToastXml(ABI::Windows::UI::Notifications::IToastNotificationManagerStatics* toastManager,
const std::wstring& title, const std::wstring& title,
const std::wstring& msg, const std::wstring& msg,
@ -97,7 +93,7 @@ class ToastEventHandler : public RuntimeClass<RuntimeClassFlags<ClassicCom>,
DesktopToastDismissedEventHandler, DesktopToastDismissedEventHandler,
DesktopToastFailedEventHandler> { DesktopToastFailedEventHandler> {
public: public:
ToastEventHandler(WindowsToastNotification* notification); ToastEventHandler(Notification* notification);
~ToastEventHandler(); ~ToastEventHandler();
IFACEMETHODIMP Invoke(ABI::Windows::UI::Notifications::IToastNotification* sender, IInspectable* args); IFACEMETHODIMP Invoke(ABI::Windows::UI::Notifications::IToastNotification* sender, IInspectable* args);
@ -105,7 +101,7 @@ class ToastEventHandler : public RuntimeClass<RuntimeClassFlags<ClassicCom>,
IFACEMETHODIMP Invoke(ABI::Windows::UI::Notifications::IToastNotification* sender, ABI::Windows::UI::Notifications::IToastFailedEventArgs* e); IFACEMETHODIMP Invoke(ABI::Windows::UI::Notifications::IToastNotification* sender, ABI::Windows::UI::Notifications::IToastFailedEventArgs* e);
private: private:
WindowsToastNotification* notification_; // weak ref. base::WeakPtr<Notification> notification_; // weak ref.
DISALLOW_COPY_AND_ASSIGN(ToastEventHandler); DISALLOW_COPY_AND_ASSIGN(ToastEventHandler);
}; };