c++ - native C++ 中的 CreatePushNotificationChannelForApplicationAsync

标签 c++ push-notification windows-runtime

我正在尝试在 native C++ 代码中使用 Windows 推送通知。但我在实现方面遇到了困难。我正在调用 CreatePushNotificationChannelForApplicationAsync 但它返回 HRESULT_FROM_WIN32(ERROR_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 编译器开关检查了生成的代码,但我没有发现任何有用的东西。 native C++ 推送通知的文档写得非常糟糕,我找不到 native C++ 中的示例。

最佳答案

您的代码没问题。问题在于您只能注册来自 Windows 应用商店应用程序的推送通知。您不能从桌面应用程序执行此操作。错误是因为系统找不到您的 AppX 包标识。

关于c++ - native C++ 中的 CreatePushNotificationChannelForApplicationAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42343512/

相关文章:

windows-phone-7 - 有人对 ShellTileSchedule 有好运吗?

c# - 从 WRL COM 组件获取托管回调

c# - 仅包含接口(interface)的 WinRT 组件 DLL?

c++ - (C++) "No operator ">>"matches these operands"枚举

c++ - RegOpenKeyEx 返回 ERROR_SUCCESS 但 RegSetValueEx 总是返回 ERROR_ACCESS_DENIED

java 可扩展的电子邮件通知应用程序

c# - 在显示进度环 Winrt C# 应用程序时禁用 UI

c++ - 全局 vector 和 Push_Back

c++ - clang 和 gcc 中的这个警告似乎不正确

https - 使用 httpsurlconnection 类的 urbanairship 出现 401 错误