From 0debeb84c6e7074301c29342a322a5f50f5b1725 Mon Sep 17 00:00:00 2001 From: Venkata Sharath Chandra Manchala Date: Mon, 20 Sep 2021 07:00:20 -0700 Subject: [PATCH 01/11] Support foregroundtask registration for packaged applications --- .../PushNotificationChannel.cpp | 53 ++++++++++++++++--- .../PushNotificationChannel.h | 14 ++++- .../PushNotificationsDemoPackage.wapproj | 1 + 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/dev/PushNotifications/PushNotificationChannel.cpp b/dev/PushNotifications/PushNotificationChannel.cpp index 2c1c166271..47a621653b 100644 --- a/dev/PushNotifications/PushNotificationChannel.cpp +++ b/dev/PushNotifications/PushNotificationChannel.cpp @@ -89,15 +89,36 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation { if (IsPackagedAppScenario()) { - return m_channel.PushNotificationReceived([weak_self = get_weak(), handler](auto&&, auto&& args) + if (m_channel) { - if (auto strong = weak_self.get()) + return m_channel.PushNotificationReceived([weak_self = get_weak(), handler](auto&&, auto&& args) { - auto pushArgs = winrt::make(args); - pushArgs.Handled(true); - handler(*strong, pushArgs); - }; - }); + if (auto strong = weak_self.get()) + { + auto pushArgs = winrt::make(args); + pushArgs.Handled(true); + handler(*strong, pushArgs); + }; + }); + } + else + { + auto lock = m_lock.lock_exclusive(); + + THROW_HR_IF_NULL(E_UNEXPECTED, m_channelInfo.channelUri.c_str()); + + wchar_t appUserModelId[APPLICATION_USER_MODEL_ID_MAX_LENGTH] = {}; + UINT32 appUserModelIdSize{ ARRAYSIZE(appUserModelId) }; + + THROW_IF_FAILED(GetCurrentApplicationUserModelId(&appUserModelIdSize, appUserModelId)); + + THROW_IF_FAILED(PushNotifications_RegisterNotificationSinkForFullTrustApplication(appUserModelId, this)); + + return m_foregroundHandlers.add(handler); + + + } + // One more check if channelInfo struct is null - we should throw E_UNEXPECTED } else { @@ -120,7 +141,23 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation { if (IsPackagedAppScenario()) { - m_channel.PushNotificationReceived(token); + if (m_channel) + { + m_channel.PushNotificationReceived(token); + } + else + { + auto lock = m_lock.lock_exclusive(); + + wchar_t appUserModelId[APPLICATION_USER_MODEL_ID_MAX_LENGTH] = {}; + UINT32 appUserModelIdSize{ ARRAYSIZE(appUserModelId) }; + + THROW_IF_FAILED(GetCurrentApplicationUserModelId(&appUserModelIdSize, appUserModelId)); + + THROW_IF_FAILED(PushNotifications_UnregisterNotificationSinkForFullTrustApplication(appUserModelId)); + m_foregroundHandlers.remove(token); + + } } else { diff --git a/dev/PushNotifications/PushNotificationChannel.h b/dev/PushNotifications/PushNotificationChannel.h index cb02cb6e68..976b50e390 100644 --- a/dev/PushNotifications/PushNotificationChannel.h +++ b/dev/PushNotifications/PushNotificationChannel.h @@ -5,6 +5,7 @@ #include "Microsoft.Windows.PushNotifications.PushNotificationChannel.g.h" #include #include "winrt/Windows.Networking.PushNotifications.h" +#include #include "externs.h" namespace winrt::Microsoft::Windows::PushNotifications::implementation @@ -13,7 +14,7 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation winrt::Microsoft::Windows::PushNotifications::PushNotificationChannel, winrt::Microsoft::Windows::PushNotifications::PushNotificationReceivedEventArgs> PushNotificationEventHandler; - struct PushNotificationChannel : PushNotificationChannelT + struct PushNotificationChannel : PushNotificationChannelT { PushNotificationChannel(winrt::Windows::Networking::PushNotifications::PushNotificationChannel const& channel); @@ -29,6 +30,17 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation // IWpnForegroundSink HRESULT __stdcall InvokeAll(_In_ ULONG length, _In_ byte* payload, _Out_ BOOL* foregroundHandled) noexcept; + //INotificationHandler + HRESULT __stdcall OnRawNotificationReceived(unsigned int payloadLength, _In_ byte* payload, _In_ HSTRING /*correlationVector */) noexcept + { + BOOL foregroundHandled = false; + THROW_IF_FAILED(InvokeAll(payloadLength, payload, &foregroundHandled)); + THROW_HR_IF(E_NOT_VALID_STATE, !foregroundHandled); + + return S_OK; + } + + private: bool IsPackagedAppScenario(); bool IsBackgroundTaskBuilderAvailable(); diff --git a/test/TestApps/PushNotificationsDemoPackage/PushNotificationsDemoPackage.wapproj b/test/TestApps/PushNotificationsDemoPackage/PushNotificationsDemoPackage.wapproj index 34f6760256..ff8c5ec76b 100644 --- a/test/TestApps/PushNotificationsDemoPackage/PushNotificationsDemoPackage.wapproj +++ b/test/TestApps/PushNotificationsDemoPackage/PushNotificationsDemoPackage.wapproj @@ -48,6 +48,7 @@ True $(Platform) 0 + C:\Users\vemancha\Desktop\ Always From ec54a798cd8b0e956c68e7cc13928decef854c1c Mon Sep 17 00:00:00 2001 From: Venkata Sharath Chandra Manchala Date: Wed, 22 Sep 2021 08:34:41 -0700 Subject: [PATCH 02/11] Address comments --- .../PushNotificationChannel.cpp | 60 +++++++++++++++---- .../PushNotificationChannel.h | 10 +--- dev/PushNotifications/externs.h | 23 +++++++ .../PushNotificationsDemoApp/main.cpp | 2 +- .../PushNotificationsDemoPackage.wapproj | 1 - 5 files changed, 73 insertions(+), 23 deletions(-) diff --git a/dev/PushNotifications/PushNotificationChannel.cpp b/dev/PushNotifications/PushNotificationChannel.cpp index 47a621653b..5712e55892 100644 --- a/dev/PushNotifications/PushNotificationChannel.cpp +++ b/dev/PushNotifications/PushNotificationChannel.cpp @@ -13,7 +13,6 @@ #include "PushNotificationTelemetry.h" #include - namespace winrt::Windows { using namespace winrt::Windows::Networking::PushNotifications; @@ -107,18 +106,17 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation THROW_HR_IF_NULL(E_UNEXPECTED, m_channelInfo.channelUri.c_str()); - wchar_t appUserModelId[APPLICATION_USER_MODEL_ID_MAX_LENGTH] = {}; - UINT32 appUserModelIdSize{ ARRAYSIZE(appUserModelId) }; + wil::unique_cotaskmem_string appUserModelId; + THROW_IF_FAILED(GetAppUserModelId(appUserModelId)); - THROW_IF_FAILED(GetCurrentApplicationUserModelId(&appUserModelIdSize, appUserModelId)); + THROW_IF_FAILED(PushNotifications_RegisterNotificationSinkForFullTrustApplication(appUserModelId.get(), this)); - THROW_IF_FAILED(PushNotifications_RegisterNotificationSinkForFullTrustApplication(appUserModelId, this)); + winrt::event_token token = m_foregroundHandlers.add(handler); - return m_foregroundHandlers.add(handler); - + ++m_foregroundHandlerCount; + return token; } - // One more check if channelInfo struct is null - we should throw E_UNEXPECTED } else { @@ -149,14 +147,14 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation { auto lock = m_lock.lock_exclusive(); - wchar_t appUserModelId[APPLICATION_USER_MODEL_ID_MAX_LENGTH] = {}; - UINT32 appUserModelIdSize{ ARRAYSIZE(appUserModelId) }; + wil::unique_cotaskmem_string appUserModelId; + THROW_IF_FAILED(GetAppUserModelId(appUserModelId)); - THROW_IF_FAILED(GetCurrentApplicationUserModelId(&appUserModelIdSize, appUserModelId)); + THROW_IF_FAILED(PushNotifications_UnregisterNotificationSinkForFullTrustApplication(appUserModelId.get())); - THROW_IF_FAILED(PushNotifications_UnregisterNotificationSinkForFullTrustApplication(appUserModelId)); m_foregroundHandlers.remove(token); + --m_foregroundHandlerCount; } } else @@ -185,6 +183,44 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation } CATCH_RETURN() + HRESULT __stdcall PushNotificationChannel::OnRawNotificationReceived(unsigned int payloadLength, _In_ byte* payload, _In_ HSTRING /*correlationVector */) noexcept + { + try + { + BOOL foregroundHandled = false; + THROW_IF_FAILED(InvokeAll(payloadLength, payload, &foregroundHandled)); + THROW_HR(E_INVALIDARG); + } + catch(...) + { + //TODO: Capture the HRESULT in telemetry + winrt::com_array payloadArray{ payload, payload + (payloadLength * sizeof(uint8_t)) }; + std::string commandLine = "----WindowsAppRuntimePushServer:-Payload:\""; + commandLine.append(reinterpret_cast(payload), payloadLength); + commandLine.append("\""); + + wil::unique_cotaskmem_string processName; + THROW_IF_FAILED(GetCurrentProcessPath(processName)); + + const std::string processNameAsUtf8String = ConvertProcessNameToUtf8String(processName.get()); + + SHELLEXECUTEINFOA shellExecuteInfo{}; + shellExecuteInfo.cbSize = sizeof(SHELLEXECUTEINFOA); + shellExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_DOENVSUBST; + shellExecuteInfo.lpFile = processNameAsUtf8String.c_str(); + shellExecuteInfo.lpParameters = commandLine.c_str(); + + shellExecuteInfo.nShow = SW_NORMAL; + + if (!ShellExecuteExA(&shellExecuteInfo)) + { + THROW_IF_WIN32_ERROR(GetLastError()); + } + } + + return S_OK; + } + bool PushNotificationChannel::IsBackgroundTaskBuilderAvailable() { return winrt::Windows::ApiInformation::IsMethodPresent(L"Windows.ApplicationModel.Background.BackgroundTaskBuilder", L"SetTaskEntryPointClsid"); diff --git a/dev/PushNotifications/PushNotificationChannel.h b/dev/PushNotifications/PushNotificationChannel.h index 976b50e390..11ca548e96 100644 --- a/dev/PushNotifications/PushNotificationChannel.h +++ b/dev/PushNotifications/PushNotificationChannel.h @@ -31,15 +31,7 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation HRESULT __stdcall InvokeAll(_In_ ULONG length, _In_ byte* payload, _Out_ BOOL* foregroundHandled) noexcept; //INotificationHandler - HRESULT __stdcall OnRawNotificationReceived(unsigned int payloadLength, _In_ byte* payload, _In_ HSTRING /*correlationVector */) noexcept - { - BOOL foregroundHandled = false; - THROW_IF_FAILED(InvokeAll(payloadLength, payload, &foregroundHandled)); - THROW_HR_IF(E_NOT_VALID_STATE, !foregroundHandled); - - return S_OK; - } - + HRESULT __stdcall OnRawNotificationReceived(unsigned int payloadLength, _In_ byte* payload, _In_ HSTRING /*correlationVector */) noexcept; private: bool IsPackagedAppScenario(); diff --git a/dev/PushNotifications/externs.h b/dev/PushNotifications/externs.h index 090418cb8b..67bbb83b48 100644 --- a/dev/PushNotifications/externs.h +++ b/dev/PushNotifications/externs.h @@ -21,3 +21,26 @@ inline HRESULT GetCurrentProcessPath(wil::unique_cotaskmem_string& processName) return wil::GetModuleFileNameExW(GetCurrentProcess(), nullptr, processName); }; +inline HRESULT GetAppUserModelId(wil::unique_cotaskmem_string& appUserModelId) +{ + wchar_t appId[APPLICATION_USER_MODEL_ID_MAX_LENGTH] = {}; + UINT32 appIdSize{ ARRAYSIZE(appId) }; + + GetCurrentApplicationUserModelId(&appIdSize, appId); + + appUserModelId = wil::make_unique_string(appId); + + return S_OK; +} + +inline const std::string ConvertProcessNameToUtf8String(std::wstring processName) +{ + int size_needed = WideCharToMultiByte(CP_UTF8, 0, processName.c_str(), -1, NULL, 0, nullptr, nullptr); + THROW_LAST_ERROR_IF(size_needed == 0); + + // size_needed minus the null character + std::string utf8(size_needed - 1, 0); + int size = WideCharToMultiByte(CP_UTF8, 0, processName.c_str(), size_needed - 1, &utf8[0], size_needed - 1, nullptr, nullptr); + THROW_LAST_ERROR_IF(size == 0); + return utf8; +} diff --git a/test/TestApps/PushNotificationsDemoApp/main.cpp b/test/TestApps/PushNotificationsDemoApp/main.cpp index ccdf58a866..2ad3518336 100644 --- a/test/TestApps/PushNotificationsDemoApp/main.cpp +++ b/test/TestApps/PushNotificationsDemoApp/main.cpp @@ -18,7 +18,7 @@ winrt::Windows::Foundation::IAsyncOperation RequestChan // To obtain an AAD RemoteIdentifier for your app, // follow the instructions on https://docs.microsoft.com/azure/active-directory/develop/quickstart-register-app auto channelOperation = PushNotificationManager::CreateChannelAsync( - winrt::guid("ccd2ae3f-764f-4ae3-be45-9804761b28b2")); + winrt::guid("0160ee84-0c53-4851-9ff2-d7f5a87ed914")); // Setup the inprogress event handler channelOperation.Progress( diff --git a/test/TestApps/PushNotificationsDemoPackage/PushNotificationsDemoPackage.wapproj b/test/TestApps/PushNotificationsDemoPackage/PushNotificationsDemoPackage.wapproj index ff8c5ec76b..34f6760256 100644 --- a/test/TestApps/PushNotificationsDemoPackage/PushNotificationsDemoPackage.wapproj +++ b/test/TestApps/PushNotificationsDemoPackage/PushNotificationsDemoPackage.wapproj @@ -48,7 +48,6 @@ True $(Platform) 0 - C:\Users\vemancha\Desktop\ Always From c9510925130ed92ae24298c14fc92a2d65c69542 Mon Sep 17 00:00:00 2001 From: Venkata Sharath Chandra Manchala Date: Thu, 23 Sep 2021 09:30:17 -0700 Subject: [PATCH 03/11] Address comments2 --- .../PushNotificationChannel.cpp | 64 ++++++------------- dev/PushNotifications/externs.h | 56 ++++++++++++++-- 2 files changed, 69 insertions(+), 51 deletions(-) diff --git a/dev/PushNotifications/PushNotificationChannel.cpp b/dev/PushNotifications/PushNotificationChannel.cpp index 5712e55892..d8044318c2 100644 --- a/dev/PushNotifications/PushNotificationChannel.cpp +++ b/dev/PushNotifications/PushNotificationChannel.cpp @@ -12,6 +12,7 @@ #include "externs.h" #include "PushNotificationTelemetry.h" #include +#include namespace winrt::Windows { @@ -104,18 +105,18 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation { auto lock = m_lock.lock_exclusive(); - THROW_HR_IF_NULL(E_UNEXPECTED, m_channelInfo.channelUri.c_str()); + std::variant details{ m_channelInfo }; + THROW_HR_IF_NULL(E_UNEXPECTED, std::get_if(&details)); - wil::unique_cotaskmem_string appUserModelId; - THROW_IF_FAILED(GetAppUserModelId(appUserModelId)); - - THROW_IF_FAILED(PushNotifications_RegisterNotificationSinkForFullTrustApplication(appUserModelId.get(), this)); - - winrt::event_token token = m_foregroundHandlers.add(handler); + if (!m_foregroundHandlerCount++) + { + wil::unique_cotaskmem_string appUserModelId; + THROW_IF_FAILED(GetAppUserModelId(appUserModelId)); - ++m_foregroundHandlerCount; + THROW_IF_FAILED(PushNotifications_RegisterNotificationSinkForFullTrustApplication(appUserModelId.get(), this)); + } - return token; + return m_foregroundHandlers.add(handler); } } else @@ -147,14 +148,15 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation { auto lock = m_lock.lock_exclusive(); - wil::unique_cotaskmem_string appUserModelId; - THROW_IF_FAILED(GetAppUserModelId(appUserModelId)); + if (!--m_foregroundHandlerCount) + { + wil::unique_cotaskmem_string appUserModelId; + THROW_IF_FAILED(GetAppUserModelId(appUserModelId)); - THROW_IF_FAILED(PushNotifications_UnregisterNotificationSinkForFullTrustApplication(appUserModelId.get())); + THROW_IF_FAILED(PushNotifications_UnregisterNotificationSinkForFullTrustApplication(appUserModelId.get())); + } m_foregroundHandlers.remove(token); - - --m_foregroundHandlerCount; } } else @@ -185,37 +187,11 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation HRESULT __stdcall PushNotificationChannel::OnRawNotificationReceived(unsigned int payloadLength, _In_ byte* payload, _In_ HSTRING /*correlationVector */) noexcept { - try - { - BOOL foregroundHandled = false; - THROW_IF_FAILED(InvokeAll(payloadLength, payload, &foregroundHandled)); - THROW_HR(E_INVALIDARG); - } - catch(...) + BOOL foregroundHandled = false; + THROW_IF_FAILED(InvokeAll(payloadLength, payload, &foregroundHandled)); + if (!foregroundHandled) { - //TODO: Capture the HRESULT in telemetry - winrt::com_array payloadArray{ payload, payload + (payloadLength * sizeof(uint8_t)) }; - std::string commandLine = "----WindowsAppRuntimePushServer:-Payload:\""; - commandLine.append(reinterpret_cast(payload), payloadLength); - commandLine.append("\""); - - wil::unique_cotaskmem_string processName; - THROW_IF_FAILED(GetCurrentProcessPath(processName)); - - const std::string processNameAsUtf8String = ConvertProcessNameToUtf8String(processName.get()); - - SHELLEXECUTEINFOA shellExecuteInfo{}; - shellExecuteInfo.cbSize = sizeof(SHELLEXECUTEINFOA); - shellExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_DOENVSUBST; - shellExecuteInfo.lpFile = processNameAsUtf8String.c_str(); - shellExecuteInfo.lpParameters = commandLine.c_str(); - - shellExecuteInfo.nShow = SW_NORMAL; - - if (!ShellExecuteExA(&shellExecuteInfo)) - { - THROW_IF_WIN32_ERROR(GetLastError()); - } + ProtocolLaunchHelper(payloadLength, payload); } return S_OK; diff --git a/dev/PushNotifications/externs.h b/dev/PushNotifications/externs.h index 67bbb83b48..f3d418cb57 100644 --- a/dev/PushNotifications/externs.h +++ b/dev/PushNotifications/externs.h @@ -33,14 +33,56 @@ inline HRESULT GetAppUserModelId(wil::unique_cotaskmem_string& appUserModelId) return S_OK; } -inline const std::string ConvertProcessNameToUtf8String(std::wstring processName) +inline const std::wstring ConvertByteArrayToWideString(unsigned int payloadLength, byte* payload) { - int size_needed = WideCharToMultiByte(CP_UTF8, 0, processName.c_str(), -1, NULL, 0, nullptr, nullptr); - THROW_LAST_ERROR_IF(size_needed == 0); + int size = MultiByteToWideChar( + CP_UTF8, + 0, + reinterpret_cast(payload), + payloadLength, + nullptr, + 0); + THROW_LAST_ERROR_IF(size == 0); - // size_needed minus the null character - std::string utf8(size_needed - 1, 0); - int size = WideCharToMultiByte(CP_UTF8, 0, processName.c_str(), size_needed - 1, &utf8[0], size_needed - 1, nullptr, nullptr); + std::wstring payloadAsWideString(size, 0); + size = MultiByteToWideChar( + CP_UTF8, + 0, + reinterpret_cast(payload), + payloadLength, + &payloadAsWideString[0], + size); THROW_LAST_ERROR_IF(size == 0); - return utf8; + + return payloadAsWideString; +} + +inline void ProtocolLaunchHelper(unsigned int payloadLength, _In_ byte* payload) +{ + // Command line format: ----WindowsAppRuntimePushServer:-Payload:"" + std::wstring commandLine = L"----WindowsAppRuntimePushServer:-Payload:\""; + + // Escape special characters to follow command line standards for any app activation type in AppLifecycle + // (See AppInstance.cpp and Serialize() from other activation types) + std::wstring payloadAsWideString = ConvertByteArrayToWideString(payloadLength, payload); + auto payloadAsEscapedUriFormat = winrt::Windows::Foundation::Uri::EscapeComponent(payloadAsWideString.c_str()); + + commandLine.append(payloadAsEscapedUriFormat); + commandLine.append(L"\""); + + wil::unique_cotaskmem_string processName; + THROW_IF_FAILED(GetCurrentProcessPath(processName)); + + SHELLEXECUTEINFO shellExecuteInfo{}; + shellExecuteInfo.cbSize = sizeof(SHELLEXECUTEINFO); + shellExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_DOENVSUBST; + shellExecuteInfo.lpFile = processName.get(); + shellExecuteInfo.lpParameters = commandLine.c_str(); + + shellExecuteInfo.nShow = SW_NORMAL; + + if (!ShellExecuteEx(&shellExecuteInfo)) + { + THROW_IF_WIN32_ERROR(GetLastError()); + } } From 484fcaad54b3ea1c060215670ec3d82cc0152572 Mon Sep 17 00:00:00 2001 From: Venkata Sharath Chandra Manchala Date: Mon, 27 Sep 2021 07:44:36 -0700 Subject: [PATCH 04/11] Introduce PushNotificationsUtility.h/cpp --- .../PushNotificationChannel.cpp | 6 +- .../PushNotificationUtility.cpp | 59 +++++++++++++++++ .../PushNotificationUtility.h | 20 ++++++ .../PushNotifications.vcxitems | 2 + dev/PushNotifications/externs.h | 66 ------------------- 5 files changed, 83 insertions(+), 70 deletions(-) create mode 100644 dev/PushNotifications/PushNotificationUtility.cpp create mode 100644 dev/PushNotifications/PushNotificationUtility.h diff --git a/dev/PushNotifications/PushNotificationChannel.cpp b/dev/PushNotifications/PushNotificationChannel.cpp index d8044318c2..a7573ea8a4 100644 --- a/dev/PushNotifications/PushNotificationChannel.cpp +++ b/dev/PushNotifications/PushNotificationChannel.cpp @@ -12,7 +12,7 @@ #include "externs.h" #include "PushNotificationTelemetry.h" #include -#include +#include "PushNotificationUtility.h" namespace winrt::Windows { @@ -105,9 +105,6 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation { auto lock = m_lock.lock_exclusive(); - std::variant details{ m_channelInfo }; - THROW_HR_IF_NULL(E_UNEXPECTED, std::get_if(&details)); - if (!m_foregroundHandlerCount++) { wil::unique_cotaskmem_string appUserModelId; @@ -189,6 +186,7 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation { BOOL foregroundHandled = false; THROW_IF_FAILED(InvokeAll(payloadLength, payload, &foregroundHandled)); + if (!foregroundHandled) { ProtocolLaunchHelper(payloadLength, payload); diff --git a/dev/PushNotifications/PushNotificationUtility.cpp b/dev/PushNotifications/PushNotificationUtility.cpp new file mode 100644 index 0000000000..8a88770b3c --- /dev/null +++ b/dev/PushNotifications/PushNotificationUtility.cpp @@ -0,0 +1,59 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. +#include "pch.h" +#include "externs.h" +#include "PushNotificationUtility.h" + +const std::wstring ConvertByteArrayToWideString(unsigned int payloadLength, byte* payload) noexcept +{ + int size = MultiByteToWideChar( + CP_UTF8, + 0, + reinterpret_cast(payload), + payloadLength, + nullptr, + 0); + THROW_LAST_ERROR_IF(size == 0); + + std::wstring payloadAsWideString(size, 0); + size = MultiByteToWideChar( + CP_UTF8, + 0, + reinterpret_cast(payload), + payloadLength, + &payloadAsWideString[0], + size); + THROW_LAST_ERROR_IF(size == 0); + + return payloadAsWideString; +} + +void ProtocolLaunchHelper(unsigned int payloadLength, _In_ byte* payload) noexcept +{ + // Command line format: ----WindowsAppRuntimePushServer:-Payload:"" + std::wstring commandLine = L"----WindowsAppRuntimePushServer:-Payload:\""; + + // Escape special characters to follow command line standards for any app activation type in AppLifecycle + // (See AppInstance.cpp and Serialize() from other activation types) + std::wstring payloadAsWideString = ConvertByteArrayToWideString(payloadLength, payload); + auto payloadAsEscapedUriFormat = winrt::Windows::Foundation::Uri::EscapeComponent(payloadAsWideString.c_str()); + + commandLine.append(payloadAsEscapedUriFormat); + commandLine.append(L"\""); + + wil::unique_cotaskmem_string processName; + THROW_IF_FAILED(GetCurrentProcessPath(processName)); + + SHELLEXECUTEINFO shellExecuteInfo{}; + shellExecuteInfo.cbSize = sizeof(SHELLEXECUTEINFO); + shellExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_DOENVSUBST; + shellExecuteInfo.lpFile = processName.get(); + shellExecuteInfo.lpParameters = commandLine.c_str(); + + shellExecuteInfo.nShow = SW_NORMAL; + + if (!ShellExecuteEx(&shellExecuteInfo)) + { + THROW_IF_WIN32_ERROR(GetLastError()); + } +} diff --git a/dev/PushNotifications/PushNotificationUtility.h b/dev/PushNotifications/PushNotificationUtility.h new file mode 100644 index 0000000000..475b10a92f --- /dev/null +++ b/dev/PushNotifications/PushNotificationUtility.h @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +#pragma once +#include "pch.h" + +const std::wstring ConvertByteArrayToWideString(unsigned int payloadLength, byte* payload) noexcept; +void ProtocolLaunchHelper(unsigned int payloadLength, _In_ byte* payload) noexcept; + +inline HRESULT GetAppUserModelId(wil::unique_cotaskmem_string& appUserModelId) +{ + wchar_t appId[APPLICATION_USER_MODEL_ID_MAX_LENGTH] = {}; + UINT32 appIdSize{ ARRAYSIZE(appId) }; + + GetCurrentApplicationUserModelId(&appIdSize, appId); + + appUserModelId = wil::make_unique_string(appId); + + return S_OK; +} diff --git a/dev/PushNotifications/PushNotifications.vcxitems b/dev/PushNotifications/PushNotifications.vcxitems index 118587af45..7f6be3e805 100644 --- a/dev/PushNotifications/PushNotifications.vcxitems +++ b/dev/PushNotifications/PushNotifications.vcxitems @@ -23,10 +23,12 @@ + + diff --git a/dev/PushNotifications/externs.h b/dev/PushNotifications/externs.h index f3d418cb57..2217362df3 100644 --- a/dev/PushNotifications/externs.h +++ b/dev/PushNotifications/externs.h @@ -20,69 +20,3 @@ inline HRESULT GetCurrentProcessPath(wil::unique_cotaskmem_string& processName) { return wil::GetModuleFileNameExW(GetCurrentProcess(), nullptr, processName); }; - -inline HRESULT GetAppUserModelId(wil::unique_cotaskmem_string& appUserModelId) -{ - wchar_t appId[APPLICATION_USER_MODEL_ID_MAX_LENGTH] = {}; - UINT32 appIdSize{ ARRAYSIZE(appId) }; - - GetCurrentApplicationUserModelId(&appIdSize, appId); - - appUserModelId = wil::make_unique_string(appId); - - return S_OK; -} - -inline const std::wstring ConvertByteArrayToWideString(unsigned int payloadLength, byte* payload) -{ - int size = MultiByteToWideChar( - CP_UTF8, - 0, - reinterpret_cast(payload), - payloadLength, - nullptr, - 0); - THROW_LAST_ERROR_IF(size == 0); - - std::wstring payloadAsWideString(size, 0); - size = MultiByteToWideChar( - CP_UTF8, - 0, - reinterpret_cast(payload), - payloadLength, - &payloadAsWideString[0], - size); - THROW_LAST_ERROR_IF(size == 0); - - return payloadAsWideString; -} - -inline void ProtocolLaunchHelper(unsigned int payloadLength, _In_ byte* payload) -{ - // Command line format: ----WindowsAppRuntimePushServer:-Payload:"" - std::wstring commandLine = L"----WindowsAppRuntimePushServer:-Payload:\""; - - // Escape special characters to follow command line standards for any app activation type in AppLifecycle - // (See AppInstance.cpp and Serialize() from other activation types) - std::wstring payloadAsWideString = ConvertByteArrayToWideString(payloadLength, payload); - auto payloadAsEscapedUriFormat = winrt::Windows::Foundation::Uri::EscapeComponent(payloadAsWideString.c_str()); - - commandLine.append(payloadAsEscapedUriFormat); - commandLine.append(L"\""); - - wil::unique_cotaskmem_string processName; - THROW_IF_FAILED(GetCurrentProcessPath(processName)); - - SHELLEXECUTEINFO shellExecuteInfo{}; - shellExecuteInfo.cbSize = sizeof(SHELLEXECUTEINFO); - shellExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_DOENVSUBST; - shellExecuteInfo.lpFile = processName.get(); - shellExecuteInfo.lpParameters = commandLine.c_str(); - - shellExecuteInfo.nShow = SW_NORMAL; - - if (!ShellExecuteEx(&shellExecuteInfo)) - { - THROW_IF_WIN32_ERROR(GetLastError()); - } -} From 45cdd39a1ee06c1fb4c27b7b0bdfce1614c4daf5 Mon Sep 17 00:00:00 2001 From: Venkata Sharath Chandra Manchala Date: Tue, 28 Sep 2021 08:33:02 -0700 Subject: [PATCH 05/11] Address comments --- dev/PushNotifications/PushNotificationUtility.cpp | 4 ++-- dev/PushNotifications/PushNotificationUtility.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dev/PushNotifications/PushNotificationUtility.cpp b/dev/PushNotifications/PushNotificationUtility.cpp index 8a88770b3c..2a12d21e1b 100644 --- a/dev/PushNotifications/PushNotificationUtility.cpp +++ b/dev/PushNotifications/PushNotificationUtility.cpp @@ -4,7 +4,7 @@ #include "externs.h" #include "PushNotificationUtility.h" -const std::wstring ConvertByteArrayToWideString(unsigned int payloadLength, byte* payload) noexcept +const std::wstring ConvertByteArrayToWideString(unsigned int payloadLength, _In_reads_(payloadLength) byte* payload) { int size = MultiByteToWideChar( CP_UTF8, @@ -28,7 +28,7 @@ const std::wstring ConvertByteArrayToWideString(unsigned int payloadLength, byte return payloadAsWideString; } -void ProtocolLaunchHelper(unsigned int payloadLength, _In_ byte* payload) noexcept +void ProtocolLaunchHelper(unsigned int payloadLength, _In_reads_(payloadLength) byte* payload) { // Command line format: ----WindowsAppRuntimePushServer:-Payload:"" std::wstring commandLine = L"----WindowsAppRuntimePushServer:-Payload:\""; diff --git a/dev/PushNotifications/PushNotificationUtility.h b/dev/PushNotifications/PushNotificationUtility.h index 475b10a92f..fba670b964 100644 --- a/dev/PushNotifications/PushNotificationUtility.h +++ b/dev/PushNotifications/PushNotificationUtility.h @@ -4,8 +4,8 @@ #pragma once #include "pch.h" -const std::wstring ConvertByteArrayToWideString(unsigned int payloadLength, byte* payload) noexcept; -void ProtocolLaunchHelper(unsigned int payloadLength, _In_ byte* payload) noexcept; +const std::wstring ConvertByteArrayToWideString(unsigned int payloadLength, _In_reads_(payloadLength) byte* payload); +void ProtocolLaunchHelper(unsigned int payloadLength, _In_reads_(payloadLength) byte* payload); inline HRESULT GetAppUserModelId(wil::unique_cotaskmem_string& appUserModelId) { From 537af31ba4b6adf8bb5e9f33d2f61b5b1588a6d6 Mon Sep 17 00:00:00 2001 From: Venkata Sharath Chandra Manchala Date: Wed, 29 Sep 2021 09:22:24 -0700 Subject: [PATCH 06/11] Mark PushNotificationUtility.h file to be shared across the PushNotifications directory --- dev/PushNotifications/PushNotifications.vcxitems | 5 ++++- .../PushNotificationsLongRunningTask.vcxproj | 12 ++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/dev/PushNotifications/PushNotifications.vcxitems b/dev/PushNotifications/PushNotifications.vcxitems index 7f6be3e805..acf9dce25e 100644 --- a/dev/PushNotifications/PushNotifications.vcxitems +++ b/dev/PushNotifications/PushNotifications.vcxitems @@ -28,7 +28,6 @@ - @@ -38,6 +37,10 @@ + + + + \ No newline at end of file diff --git a/dev/PushNotifications/PushNotificationsLongRunningTask/PushNotificationsLongRunningTask.vcxproj b/dev/PushNotifications/PushNotificationsLongRunningTask/PushNotificationsLongRunningTask.vcxproj index 7f1a18296a..af3d8fe24b 100644 --- a/dev/PushNotifications/PushNotificationsLongRunningTask/PushNotificationsLongRunningTask.vcxproj +++ b/dev/PushNotifications/PushNotificationsLongRunningTask/PushNotificationsLongRunningTask.vcxproj @@ -123,7 +123,7 @@ WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) true stdcpp17 - %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)..\WindowsAppSDK_BootstrapDLL + %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL Windows @@ -141,7 +141,7 @@ true stdcpp17 Guard - %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)..\WindowsAppSDK_BootstrapDLL + %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL Windows @@ -158,7 +158,7 @@ _DEBUG;_WINDOWS;%(PreprocessorDefinitions) true stdcpp17 - %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)..\WindowsAppSDK_BootstrapDLL + %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL Windows @@ -176,7 +176,7 @@ true stdcpp17 Guard - %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)..\WindowsAppSDK_BootstrapDLL + %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL Windows @@ -193,7 +193,7 @@ _DEBUG;_WINDOWS;%(PreprocessorDefinitions) true stdcpp20 - %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)..\WindowsAppSDK_BootstrapDLL + %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL Windows @@ -211,7 +211,7 @@ true Guard stdcpp20 - %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)..\WindowsAppSDK_BootstrapDLL + %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory);$(OutDir)..\PushNotificationsLongRunningTask.ProxyStub;$(OutDir)\..\WindowsAppRuntime_DLL;$(OutDir)..\WindowsAppSDK_BootstrapDLL Windows From 6a3aa4d48b979ee3543c5ca674198a564fc055ab Mon Sep 17 00:00:00 2001 From: Venkata Sharath Chandra Manchala Date: Fri, 1 Oct 2021 05:56:43 -0700 Subject: [PATCH 07/11] Add try/catch around onRawNotificationRecveived --- dev/PushNotifications/PushNotificationChannel.cpp | 11 ++++++++--- dev/PushNotifications/PushNotificationChannel.h | 2 +- dev/PushNotifications/PushNotificationUtility.cpp | 4 ++-- dev/PushNotifications/PushNotificationUtility.h | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/dev/PushNotifications/PushNotificationChannel.cpp b/dev/PushNotifications/PushNotificationChannel.cpp index a7573ea8a4..17a59e25e1 100644 --- a/dev/PushNotifications/PushNotificationChannel.cpp +++ b/dev/PushNotifications/PushNotificationChannel.cpp @@ -103,9 +103,10 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation } else { + // The channelUri is directly obtained when we request Channel from UDK using RemoteId auto lock = m_lock.lock_exclusive(); - if (!m_foregroundHandlerCount++) + if (!m_foregroundHandlerCount) { wil::unique_cotaskmem_string appUserModelId; THROW_IF_FAILED(GetAppUserModelId(appUserModelId)); @@ -113,6 +114,8 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation THROW_IF_FAILED(PushNotifications_RegisterNotificationSinkForFullTrustApplication(appUserModelId.get(), this)); } + ++m_foregroundHandlerCount; + return m_foregroundHandlers.add(handler); } } @@ -145,7 +148,7 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation { auto lock = m_lock.lock_exclusive(); - if (!--m_foregroundHandlerCount) + if (m_foregroundHandlerCount == 1) { wil::unique_cotaskmem_string appUserModelId; THROW_IF_FAILED(GetAppUserModelId(appUserModelId)); @@ -154,6 +157,7 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation } m_foregroundHandlers.remove(token); + --m_foregroundHandlerCount; } } else @@ -182,7 +186,7 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation } CATCH_RETURN() - HRESULT __stdcall PushNotificationChannel::OnRawNotificationReceived(unsigned int payloadLength, _In_ byte* payload, _In_ HSTRING /*correlationVector */) noexcept + HRESULT __stdcall PushNotificationChannel::OnRawNotificationReceived(unsigned int payloadLength, _In_ byte* payload, _In_ HSTRING /*correlationVector */) noexcept try { BOOL foregroundHandled = false; THROW_IF_FAILED(InvokeAll(payloadLength, payload, &foregroundHandled)); @@ -194,6 +198,7 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation return S_OK; } + CATCH_RETURN(); bool PushNotificationChannel::IsBackgroundTaskBuilderAvailable() { diff --git a/dev/PushNotifications/PushNotificationChannel.h b/dev/PushNotifications/PushNotificationChannel.h index 11ca548e96..4a6b00c933 100644 --- a/dev/PushNotifications/PushNotificationChannel.h +++ b/dev/PushNotifications/PushNotificationChannel.h @@ -30,7 +30,7 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation // IWpnForegroundSink HRESULT __stdcall InvokeAll(_In_ ULONG length, _In_ byte* payload, _Out_ BOOL* foregroundHandled) noexcept; - //INotificationHandler + // INotificationHandler HRESULT __stdcall OnRawNotificationReceived(unsigned int payloadLength, _In_ byte* payload, _In_ HSTRING /*correlationVector */) noexcept; private: diff --git a/dev/PushNotifications/PushNotificationUtility.cpp b/dev/PushNotifications/PushNotificationUtility.cpp index 2a12d21e1b..d0cf2408f3 100644 --- a/dev/PushNotifications/PushNotificationUtility.cpp +++ b/dev/PushNotifications/PushNotificationUtility.cpp @@ -4,7 +4,7 @@ #include "externs.h" #include "PushNotificationUtility.h" -const std::wstring ConvertByteArrayToWideString(unsigned int payloadLength, _In_reads_(payloadLength) byte* payload) +std::wstring Utf8BytesToWideString(unsigned int payloadLength, _In_reads_(payloadLength) byte* payload) { int size = MultiByteToWideChar( CP_UTF8, @@ -35,7 +35,7 @@ void ProtocolLaunchHelper(unsigned int payloadLength, _In_reads_(payloadLength) // Escape special characters to follow command line standards for any app activation type in AppLifecycle // (See AppInstance.cpp and Serialize() from other activation types) - std::wstring payloadAsWideString = ConvertByteArrayToWideString(payloadLength, payload); + std::wstring payloadAsWideString = Utf8BytesToWideString(payloadLength, payload); auto payloadAsEscapedUriFormat = winrt::Windows::Foundation::Uri::EscapeComponent(payloadAsWideString.c_str()); commandLine.append(payloadAsEscapedUriFormat); diff --git a/dev/PushNotifications/PushNotificationUtility.h b/dev/PushNotifications/PushNotificationUtility.h index fba670b964..dd6c6e4bc9 100644 --- a/dev/PushNotifications/PushNotificationUtility.h +++ b/dev/PushNotifications/PushNotificationUtility.h @@ -12,7 +12,7 @@ inline HRESULT GetAppUserModelId(wil::unique_cotaskmem_string& appUserModelId) wchar_t appId[APPLICATION_USER_MODEL_ID_MAX_LENGTH] = {}; UINT32 appIdSize{ ARRAYSIZE(appId) }; - GetCurrentApplicationUserModelId(&appIdSize, appId); + RETURN_IF_FAILED(GetCurrentApplicationUserModelId(&appIdSize, appId)); appUserModelId = wil::make_unique_string(appId); From d2f6f383ded28e0a021a3b485c66a49b2c22edcf Mon Sep 17 00:00:00 2001 From: Venkata Sharath Chandra Manchala Date: Fri, 1 Oct 2021 10:17:49 -0700 Subject: [PATCH 08/11] Set foregroundhandled to true by default --- dev/PushNotifications/PushNotificationChannel.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/PushNotifications/PushNotificationChannel.cpp b/dev/PushNotifications/PushNotificationChannel.cpp index 17a59e25e1..fb853183f1 100644 --- a/dev/PushNotifications/PushNotificationChannel.cpp +++ b/dev/PushNotifications/PushNotificationChannel.cpp @@ -188,7 +188,7 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation HRESULT __stdcall PushNotificationChannel::OnRawNotificationReceived(unsigned int payloadLength, _In_ byte* payload, _In_ HSTRING /*correlationVector */) noexcept try { - BOOL foregroundHandled = false; + BOOL foregroundHandled = true; THROW_IF_FAILED(InvokeAll(payloadLength, payload, &foregroundHandled)); if (!foregroundHandled) From 3b09702e6d10ce5ab8273c32f1c7d9c8912468fc Mon Sep 17 00:00:00 2001 From: Venkata Sharath Chandra Manchala Date: Tue, 5 Oct 2021 08:59:57 -0700 Subject: [PATCH 09/11] Address comments for getappusermodelId API --- dev/PushNotifications/PushNotificationChannel.cpp | 6 ++---- dev/PushNotifications/PushNotificationUtility.cpp | 12 ++++++++++++ dev/PushNotifications/PushNotificationUtility.h | 13 +------------ 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/dev/PushNotifications/PushNotificationChannel.cpp b/dev/PushNotifications/PushNotificationChannel.cpp index fb853183f1..2b36467e98 100644 --- a/dev/PushNotifications/PushNotificationChannel.cpp +++ b/dev/PushNotifications/PushNotificationChannel.cpp @@ -108,8 +108,7 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation if (!m_foregroundHandlerCount) { - wil::unique_cotaskmem_string appUserModelId; - THROW_IF_FAILED(GetAppUserModelId(appUserModelId)); + auto appUserModelId = GetAppUserModelId(); THROW_IF_FAILED(PushNotifications_RegisterNotificationSinkForFullTrustApplication(appUserModelId.get(), this)); } @@ -150,8 +149,7 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation if (m_foregroundHandlerCount == 1) { - wil::unique_cotaskmem_string appUserModelId; - THROW_IF_FAILED(GetAppUserModelId(appUserModelId)); + auto appUserModelId = GetAppUserModelId(); THROW_IF_FAILED(PushNotifications_UnregisterNotificationSinkForFullTrustApplication(appUserModelId.get())); } diff --git a/dev/PushNotifications/PushNotificationUtility.cpp b/dev/PushNotifications/PushNotificationUtility.cpp index d0cf2408f3..65470bde04 100644 --- a/dev/PushNotifications/PushNotificationUtility.cpp +++ b/dev/PushNotifications/PushNotificationUtility.cpp @@ -4,6 +4,18 @@ #include "externs.h" #include "PushNotificationUtility.h" +wil::unique_cotaskmem_string GetAppUserModelId() +{ + wchar_t appId[APPLICATION_USER_MODEL_ID_MAX_LENGTH] = {}; + UINT32 appIdSize{ ARRAYSIZE(appId) }; + + const HRESULT hr = ::GetCurrentApplicationUserModelId(&appIdSize, appId); + + THROW_HR_IF(hr, hr != HRESULT_FROM_WIN32(ERROR_SUCCESS)); + + return wil::make_unique_string(appId); +} + std::wstring Utf8BytesToWideString(unsigned int payloadLength, _In_reads_(payloadLength) byte* payload) { int size = MultiByteToWideChar( diff --git a/dev/PushNotifications/PushNotificationUtility.h b/dev/PushNotifications/PushNotificationUtility.h index dd6c6e4bc9..69475daa1d 100644 --- a/dev/PushNotifications/PushNotificationUtility.h +++ b/dev/PushNotifications/PushNotificationUtility.h @@ -6,15 +6,4 @@ const std::wstring ConvertByteArrayToWideString(unsigned int payloadLength, _In_reads_(payloadLength) byte* payload); void ProtocolLaunchHelper(unsigned int payloadLength, _In_reads_(payloadLength) byte* payload); - -inline HRESULT GetAppUserModelId(wil::unique_cotaskmem_string& appUserModelId) -{ - wchar_t appId[APPLICATION_USER_MODEL_ID_MAX_LENGTH] = {}; - UINT32 appIdSize{ ARRAYSIZE(appId) }; - - RETURN_IF_FAILED(GetCurrentApplicationUserModelId(&appIdSize, appId)); - - appUserModelId = wil::make_unique_string(appId); - - return S_OK; -} +wil::unique_cotaskmem_string GetAppUserModelId(); From 35b99481ff044137c2e2fb0f22b2162cda80ee0e Mon Sep 17 00:00:00 2001 From: Venkata Sharath Chandra Manchala Date: Tue, 5 Oct 2021 09:17:46 -0700 Subject: [PATCH 10/11] Address nits --- dev/PushNotifications/PushNotificationChannel.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/PushNotifications/PushNotificationChannel.h b/dev/PushNotifications/PushNotificationChannel.h index 4a6b00c933..1a1595acd1 100644 --- a/dev/PushNotifications/PushNotificationChannel.h +++ b/dev/PushNotifications/PushNotificationChannel.h @@ -5,7 +5,7 @@ #include "Microsoft.Windows.PushNotifications.PushNotificationChannel.g.h" #include #include "winrt/Windows.Networking.PushNotifications.h" -#include +#include #include "externs.h" namespace winrt::Microsoft::Windows::PushNotifications::implementation From a3d70bc9ade18a0c6f0f1a189344c052179ab0f2 Mon Sep 17 00:00:00 2001 From: Venkata Sharath Chandra Manchala Date: Tue, 5 Oct 2021 09:42:47 -0700 Subject: [PATCH 11/11] Adress one more nit --- dev/PushNotifications/PushNotificationUtility.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dev/PushNotifications/PushNotificationUtility.cpp b/dev/PushNotifications/PushNotificationUtility.cpp index 65470bde04..081800b8fe 100644 --- a/dev/PushNotifications/PushNotificationUtility.cpp +++ b/dev/PushNotifications/PushNotificationUtility.cpp @@ -9,9 +9,7 @@ wil::unique_cotaskmem_string GetAppUserModelId() wchar_t appId[APPLICATION_USER_MODEL_ID_MAX_LENGTH] = {}; UINT32 appIdSize{ ARRAYSIZE(appId) }; - const HRESULT hr = ::GetCurrentApplicationUserModelId(&appIdSize, appId); - - THROW_HR_IF(hr, hr != HRESULT_FROM_WIN32(ERROR_SUCCESS)); + THROW_IF_FAILED(::GetCurrentApplicationUserModelId(&appIdSize, appId)); return wil::make_unique_string(appId); }