/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "ToastNotificationHandler.h" #include "MetroUtils.h" #include "mozilla/Services.h" #include "FrameworkView.h" using namespace ABI::Windows::Foundation; using namespace ABI::Windows::Data::Xml::Dom; using namespace Microsoft::WRL; using namespace Microsoft::WRL::Wrappers; using namespace mozilla; using namespace ABI::Windows::UI::Notifications; typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_IInspectable_t ToastActivationHandler; void ToastNotificationHandler::DisplayNotification(HSTRING title, HSTRING msg, HSTRING imagePath) { ComPtr toastNotificationManagerStatics; AssertHRESULT(GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(), toastNotificationManagerStatics.GetAddressOf())); ComPtr toastXml; toastNotificationManagerStatics->GetTemplateContent(ToastTemplateType::ToastTemplateType_ToastImageAndText03, &toastXml); ComPtr toastTextElements, toastImageElements; ComPtr titleTextNodeRoot, msgTextNodeRoot, imageNodeRoot, srcAttribute; HSTRING textNodeStr, imageNodeStr, srcNodeStr; HSTRING_HEADER textHeader, imageHeader, srcHeader; WindowsCreateStringReference(L"text", 4, &textHeader, &textNodeStr); WindowsCreateStringReference(L"image", 5, &imageHeader, &imageNodeStr); WindowsCreateStringReference(L"src", 3, &srcHeader, &srcNodeStr); toastXml->GetElementsByTagName(textNodeStr, &toastTextElements); toastXml->GetElementsByTagName(imageNodeStr, &toastImageElements); AssertHRESULT(toastTextElements->Item(0, &titleTextNodeRoot)); AssertHRESULT(toastTextElements->Item(1, &msgTextNodeRoot)); AssertHRESULT(toastImageElements->Item(0, &imageNodeRoot)); ComPtr attributes; AssertHRESULT(imageNodeRoot->get_Attributes(&attributes)); AssertHRESULT(attributes->GetNamedItem(srcNodeStr, &srcAttribute)); SetNodeValueString(title, titleTextNodeRoot.Get(), toastXml.Get()); SetNodeValueString(msg, msgTextNodeRoot.Get(), toastXml.Get()); SetNodeValueString(imagePath, srcAttribute.Get(), toastXml.Get()); ComPtr notification; ComPtr factory; AssertHRESULT(GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_Notifications_ToastNotification).Get(), factory.GetAddressOf())); AssertHRESULT(factory->CreateToastNotification(toastXml.Get(), ¬ification)); EventRegistrationToken activatedToken; AssertHRESULT(notification->add_Activated(Callback(this, &ToastNotificationHandler::OnActivate).Get(), &activatedToken)); ComPtr notifier; toastNotificationManagerStatics->CreateToastNotifier(¬ifier); notifier->Show(notification.Get()); } void ToastNotificationHandler::SetNodeValueString(HSTRING inputString, ComPtr node, ComPtr xml) { ComPtr inputText; ComPtr inputTextNode, pAppendedChild; AssertHRESULT(xml->CreateTextNode(inputString, &inputText)); AssertHRESULT(inputText.As(&inputTextNode)); AssertHRESULT(node->AppendChild(inputTextNode.Get(), &pAppendedChild)); } HRESULT ToastNotificationHandler::OnActivate(IToastNotification *notification, IInspectable *inspectable) { MetroUtils::FireObserver("metro_native_toast_clicked"); return S_OK; }