Get image notifications working on Win7 + Win10

This commit is contained in:
Samuel Attard 2017-04-24 12:03:05 +10:00
parent 6bbc4c3113
commit 03688b9415
No known key found for this signature in database
GPG key ID: 273DC1869D8F13EF

View file

@ -4,15 +4,20 @@
#include "atom/browser/api/atom_api_notification.h" #include "atom/browser/api/atom_api_notification.h"
#include "atom/browser/browser.h"
#include "atom/browser/ui/notification_delegate_adapter.h" #include "atom/browser/ui/notification_delegate_adapter.h"
#include "atom/browser/ui/win/toast_handler.h" #include "atom/browser/ui/win/toast_handler.h"
#include "atom/browser/ui/win/toast_lib.h" #include "atom/browser/ui/win/toast_lib.h"
#include "base/files/file_util.h"
#include "base/md5.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "browser/notification.h" #include "browser/notification.h"
#include "browser/notification_presenter.h" #include "browser/notification_presenter.h"
#include "browser/win/notification_presenter_win.h"
#include "browser/win/notification_presenter_win7.h" #include "browser/win/notification_presenter_win7.h"
#include "common/string_conversion.h" #include "common/string_conversion.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace atom { namespace atom {
@ -21,13 +26,49 @@ namespace api {
bool can_toast_ = true; bool can_toast_ = true;
bool initialized_ = false; bool initialized_ = false;
brightray::NotificationPresenter* presenter; brightray::NotificationPresenterWin7* presenter;
base::ScopedTempDir temp_dir_;
bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) {
std::vector<unsigned char> png_data;
if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data))
return false;
char* data = reinterpret_cast<char*>(&png_data[0]);
int size = static_cast<int>(png_data.size());
return base::WriteFile(path, data, size) == size;
}
void Notification::Show() { void Notification::Show() {
SkBitmap image = *(new SkBitmap);
if (has_icon_) {
image = *(icon_.ToSkBitmap());
}
if (can_toast_) { if (can_toast_) {
atom::AtomToastHandler* handler = new atom::AtomToastHandler(this); atom::AtomToastHandler* handler = new atom::AtomToastHandler(this);
WinToastLib::WinToastTemplate toast = WinToastLib::WinToastTemplate(WinToastLib::WinToastTemplate::TextTwoLines); WinToastLib::WinToastTemplate::WinToastTemplateType toastType = WinToastLib::WinToastTemplate::TextOneLine;
// toast.setImagePath(L"C:\example.png"); if (!has_icon_) {
if (body_ != L"") {
toastType = WinToastLib::WinToastTemplate::TextTwoLines;
} else {
toastType = WinToastLib::WinToastTemplate::TextOneLine;
}
} else {
if (body_ != L"") {
toastType = WinToastLib::WinToastTemplate::ImageWithTwoLines;
} else {
toastType = WinToastLib::WinToastTemplate::ImageWithOneLine;
}
}
WinToastLib::WinToastTemplate toast = WinToastLib::WinToastTemplate(toastType);
std::string filename = base::MD5String(base::UTF16ToUTF8(icon_path_)) + ".png";
base::FilePath savePath = temp_dir_.GetPath().Append(base::UTF8ToUTF16(filename));
if (has_icon_ && SaveIconToPath(image, savePath)) {
toast.setImagePath(savePath.value());
}
toast.setTextField(title_, WinToastLib::WinToastTemplate::TextField::FirstLine); toast.setTextField(title_, WinToastLib::WinToastTemplate::TextField::FirstLine);
toast.setTextField(body_, WinToastLib::WinToastTemplate::TextField::SecondLine); toast.setTextField(body_, WinToastLib::WinToastTemplate::TextField::SecondLine);
toast.setSilent(silent_); toast.setSilent(silent_);
@ -38,27 +79,26 @@ void Notification::Show() {
} else { } else {
AtomNotificationDelegateAdapter* adapter = new AtomNotificationDelegateAdapter(this); AtomNotificationDelegateAdapter* adapter = new AtomNotificationDelegateAdapter(this);
auto notif = presenter->CreateNotification(adapter); auto notif = presenter->CreateNotification(adapter);
GURL* u = new GURL; GURL nullUrl = *(new GURL);
notif->Show( notif->Show(
title_, title_,
body_, body_,
"", "",
u->Resolve(""), nullUrl,
*(new SkBitmap), image,
true silent_
); );
} }
} }
void Notification::OnInitialProps() { void Notification::OnInitialProps() {
if (!initialized_) { if (!initialized_) {
WinToastLib::WinToast::instance()->setAppName(L"WinToastExample"); Browser* browser = Browser::Get();
WinToastLib::WinToast::instance()->setAppUserModelId( WinToastLib::WinToast::instance()->setAppName(base::UTF8ToUTF16(browser->GetName()));
WinToastLib::WinToast::configureAUMI(L"mohabouje", L"wintoast", L"wintoastexample", L"20161006") WinToastLib::WinToast::instance()->setAppUserModelId(browser->GetAppUserModelID());
);
can_toast_ = WinToastLib::WinToast::instance()->initialize(); can_toast_ = WinToastLib::WinToast::instance()->initialize();
temp_dir_.CreateUniqueTempDir();
} }
can_toast_ = false;
if (!can_toast_) { if (!can_toast_) {
presenter = new brightray::NotificationPresenterWin7; presenter = new brightray::NotificationPresenterWin7;
} }