Merge pull request #11431 from electron/notifications-debugging
infra: Allow notifications debugging
This commit is contained in:
commit
781311aa3c
6 changed files with 75 additions and 5 deletions
|
@ -32,6 +32,8 @@ class CocoaNotification : public Notification {
|
||||||
NSUserNotification* notification() const { return notification_; }
|
NSUserNotification* notification() const { return notification_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void LogAction(const char* action);
|
||||||
|
|
||||||
base::scoped_nsobject<NSUserNotification> notification_;
|
base::scoped_nsobject<NSUserNotification> notification_;
|
||||||
int action_index_;
|
int action_index_;
|
||||||
|
|
||||||
|
|
|
@ -28,12 +28,19 @@ CocoaNotification::~CocoaNotification() {
|
||||||
|
|
||||||
void CocoaNotification::Show(const NotificationOptions& options) {
|
void CocoaNotification::Show(const NotificationOptions& options) {
|
||||||
notification_.reset([[NSUserNotification alloc] init]);
|
notification_.reset([[NSUserNotification alloc] init]);
|
||||||
|
|
||||||
|
NSString* identifier = [NSString stringWithFormat:@"%s%d", "ElectronNotification", g_identifier_];
|
||||||
|
|
||||||
[notification_ setTitle:base::SysUTF16ToNSString(options.title)];
|
[notification_ setTitle:base::SysUTF16ToNSString(options.title)];
|
||||||
[notification_ setSubtitle:base::SysUTF16ToNSString(options.subtitle)];
|
[notification_ setSubtitle:base::SysUTF16ToNSString(options.subtitle)];
|
||||||
[notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)];
|
[notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)];
|
||||||
[notification_ setIdentifier:[NSString stringWithFormat:@"%s%d", "ElectronNotification", g_identifier_]];
|
[notification_ setIdentifier:identifier];
|
||||||
g_identifier_++;
|
g_identifier_++;
|
||||||
|
|
||||||
|
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
|
||||||
|
LOG(INFO) << "Notification created (" << [identifier UTF8String] << ")";
|
||||||
|
}
|
||||||
|
|
||||||
if ([notification_ respondsToSelector:@selector(setContentImage:)] &&
|
if ([notification_ respondsToSelector:@selector(setContentImage:)] &&
|
||||||
!options.icon.drawsNothing()) {
|
!options.icon.drawsNothing()) {
|
||||||
NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
|
NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
|
||||||
|
@ -74,22 +81,38 @@ void CocoaNotification::Dismiss() {
|
||||||
if (notification_)
|
if (notification_)
|
||||||
[NSUserNotificationCenter.defaultUserNotificationCenter
|
[NSUserNotificationCenter.defaultUserNotificationCenter
|
||||||
removeDeliveredNotification:notification_];
|
removeDeliveredNotification:notification_];
|
||||||
|
|
||||||
NotificationDismissed();
|
NotificationDismissed();
|
||||||
|
|
||||||
|
this->LogAction("dismissed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CocoaNotification::NotificationDisplayed() {
|
void CocoaNotification::NotificationDisplayed() {
|
||||||
if (delegate())
|
if (delegate())
|
||||||
delegate()->NotificationDisplayed();
|
delegate()->NotificationDisplayed();
|
||||||
|
|
||||||
|
this->LogAction("displayed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CocoaNotification::NotificationReplied(const std::string& reply) {
|
void CocoaNotification::NotificationReplied(const std::string& reply) {
|
||||||
if (delegate())
|
if (delegate())
|
||||||
delegate()->NotificationReplied(reply);
|
delegate()->NotificationReplied(reply);
|
||||||
|
|
||||||
|
this->LogAction("replied to");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CocoaNotification::NotificationButtonClicked() {
|
void CocoaNotification::NotificationButtonClicked() {
|
||||||
if (delegate())
|
if (delegate())
|
||||||
delegate()->NotificationAction(action_index_);
|
delegate()->NotificationAction(action_index_);
|
||||||
|
|
||||||
|
this->LogAction("button clicked");
|
||||||
|
}
|
||||||
|
|
||||||
|
void CocoaNotification::LogAction(const char* action) {
|
||||||
|
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
|
||||||
|
NSString* identifier = [notification_ valueForKey:@"identifier"];
|
||||||
|
LOG(INFO) << "Notification " << action << " (" << [identifier UTF8String] << ")";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace brightray
|
} // namespace brightray
|
||||||
|
|
|
@ -28,13 +28,18 @@
|
||||||
- (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 (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
|
||||||
|
LOG(INFO) << "Notification activated (" << [notif.identifier UTF8String] << ")";
|
||||||
|
}
|
||||||
|
|
||||||
if (notification) {
|
if (notification) {
|
||||||
if (notif.activationType == NSUserNotificationActivationTypeReplied) {
|
if (notif.activationType == NSUserNotificationActivationTypeReplied) {
|
||||||
notification->NotificationReplied([notif.response.string UTF8String]);
|
notification->NotificationReplied([notif.response.string UTF8String]);
|
||||||
} else if (notif.activationType == NSUserNotificationActivationTypeActionButtonClicked) {
|
} else if (notif.activationType == NSUserNotificationActivationTypeActionButtonClicked) {
|
||||||
notification->NotificationButtonClicked();
|
notification->NotificationButtonClicked();
|
||||||
} else if (notif.activationType == NSUserNotificationActivationTypeContentsClicked) {
|
} else if (notif.activationType == NSUserNotificationActivationTypeContentsClicked) {
|
||||||
notification->NotificationClicked();
|
notification->NotificationClicked();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,11 @@ CocoaNotification* NotificationPresenterMac::GetNotification(
|
||||||
isEqual:ns_notification.identifier])
|
isEqual:ns_notification.identifier])
|
||||||
return native_notification;
|
return native_notification;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
|
||||||
|
LOG(INFO) << "Could not find notification for " << [ns_notification.identifier UTF8String];
|
||||||
|
}
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "base/environment.h"
|
||||||
#include "base/files/file_util.h"
|
#include "base/files/file_util.h"
|
||||||
#include "base/md5.h"
|
#include "base/md5.h"
|
||||||
#include "base/strings/utf_string_conversions.h"
|
#include "base/strings/utf_string_conversions.h"
|
||||||
|
@ -26,6 +27,10 @@ namespace brightray {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
bool IsDebuggingNotifications() {
|
||||||
|
return base::Environment::Create()->HasVar("ELECTRON_DEBUG_NOTIFICATIONS");
|
||||||
|
}
|
||||||
|
|
||||||
bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) {
|
bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) {
|
||||||
std::vector<unsigned char> png_data;
|
std::vector<unsigned char> png_data;
|
||||||
if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data))
|
if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data))
|
||||||
|
@ -49,6 +54,10 @@ NotificationPresenter* NotificationPresenter::Create() {
|
||||||
new NotificationPresenterWin);
|
new NotificationPresenterWin);
|
||||||
if (!presenter->Init())
|
if (!presenter->Init())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
|
if (IsDebuggingNotifications())
|
||||||
|
LOG(INFO) << "Successfully created Windows notifications presenter";
|
||||||
|
|
||||||
return presenter.release();
|
return presenter.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#include <shlobj.h>
|
#include <shlobj.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "base/environment.h"
|
||||||
#include "base/strings/utf_string_conversions.h"
|
#include "base/strings/utf_string_conversions.h"
|
||||||
#include "brightray/browser/notification_delegate.h"
|
#include "brightray/browser/notification_delegate.h"
|
||||||
#include "brightray/browser/win/notification_presenter_win.h"
|
#include "brightray/browser/win/notification_presenter_win.h"
|
||||||
|
@ -41,6 +42,10 @@ bool GetAppUserModelId(ScopedHString* app_id) {
|
||||||
return app_id->success();
|
return app_id->success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IsDebuggingNotifications() {
|
||||||
|
return base::Environment::Create()->HasVar("ELECTRON_DEBUG_NOTIFICATIONS");
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
@ -129,11 +134,14 @@ void WindowsToastNotification::Show(const NotificationOptions& options) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (IsDebuggingNotifications()) LOG(INFO) << "Notification created";
|
||||||
|
|
||||||
if (delegate())
|
if (delegate())
|
||||||
delegate()->NotificationDisplayed();
|
delegate()->NotificationDisplayed();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowsToastNotification::Dismiss() {
|
void WindowsToastNotification::Dismiss() {
|
||||||
|
if (IsDebuggingNotifications()) LOG(INFO) << "Hiding notification";
|
||||||
toast_notifier_->Hide(toast_notification_.Get());
|
toast_notifier_->Hide(toast_notification_.Get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,16 +172,28 @@ bool WindowsToastNotification::GetToastXml(
|
||||||
? ABI::Windows::UI::Notifications::ToastTemplateType_ToastText02
|
? ABI::Windows::UI::Notifications::ToastTemplateType_ToastText02
|
||||||
: ABI::Windows::UI::Notifications::
|
: ABI::Windows::UI::Notifications::
|
||||||
ToastTemplateType_ToastImageAndText02;
|
ToastTemplateType_ToastImageAndText02;
|
||||||
if (FAILED(toastManager->GetTemplateContent(template_type, toast_xml)))
|
if (FAILED(toastManager->GetTemplateContent(template_type, toast_xml))) {
|
||||||
|
if (IsDebuggingNotifications())
|
||||||
|
LOG(INFO) << "Fetching XML template failed";
|
||||||
return false;
|
return false;
|
||||||
if (!SetXmlText(*toast_xml, title, msg))
|
}
|
||||||
|
|
||||||
|
if (!SetXmlText(*toast_xml, title, msg)) {
|
||||||
|
if (IsDebuggingNotifications())
|
||||||
|
LOG(INFO) << "Setting text fields on template failed";
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure the toast's notification sound
|
// Configure the toast's notification sound
|
||||||
if (silent) {
|
if (silent) {
|
||||||
if (FAILED(SetXmlAudioSilent(*toast_xml)))
|
if (FAILED(SetXmlAudioSilent(*toast_xml))) {
|
||||||
|
if (IsDebuggingNotifications()) {
|
||||||
|
LOG(INFO) << "Setting \"silent\" option on notification failed";
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure the toast's image
|
// Configure the toast's image
|
||||||
|
@ -398,6 +418,8 @@ IFACEMETHODIMP ToastEventHandler::Invoke(
|
||||||
content::BrowserThread::PostTask(
|
content::BrowserThread::PostTask(
|
||||||
content::BrowserThread::UI, FROM_HERE,
|
content::BrowserThread::UI, FROM_HERE,
|
||||||
base::Bind(&Notification::NotificationClicked, notification_));
|
base::Bind(&Notification::NotificationClicked, notification_));
|
||||||
|
if (IsDebuggingNotifications()) LOG(INFO) << "Notification clicked";
|
||||||
|
|
||||||
return S_OK;
|
return S_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -407,6 +429,8 @@ IFACEMETHODIMP ToastEventHandler::Invoke(
|
||||||
content::BrowserThread::PostTask(
|
content::BrowserThread::PostTask(
|
||||||
content::BrowserThread::UI, FROM_HERE,
|
content::BrowserThread::UI, FROM_HERE,
|
||||||
base::Bind(&Notification::NotificationDismissed, notification_));
|
base::Bind(&Notification::NotificationDismissed, notification_));
|
||||||
|
if (IsDebuggingNotifications()) LOG(INFO) << "Notification dismissed";
|
||||||
|
|
||||||
return S_OK;
|
return S_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -416,6 +440,8 @@ IFACEMETHODIMP ToastEventHandler::Invoke(
|
||||||
content::BrowserThread::PostTask(
|
content::BrowserThread::PostTask(
|
||||||
content::BrowserThread::UI, FROM_HERE,
|
content::BrowserThread::UI, FROM_HERE,
|
||||||
base::Bind(&Notification::NotificationFailed, notification_));
|
base::Bind(&Notification::NotificationFailed, notification_));
|
||||||
|
if (IsDebuggingNotifications()) LOG(INFO) << "Notification failed";
|
||||||
|
|
||||||
return S_OK;
|
return S_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue