本机 C++ 中的 CreatePushNotificationChannelForApplicationAsync [英] CreatePushNotificationChannelForApplicationAsync in native C++

查看:73
本文介绍了本机 C++ 中的 CreatePushNotificationChannelForApplicationAsync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在本机 C++ 代码中使用 Windows 推送通知.但我在实施方面很挣扎.我正在调用 CreatePushNotificationChannelForApplicationAsync 但它返回HRESULT_FROM_WIN32(ERROR_NOT_FOUND):未找到元素.

I'm trying to use windows push notifications in native C++ code. But I struggle with implementation. I'm calling CreatePushNotificationChannelForApplicationAsync but it returns HRESULT_FROM_WIN32(ERROR_NOT_FOUND) : Element not found.

我的操作系统是 Win10,我使用的是 Visual Studio 2015.

My OS is Win10 and I use 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 中非常简单.

On the other hand it is pretty straightforward in 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++ 中的示例.

I also checked generated code with /d1ZWtokens compiler switch but I didn't find anything useful. Documentation for push notification for native C++ is really poorly written and I couldn't find and example in native C++.

推荐答案

你的代码没问题.问题在于您只能注册来自 Windows 应用商店应用程序的推送通知.您不能从桌面应用程序中做到这一点.错误来自于系统找不到您的 AppX 包标识.

Your code is okay. The problem lies in the fact that you can only register for push notifications from Windows Store applications. You cannot do that from desktop applications. The error comes from the fact that the system cannot find your AppX package identity.

这篇关于本机 C++ 中的 CreatePushNotificationChannelForApplicationAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆