本机C ++中的CreatePushNotificationChannelForApplicationAsync

【本机C ++中的CreatePushNotificationChannelForApplicationAsync】丈夫欲遂平生志,一载寒窗一举汤。这篇文章主要讲述本机C ++中的CreatePushNotificationChannelForApplicationAsync相关的知识,希望能为你提供帮助。
我正在尝试在本机C ++代码中使用Windows推送通知。但我在实施方面很困难。我打电话给CreatePushNotificationChannelForApplicationAsync,但它返回HRESULT_FROM_WIN32(ERROR_NOT_FOUND) : Element not found.
我的操作系统是Win10,我使用的是Visual Studio 2015。
这是我的代码:

#include < wrl.h> #include < windows.networking.pushnotifications.h> #include < ppltasks.h> #pragma comment(lib, "runtimeobject.lib")using namespace ABI::Windows::Foundation; using namespace Microsoft::WRL; using namespace Microsoft::WRL::Wrappers; using namespace ABI::Windows::Networking::PushNotifications; using namespace concurrency; int main(char* argv[], int argc) { RoInitializeWrapper init(RO_INIT_MULTITHREADED); if ( FAILED(init) ) return 0; ComPtr< IPushNotificationChannelManagerStatics> channelManager; HRESULT hr = GetActivationFactory(HStringReference(L"Windows.Networking.PushNotifications.PushNotificationChannelManager").Get(), & channelManager); if ( FAILED(hr) ) return 0; IAsyncOperation< PushNotificationChannel*> * asyncOp; hr = channelManager-> CreatePushNotificationChannelForApplicationAsync(& asyncOp); // return HRESULT_FROM_WIN32(ERROR_NOT_FOUND)if ( FAILED(hr) ) return 0; // create task to obtain uri from asyncOpreturn 0; }

另一方面,它在WinRT中非常简单。
namespace WnsPushAPI { public ref class WnsWrapper sealed { public:void ObtainUri() { IAsyncOperation< PushNotificationChannel^> ^ channelOperation = PushNotificationChannelManager::CreatePushNotificationChannelForApplicationAsync(); auto channelTask = create_task(channelOperation); channelTask.then([this](PushNotificationChannel^ channel) { // ... Save URI for latter use. channel-> Uri-> Data() channel-> PushNotificationReceived += ref new TypedEventHandler< PushNotificationChannel^, PushNotificationReceivedEventArgs^> (this, & WnsWrapper::OnNotify); }, task_continuation_context::use_current()); }void OnNotify(PushNotificationChannel^ sender, PushNotificationReceivedEventArgs^ e) { // do something }; }; }

我还使用/d1ZWtokens编译器开关检查生成的代码,但我没有找到任何有用的东西。用于本机C ++的推送通知的文档编写得很糟糕,我在本机C ++中找不到和示例。
答案你的代码没问题。问题在于您只能注册来自Windows应用商店应用的推送通知。您无法从桌面应用程序执行此操作。该错误来自系统无法找到您的AppX包标识的事实。
另一答案@elanius你解决了这个问题吗?
ComPtr< IPushNotificationChannelManagerStatics> channelManager = NULL; HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Networking_PushNotifications_PushNotificationChannelManager).Get(), & channelManager); IAsyncOperation< PushNotificationChannel*> * asyncOp; hr = channelManager-> CreatePushNotificationChannelForApplicationAsync(& asyncOp);

channelManager:得到“没有为wpnapps.dll加载符号”。和asyncOp是NULL。
另一答案尝试从我的桌面应用程序使用WNS时,我也遇到了同样的HRESULT_FROM_WIN32(ERROR_NOT_FOUND)错误。万一有人也面临这个问题,我想提一下CreatePushNotificationChannelForApplicationAsync从桌面桥上工作得很好。这是我使用的代码。请注意它是基于winrt的,我认为基于WRL的代码也可以正常工作,只需要使用Desktop Bridge包装桌面应用程序。
winrt::init_apartment(); PushNotificationChannelManagerForUser manager = PushNotificationChannelManager::GetDefault(); IAsyncOperation< PushNotificationChannel> async = manager.CreatePushNotificationChannelForApplicationAsync(); async.Completed([](auto & & result, auto & & status) { auto channel = result.GetResults(); });


    推荐阅读