Windows Phone 8 推送通知的推送通道始终创建新的通道 URI。

3

我想确认我的推送通知实现是否正确。

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

我知道推送通道会定期过期,但对我来说,每当退出应用程序/页面时都会发生这种情况,因此在调用onNavigateTo时,我会找到已存在的推送通道并始终创建新的通道URI。这正确吗?

我的代码如下:

protected override void 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,我再次保存到我的Web服务中。实际上,我的通道表不断增长。
这种方式是否应该不断生成新的通道URI?对我来说有点不合理。我不确定烤面包和平铺式通知是如何工作的,但我认为当应用程序关闭时,通道URI需要是静态的,以便在应用程序关闭时继续接收通知,但也许bindtotoast和bindtotile的功能可以实现这一点,因此我所做的是正确的,因为它涉及原始通知。

我正在为' PushChannel_ChannelUriUpdated '中应该放置什么而苦恼。你能给个例子吗? - creatiive
1个回答

7

你大多数时候做得很对。

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

一些重要的点

  1. 当为仍然有效的URI请求新的通道URI时,你会收到相同的URI。
  2. 当你请求新的通道URI并且当前的URI已经过期时,通常会返回相同的URI,但通道将再次变为活动状态。
  3. 除非在后端跟踪此信息并且应用程序查询后端,否则无法从应用程序内部知道通道何时过期,例如你的registerPushChannel方法。
  4. 没有办法告诉应用程序通道已过期,或告诉用户重新打开应用程序以使用推送基础结构重新建立通道连接。

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


太好了。谢谢你。是的,在插入时,我会检查该用户的通道Uri是否已经存在。很高兴知道我做得对。再次感谢。 - doktorg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接