WP8上的推送通知始终生成新的URI

时间:2014-07-22 12:42:14

标签: windows-phone-8 push-notification

我正在开发一个使用原始通知的Windows Phone 8应用。为此,我正在关注示例"How to send and receive raw notifications for Windows Phone 8"

我完全按照样本编码:

public MainPage()
        {
            /// Holds the push channel that is created or found.
            HttpNotificationChannel pushChannel;

            // The name of our push channel.
            string channelName = "RawSampleChannel";

            InitializeComponent();

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

                // Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
                System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
                MessageBox.Show(String.Format("Channel Uri is {0}",
                    pushChannel.ChannelUri.ToString()));

            }
        }

在设备中运行代码后,我发现应用程序每次启动时都会生成不同的URI。

我意识到 HttpNotificationChannel.Find(channelName)总是返回null(这就是应用程序始终生成新URI的原因。)

我已经阅读了this,但仍然没有帮助我。

所以,我的问题是:

  • 如果应用已经生成了频道,那么 HttpNotificationChannel.Find(channelName)会返回与“null”不同的内容吗?

如果我错过了可以提供帮助的内容,请告诉我。

1 个答案:

答案 0 :(得分:0)

我查看了你的帖子和提到的链接。似乎channelUri更新是相当随机的(通常它肯定会改变app卸载/安装,但也可能有其他场景)。确保用户正确定位的一种确定方法是将channelUri与另一个唯一标识符相关联。例如,在获取channelUri之后,您可能希望使用uri和唯一的用户ID更新服务器数据库。对于子请求调用,可以仅为同一用户更新频道。希望这有点帮助。