23
23
#include < assert.h>
24
24
#include < unordered_map>
25
25
#include < array>
26
+ #include < appmodel.h>
26
27
27
28
#pragma comment(lib,"shlwapi")
28
29
#pragma comment(lib,"user32")
@@ -504,10 +505,12 @@ bool WinToast::initialize(_Out_opt_ WinToastError* error) {
504
505
}
505
506
}
506
507
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
+ }
511
514
}
512
515
513
516
_isInitialized = true ;
@@ -636,6 +639,27 @@ HRESULT WinToast::createShellLinkHelper() {
636
639
return hr;
637
640
}
638
641
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
+
639
663
INT64 WinToast::showToast (_In_ const WinToastTemplate& toast, _In_ std::unique_ptr<IWinToastHandler> handler, _Out_ WinToastError* error) {
640
664
setError (error, WinToastError::NoError);
641
665
INT64 id = -1 ;
@@ -654,7 +678,13 @@ INT64 WinToast::showToast(_In_ const WinToastTemplate& toast, _In_ std::unique_
654
678
HRESULT hr = DllImporter::Wrap_GetActivationFactory (WinToastStringWrapper (RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get (), ¬ificationManager);
655
679
if (SUCCEEDED (hr)) {
656
680
ComPtr<IToastNotifier> notifier;
657
- hr = notificationManager->CreateToastNotifierWithId (WinToastStringWrapper (_aumi).Get (), ¬ifier);
681
+ if (hasIdentity ()) {
682
+ hr = notificationManager->CreateToastNotifier (¬ifier);
683
+ }
684
+ else {
685
+ hr = notificationManager->CreateToastNotifierWithId (WinToastStringWrapper (_aumi).Get (), ¬ifier);
686
+ }
687
+
658
688
if (SUCCEEDED (hr)) {
659
689
ComPtr<IToastNotificationFactory> notificationFactory;
660
690
hr = DllImporter::Wrap_GetActivationFactory (WinToastStringWrapper (RuntimeClass_Windows_UI_Notifications_ToastNotification).Get (), ¬ificationFactory);
@@ -743,12 +773,17 @@ INT64 WinToast::showToast(_In_ const WinToastTemplate& toast, _In_ std::unique_
743
773
return FAILED (hr) ? -1 : id;
744
774
}
745
775
746
- ComPtr<IToastNotifier> WinToast::notifier (_In_ bool * succeded) const {
776
+ ComPtr<IToastNotifier> WinToast::notifier (_In_ bool * succeded) {
747
777
ComPtr<IToastNotificationManagerStatics> notificationManager;
748
778
ComPtr<IToastNotifier> notifier;
749
779
HRESULT hr = DllImporter::Wrap_GetActivationFactory (WinToastStringWrapper (RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get (), ¬ificationManager);
750
780
if (SUCCEEDED (hr)) {
751
- hr = notificationManager->CreateToastNotifierWithId (WinToastStringWrapper (_aumi).Get (), ¬ifier);
781
+ if (hasIdentity ()) {
782
+ hr = notificationManager->CreateToastNotifier (¬ifier);
783
+ }
784
+ else {
785
+ hr = notificationManager->CreateToastNotifierWithId (WinToastStringWrapper (_aumi).Get (), ¬ifier);
786
+ }
752
787
}
753
788
*succeded = SUCCEEDED (hr);
754
789
return notifier;
0 commit comments