我正在编写一个依赖通知工作的Windows桌面应用。但是,当我收到通知时,通道上的事件处理程序代码PushNotificationReceived似乎没有实际触发。在将uri发送到我的服务器之前,调用以下代码来获取通道:
internal async Task<PushNotificationChannel> GetChannel()
{
PushNotificationChannel pnc;
try
{
pnc = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
if (_channel == null || !pnc.Uri.Equals(_channel.Uri))
{
_channel = pnc;
_channel.PushNotificationReceived += OnPushNotificationReceived;
Debug.WriteLine(_channel.Uri);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
_channel = null;
}
dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
return _channel;
}
这样,无论何时创建或更新频道(通过不同的频道uri),它都应该将新频道的PushNotificationReceived事件分配给以下内容(基本上从msdn的示例中解除):
void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
string typeString = String.Empty;
string notificationContent = String.Empty;
switch (e.NotificationType)
{
//
//other notification types omitted for brevity
//
case PushNotificationType.Toast:
notificationContent = e.ToastNotification.Content.GetXml();
typeString = "Toast";
// Setting the cancel property prevents the notification from being delivered. It's especially important to do this for toasts:
// if your application is already on the screen, there's no need to display a toast from push notifications.
e.Cancel = true;
break;
}
Debug.WriteLine("Received notification, with payload: {0}", notificationContent);
string text = "Received a " + typeString + " notification, containing: " + notificationContent;
var ignored = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
MainPage.Current.ClearBanner();
});
}
重要的是,“MainPage.Current”是对应用程序主页面的引用,作为静态变量。清晰的横幅线只是从主页面上删除了一个粉红色的横幅(只是想让一些简单的工作开始)。
但是,代码似乎永远不会触发(没有调试语句,粉红色横幅仍然存在)。我成功获得了Toast通知,点击它会将焦点设置到我的应用程序,所以它肯定不会出错。
我做错了什么或者某种方式调试通知本身?