I am trying out the Windows Notification Service (WNS) to have push notifications for my Windows application. But I am facing an issue while creating the token. What is wrong here? (Trying to use C++ WinRT APIs).
In application where I am using Windows AppSDK:
This is the below function where I am requesting the Channel URI.
winrt::Windows::Foundation::IAsyncOperation<PushNotificationChannel> RequestChannelAsync()
{
// remote Id is the Azure AppId
auto channelOperation{ PushNotificationManager::Default().CreateChannelAsync(remoteId) };
// Setup the in-progress event handler
channelOperation.Progress(
[](auto&& sender, auto&& args)
{
if (args.status == PushNotificationChannelStatus::InProgress)
{
// This is basically a noop since it isn't really an error state
std::cout << "\nWNS Channel URI request is in progress." << std::endl;
}
else if (args.status == PushNotificationChannelStatus::InProgressRetry)
{
}
});
auto result{ co_await channelOperation };
if (result.Status() == PushNotificationChannelStatus::CompletedSuccess)
{
auto channel{ result.Channel() };
std::cout << "\nWNS Channel URI: " << winrt::to_string(channel.Uri().ToString()) << std::endl;
// It's the caller's responsibility to keep the channel alive
co_return channel;
}
else if (result.Status() == PushNotificationChannelStatus::CompletedFailure)
{
co_return nullptr;
}
else
{
co_return nullptr;
}
}
The channelStatus remains in Started. It doesn't print the line 'In Progress' and comes out of the loop. Why this is happening?
Edit
The channel URI gets request in Progress. But, after even waiting for more than 10 minutes, I am not able to reach at any of the else if condition where I am checking the result.
But it goes inside this condition of below if. Am I doing anything wrong?
winrt::Microsoft::Windows::PushNotifications::PushNotificationChannel
RequestChannel()
{
auto task{ RequestChannelAsync() };
if (task.wait_for(std::chrono::minutes(10)) != AsyncStatus::Completed)
{
IAsyncInfo infor = task.as<IAsyncInfo> ();
uint32_t id = infor.Id ();
HRESULT errorCode = infor.ErrorCode ();
AsyncStatus currentStatus = infor.Status ();
task.Cancel();
return nullptr;
}
auto result{ task.GetResults() };
return result;
}