3

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

每次我打开我的应用程序时(实际上我只在特定页面上注册推送通道,所以每次我从该页面来回访问时)都会创建一个新的推送通道 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,我再次将其保存到我的 Web 服务中。我的频道表实际上一直在增长。

那么这是否应该与不断生成的新频道 URI 一起工作?这对我来说有点没有意义。我不确定 toast 和 tile 通知是如何工作的,但我假设当应用程序关闭时通道 URI 需要是静态的,以便在应用程序关闭时继续接收通知,但也许这可能是 bindtoast 和 bindtotile 的功能等等我所做的是正确的,因为它与原始通知有关。

4

1 回答 1

7

你大部分都做对了。

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

一些重要的点

  1. 当为仍然有效的频道请求新频道 Uri 时,您将获得相同的频道 Uri。
  2. 当您请求新的频道 uri 而当前的 uri 已过期时,您通常会返回相同的 uri,但该频道将再次启用。
  3. 如果不运行像您的registerPushChannel方法这样的代码,则无法从应用程序中知道频道是否已过期。(除非您在后端跟踪此内容并且应用程序查询后端。)
  4. 无法告诉应用程序频道已过期,或告诉用户重新打开应用程序以使用推送基础设施重新建立频道连接。

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

于 2013-10-03T09:28:35.937 回答