Skip to content

Commit fbff2ae

Browse files
daniel-kane-ebDan Kane
andauthored
stopped using aumi for toast notifications MSIX packaged Apps (#142)
Co-authored-by: Dan Kane <[email protected]>
1 parent f9110cb commit fbff2ae

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

packages/win_toast/windows/wintoastlib.cpp

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <assert.h>
2424
#include <unordered_map>
2525
#include <array>
26+
#include <appmodel.h>
2627

2728
#pragma comment(lib,"shlwapi")
2829
#pragma comment(lib,"user32")
@@ -504,10 +505,12 @@ bool WinToast::initialize(_Out_opt_ WinToastError* error) {
504505
}
505506
}
506507

507-
if (FAILED(DllImporter::SetCurrentProcessExplicitAppUserModelID(_aumi.c_str()))) {
508-
setError(error, WinToastError::InvalidAppUserModelID);
509-
DEBUG_MSG(L"Error while attaching the AUMI to the current proccess =(");
510-
return false;
508+
if (!hasIdentity()) {
509+
if (FAILED(DllImporter::SetCurrentProcessExplicitAppUserModelID(_aumi.c_str()))) {
510+
setError(error, WinToastError::InvalidAppUserModelID);
511+
DEBUG_MSG(L"Error while attaching the AUMI to the current proccess =(");
512+
return false;
513+
}
511514
}
512515

513516
_isInitialized = true;
@@ -636,6 +639,27 @@ HRESULT WinToast::createShellLinkHelper() {
636639
return hr;
637640
}
638641

642+
bool WinToast::hasIdentity() {
643+
UINT32 length;
644+
auto err = GetCurrentPackageFullName(&length, nullptr);
645+
if (err != ERROR_INSUFFICIENT_BUFFER) {
646+
return false;
647+
}
648+
649+
PWSTR fullName = (PWSTR)malloc(length * sizeof(*fullName));
650+
if (fullName == nullptr) {
651+
return false;
652+
}
653+
654+
err = GetCurrentPackageFullName(&length, fullName);
655+
if (err != ERROR_SUCCESS) {
656+
return false;
657+
}
658+
659+
free(fullName);
660+
return true;
661+
}
662+
639663
INT64 WinToast::showToast(_In_ const WinToastTemplate& toast, _In_ std::unique_ptr<IWinToastHandler> handler, _Out_ WinToastError* error) {
640664
setError(error, WinToastError::NoError);
641665
INT64 id = -1;
@@ -654,7 +678,13 @@ INT64 WinToast::showToast(_In_ const WinToastTemplate& toast, _In_ std::unique_
654678
HRESULT hr = DllImporter::Wrap_GetActivationFactory(WinToastStringWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(), &notificationManager);
655679
if (SUCCEEDED(hr)) {
656680
ComPtr<IToastNotifier> notifier;
657-
hr = notificationManager->CreateToastNotifierWithId(WinToastStringWrapper(_aumi).Get(), &notifier);
681+
if (hasIdentity()) {
682+
hr = notificationManager->CreateToastNotifier(&notifier);
683+
}
684+
else {
685+
hr = notificationManager->CreateToastNotifierWithId(WinToastStringWrapper(_aumi).Get(), &notifier);
686+
}
687+
658688
if (SUCCEEDED(hr)) {
659689
ComPtr<IToastNotificationFactory> notificationFactory;
660690
hr = DllImporter::Wrap_GetActivationFactory(WinToastStringWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotification).Get(), &notificationFactory);
@@ -743,12 +773,17 @@ INT64 WinToast::showToast(_In_ const WinToastTemplate& toast, _In_ std::unique_
743773
return FAILED(hr) ? -1 : id;
744774
}
745775

746-
ComPtr<IToastNotifier> WinToast::notifier(_In_ bool* succeded) const {
776+
ComPtr<IToastNotifier> WinToast::notifier(_In_ bool* succeded) {
747777
ComPtr<IToastNotificationManagerStatics> notificationManager;
748778
ComPtr<IToastNotifier> notifier;
749779
HRESULT hr = DllImporter::Wrap_GetActivationFactory(WinToastStringWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(), &notificationManager);
750780
if (SUCCEEDED(hr)) {
751-
hr = notificationManager->CreateToastNotifierWithId(WinToastStringWrapper(_aumi).Get(), &notifier);
781+
if (hasIdentity()) {
782+
hr = notificationManager->CreateToastNotifier(&notifier);
783+
}
784+
else {
785+
hr = notificationManager->CreateToastNotifierWithId(WinToastStringWrapper(_aumi).Get(), &notifier);
786+
}
752787
}
753788
*succeded = SUCCEEDED(hr);
754789
return notifier;

packages/win_toast/windows/wintoastlib.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ namespace WinToastLib {
201201
virtual bool initialize(_Out_opt_ WinToastError* error = nullptr);
202202
virtual bool isInitialized() const;
203203
virtual bool hideToast(_In_ INT64 id);
204+
virtual bool hasIdentity();
204205
virtual INT64 showToast(_In_ const WinToastTemplate& toast, _In_ std::unique_ptr<IWinToastHandler> handler, _Out_opt_ WinToastError* error = nullptr);
205206
virtual void clear();
206207
virtual enum ShortcutResult createShortcut();
@@ -228,7 +229,7 @@ namespace WinToastLib {
228229
HRESULT addActionHelper(_In_ IXmlDocument *xml, _In_ const std::wstring& action, _In_ const std::wstring& arguments);
229230
HRESULT addDurationHelper(_In_ IXmlDocument *xml, _In_ const std::wstring& duration);
230231
HRESULT addScenarioHelper(_In_ IXmlDocument *xml, _In_ const std::wstring& scenario);
231-
ComPtr<IToastNotifier> notifier(_In_ bool* succeded) const;
232+
ComPtr<IToastNotifier> notifier(_In_ bool* succeded);
232233
void setError(_Out_opt_ WinToastError* error, _In_ WinToastError value);
233234
};
234235
}

0 commit comments

Comments
 (0)