本机 C++ 中的 CreatePushNotificationChannelForApplicationAsync

2024-04-18

我正在尝试在本机 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 asyncOp

    return 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 包标识。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

本机 C++ 中的 CreatePushNotificationChannelForApplicationAsync 的相关文章

随机推荐