2013-03-09 158 views
0

所需的行为是拦截应用程序中的原始通知(不需要后台任务或用户放弃其宝贵的后台任务点之一)。根据http://msdn.microsoft.com/en-us/library/windows/apps/xaml/JJ709907(v=win.10).aspx的示例代码,我创建了一个事件处理程序来拦截原始通知。在我创建的一个应用程序中,使用Azure移动服务通过WNS发送通知事件处理程序被触发并且效果很好。如何获取PushNotificationChannel.PushNotificationReceived事件处理程序来拦截通知?

但在第二个应用程序,它不工作。通知似乎很好。也就是说,当我的Web服务让WNS发送一个Toast通知时,它就会显示出来。此外,我可以调试/跟踪原始通知,并且它们似乎也被创建得很好。有我缺少的设置吗?

我通过Fiddler看到请求和结果,但也许只是因为我在本地运行MVC 4.0 Web API项目?要求是这样的:

{Channel URI:    https://bn1.notify.windows.com/?token=AgYAAACuGgtx... 
TTL:      - 
Cache:     no 
Request for status:  no 
Tag:      
Priority:     Normal 
Token retry count:  0 

really 
} 

然后我可以跟踪这个(通过WNSRecipe/NotificationsExtensions代码是这样的WAT样品的部分全部完成)。我得到:

{Sent WNS notification: Received 
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 


- REQUEST ------------------------------------------------------------------ 
X-WNS-Type     : wns/raw 
Content-Type     : application/octet-stream 
Authorization     : Bearer EgAaAQMAAAAEgAAACoAAx1d3DqT9jZxJdOFIUJ9... 
Host       : bn1.notify.windows.com 
Content-Length    : 6 

really 

- RESPONSE ----------------------------------------------------------------- 
X-WNS-NOTIFICATIONSTATUS  : received 
X-WNS-MSG-ID     : 3FA318CE5C48E9CF 
X-WNS-DEBUG-TRACE    : BN1WNS2011828 
Content-Length    : 0 
Date       : Sat, 09 Mar 2013 04:23:11 GMT 
} 

而结果:

{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,并在“事件查看器”中获取“设备连接状态:连接”以及新的日志条目集。

+0

当你说调试/跟踪原始通知似乎是**创建**罚款,是你看到他们被交付给客户(通过说Fiddler),而没有采取行动? – 2013-03-09 00:38:51

+0

@ Jim O'Niel我为此添加了更多细节。我不确定它们是否被传送到Windows8/Win8应用程序。我只是看到他们,因为我在本地运行MVC应用程序。有没有办法让Fiddler直接拦截通知?谢谢,顺便说一下,你的博客文章有帮助。第二次想到 – Stonetip 2013-03-09 03:48:02

+0

,跳过提琴手。在事件查看器>应用程序和服务日志> Microsoft> Windows>推送通知 - 平台是哪里(我相信)你应该看到什么到你的机器。我并不完全熟悉那里显示的内容,但可能值得一看,看看您是否看到错误或表示收到了该消息(包括工作和非工作情况下) – 2013-03-09 04:26:45

回答

0

在我遇到各种可能的问题/解决方案后,我终于找到了错误。罪魁祸首是在我的事件处理程序代码中。无论出于何种原因,使用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; 
    } 
} 

这取代了问题的方法,该方法(仅供参考)是这样的:

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