2013-10-03 119 views
3

我想检查推送通知实施是否正确。Windows Phone 8推送通知推送通道始终创建新通道uri

每当我打开我的应用程序(实际上我只在特定页面上注册推送通道,所以每次我从该页面来回移动时)都会创建一个新的推送通道URI,并将其存储在我的移动设备中服务数据库发送推送通知。这对我来说似乎并不正确,因为每次打开应用程序/页面时都会生成新的推送通道URI,因此每个使用我的应用程序的设备的通道URI列表只是增长和增长。我假设你创建一个推送频道,存储频道URI并根据需要推送它。我会在这里记下我正在使用原始推送通知。

我知道推送频道每隔一段时间就会过期,但对我而言,每当我退出应用程序/页面时都会发生这种情况,因此当onNavigateTo被调用时,我发现存在的推送频道以及始终存在的新频道URI创建。它是否正确?

我的代码如下:

保护覆盖无效的OnNavigatedTo(NavigationEventArgs E) { registerPushChannel(); }

private void registerPushChannel() 
    { 
     // The name of our push channel. 
     string channelName = "RawSampleChannel"; 

     // Try to find the push channel. 
     pushChannel = HttpNotificationChannel.Find(channelName); 

     // If the channel was not found, then create a new connection to the push service. 
     if (pushChannel == null) 
     { 
      pushChannel = new HttpNotificationChannel(channelName); 

      // Register for all the events before attempting to open the channel. 
      pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated); 
      pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred); 
      pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived); 

      pushChannel.Open(); 

     } 
     else 
     { 
      // The channel was already open, so just register for all the events. 
      pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated); 
      pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred); 
      pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived); 

      // code which passes the new channel URI back to my web service    

     } 

    } 

protected override void OnNavigatedFrom(NavigationEventArgs e) 
    { 
     pushChannel.Close(); 
    } 

所以要澄清,该应用程序被打开,推注册频道和频道URI是保存在我的Web服务。 Web服务然后将通知发送到通道uri。当我退出应用程序或页面并返回时,会发现推送频道,但会创建一个新频道uri,然后再次保存到我的网络服务。我的频道表实际上保持增长和增长。

那么,这是应该如何处理新的渠道URI不断产生?这对我来说没有意义。我不知道如何吐司和磁贴通知工作,但我会假设渠道URI需要是静态时,应用程序关闭,以保持接收通知,而应用程序关闭,但也许这可能是一个功能bindtotoast和bindtotile等我正在做的是正确的,因为这是与原始通知。

+0

我正在努力与'PushChannel_ChannelUriUpdated'实际投入。你能举个例子吗? – creatiive

回答

7

你主要是做对了。

推送通知是一件有趣的事情。
您创建一个频道,将其发送到您的服务器,然后服务器可以发送,直到它失败(频道Uri过期或出现错误)。 此时应用程序需要创建一个新的ChannelUri,然后UPDATE存储在服务器上的该应用程序/设备的值。服务器将能够发送通知。

一些要点

  1. 当URI是请求一个仍然是有效的,你会得到相同的一回一个新的通道。
  2. 当你要求一个新的频道uri并且当前的频道已经过期时,你通常会得到相同的uri,但频道将会重新开始播放。
  3. 无法确定某个频道是否已从应用内过期,而无需运行代码,如registerPushChannel方法。 (除非您在后端追踪此内容,并且应用程序查询后端)
  4. 无法告诉应用程序某个频道已过期,或者告诉用户重新打开应用程序以重新建立频道连接推基础设施。

尝试确保频道始终可用的标准方法是在应用程序启动时检查频道。
这就是你正在做的,你可能只是想确保你正在更新服务器记录,而不仅仅是增加更多。

+0

甜。感谢那。是的插入我检查通道Uri为该用户不存在。很高兴知道我做对了。再次感谢。 – doktorg