C# 如何让PushNotificationChannel.PushNotificationReceived事件处理程序截获通知?
理想的行为是拦截应用程序中的原始通知(无需后台任务或用户放弃宝贵的后台任务)。根据上的示例代码,我创建了一个事件处理程序来截获原始通知。在我创建的一个应用程序中,使用Azure移动服务通过WNS发送通知,事件处理程序被触发,效果非常好 但在第二个应用程序中,它不起作用。通知似乎很好。也就是说,当我的web服务让WNS发送Toast通知时,它就会显示出来。此外,我还可以调试/跟踪原始通知,它们似乎也可以创建。有什么我错过的场景吗 我通过Fiddler看到请求和结果,但可能只是因为我在本地运行MVC4.0WebAPI项目???请求如下所示:C# 如何让PushNotificationChannel.PushNotificationReceived事件处理程序截获通知?,c#,windows-8,notifications,C#,Windows 8,Notifications,理想的行为是拦截应用程序中的原始通知(无需后台任务或用户放弃宝贵的后台任务)。根据上的示例代码,我创建了一个事件处理程序来截获原始通知。在我创建的一个应用程序中,使用Azure移动服务通过WNS发送通知,事件处理程序被触发,效果非常好 但在第二个应用程序中,它不起作用。通知似乎很好。也就是说,当我的web服务让WNS发送Toast通知时,它就会显示出来。此外,我还可以调试/跟踪原始通知,它们似乎也可以创建。有什么我错过的场景吗 我通过Fiddler看到请求和结果,但可能只是因为我在本地运行MVC
{Channel URI: https://bn1.notify.windows.com/?token=AgYAAACuGgtx...
TTL: -
Cache: no
Request for status: no
Tag:
Priority: Normal
Token retry count: 0
really
}
private async void OnPushNotification(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
String notificationContent = String.Empty;
switch (e.NotificationType)
{
case PushNotificationType.Badge:
case PushNotificationType.Tile:
case PushNotificationType.Toast:
notificationContent = e.TileNotification.Content.GetXml();
break;
case PushNotificationType.Raw:
notificationContent = e.RawNotification.Content;
break;
}
e.Cancel = true;
try
{
JObject jObject = JObject.Parse(notificationContent);
JToken jToken;
jObject.TryGetValue("refresh", out jToken);
bool refreshValue = jToken != null && Convert.ToBoolean(jToken.ToString());
if (refreshValue)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, RefreshTodoItems);
}
}
catch (Exception err)
{
Debug.WriteLine(err.Message);
}
}
然后我可以跟踪这一点(全部通过WAT示例中的WNSRecipe/NotificationsExtensions代码完成)。我得到:
结果是:
{Channel URI: https://bn1.notify.windows.com/?token=AgYAAACuGgtx...
Notification status: Received
Status code: OK
Device connection status: NotApplicable
Error description:
Debug Trace: BN1WNS2011828
MessageId: 3FA318CE5C48E9CF
Timestamp: 3/8/2013 9:23:18 PM -07:00
}
所以我假设通知正在发送
更新:我回去仔细检查了设置,并在应用程序中添加了一个测试按钮来发布我的请求。(毫无疑问,我知道该应用程序处于活动状态)。我在我的通知请求中添加了
RequestForStatus=true
,并在事件查看器中返回“设备连接状态:已连接”以及新的日志条目集。在仔细考虑各种可能的问题/解决方案后,我终于找到了问题所在。罪魁祸首在我的事件处理程序代码中。无论出于何种原因,使用NewtonSoft JObject的代码在我的另一个测试项目中运行良好,但在这个项目中以一种令人讨厌的、无声的方式失败了。我转而使用ServiceStack.Text(我一直想这么做,因为据说它要快得多……今后,除了简练的原始通知外,我还会向应用程序发送更多的数据)
我首先回到了最简单的代码,除了:
private async void OnPushNotification(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
if (e.NotificationType == PushNotificationType.Raw)
{
e.Cancel = true;
String notificationContent = String.Empty;
notificationContent = e.RawNotification.Content;
}
}
这取代了问题方法(仅供参考)如下所示:
{Channel URI: https://bn1.notify.windows.com/?token=AgYAAACuGgtx...
TTL: -
Cache: no
Request for status: no
Tag:
Priority: Normal
Token retry count: 0
really
}
private async void OnPushNotification(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
String notificationContent = String.Empty;
switch (e.NotificationType)
{
case PushNotificationType.Badge:
case PushNotificationType.Tile:
case PushNotificationType.Toast:
notificationContent = e.TileNotification.Content.GetXml();
break;
case PushNotificationType.Raw:
notificationContent = e.RawNotification.Content;
break;
}
e.Cancel = true;
try
{
JObject jObject = JObject.Parse(notificationContent);
JToken jToken;
jObject.TryGetValue("refresh", out jToken);
bool refreshValue = jToken != null && Convert.ToBoolean(jToken.ToString());
if (refreshValue)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, RefreshTodoItems);
}
}
catch (Exception err)
{
Debug.WriteLine(err.Message);
}
}
最后,我得出了这样的结论,效果很好:
private async void OnPushNotification(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
if (e.NotificationType != PushNotificationType.Raw) return;
e.Cancel = true;
try
{
// Use ServiceStack.Text.WinRT to get this
var jsonObj = JsonSerializer.DeserializeFromString<JsonObject>(e.RawNotification.Content);
// TODO: Do something with this value now that it works
}
catch (Exception err)
{
Debug.WriteLine(err.Message);
}
});
}
private async void OnPushNotification(PushNotificationChannel sender,PushNotificationReceivedEventArgs e)
{
wait Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>
{
如果(e.NotificationType!=PushNotificationType.Raw)返回;
e、 取消=真;
尝试
{
//使用ServiceStack.Text.WinRT获取此
var jsonObj=JsonSerializer.DeserializeFromString(e.RawNotification.Content);
//TODO:现在可以使用此值进行操作
}
捕获(异常错误)
{
Debug.WriteLine(错误消息);
}
});
}
当你说调试/跟踪原始通知看起来是**创建**的时候,你是否看到它们被发送到客户端(比如说通过Fiddler)而没有采取行动?@Jim O'Niel我已经添加了更多细节。我不确定它们是否已发送到Windows8/我的Win8应用程序。我之所以看到它们,是因为我在本地运行MVC应用程序。有没有办法让Fiddler直接拦截通知?谢谢,顺便说一句,你的博客文章很有帮助。再想一想,跳过小提琴手。在事件查看器>应用程序和服务日志>Microsoft>Windows>推送通知平台(我相信)中,您应该看到您的机器发生了什么。我并不完全了解这里显示的内容,但可能值得一看,看看您是否看到了错误或收到消息的指示(无论是在工作情况下还是在非工作情况下),感谢您提供有关查看推送通知平台日志的提示。正在发送通知,不过我需要花更多的时间来查看我能收集到什么。需要运行应用程序才能通过PushNotificationReceived事件截获任何类型的通知(包括原始通知)。在发送/传递原始通知时,第二个应用程序是否已启动并运行?