2017-02-20 1 views
1

Ich versuche, Windows Push-Benachrichtigungen in systemeigenem C++ - Code zu verwenden. Aber ich kämpfe mit der Umsetzung. Ich rufe CreatePushNotificationChannelForApplicationAsync aber es gibt HRESULT_FROM_WIN32(ERROR_NOT_FOUND) : Element not found.CreatePushNotificationChannelForApplicationAsync in systemeigenem C++

Mein OS ist Win10 und ich verwende Visual Studio 2015

Hier ist mein Code:

#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; 
} 

Auf der anderen Seite ist es ziemlich einfach 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 
     }; 
    }; 
} 

Ich habe auch generierten Code mit /d1ZWtokens Compiler-Schalter, aber ich habe nichts nützlich. Dokumentation für Push-Benachrichtigung für natives C++ ist wirklich schlecht geschrieben und ich konnte nicht finden und Beispiel in nativem C++.

Antwort

0

Ihr Code ist in Ordnung. Das Problem liegt darin, dass Sie sich nur für Push-Benachrichtigungen von Windows Store-Anwendungen registrieren können. Sie können das nicht von Desktop-Anwendungen aus tun. Der Fehler rührt von der Tatsache her, dass das System Ihre AppX-Paketidentität nicht finden kann.

+0

So muss es AppX-Identität für meine Desktop-App einrichten. Wenn ich UWP-App mit C++/CX erstelle, kann ich diese Identität in 'Package.appxmanifest' sehen. Ich muss herausfinden, wie ich diese Identität in meine App verschieben kann. Am Ende ist C++/CX auch nur EXE-Datei, aber irgendwie ist es registriert oder enthält gültige Identität für Win Store. – elanius

Verwandte Themen