我正在尝试在 Windows Phone 上实现基于推送通知的应用程序。我已经能够在模拟器上检索 Channel Uri 并通过我的服务器向模拟器发送推送通知。
另一方面,我在我的设备上部署相同的解决方案时遇到了问题。使用 Uri 会返回 NullReferenceException。而 Channel Uri 显示“无法评估表达式”。
这是我放置在页面构造函数中的代码。我也尝试过更改 _pushChannelName。
private static string _pushChannelName = "TestApp";
// Constructor
public MainPage()
{
HttpNotificationChannel pushChannel;
InitializeComponent();
pushChannel = HttpNotificationChannel.Find(_pushChannelName);
if (pushChannel == null)
{
MessageBox.Show("NULL");
pushChannel = new HttpNotificationChannel(_pushChannelName);
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(pushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(pushChannel_ErrorOccurred);
pushChannel.ShellToastNotificationReceived +=new EventHandler<NotificationEventArgs>(pushChannel_ShellToastNotificationReceived);
pushChannel.Open();
pushChannel.BindToShellToast();
}
else
{
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(pushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(pushChannel_ErrorOccurred);
pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(pushChannel_ShellToastNotificationReceived);
//System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
textBox1.Text = pushChannel.ChannelUri.ToString();
}
MessageBox.Show(String.Format("Channel Uri is {0}", pushChannel.ChannelUri.ToString()));
}
我还尝试从 ChangeUri 事件中检查 Uri。该事件不会在设备上触发,而 Push 应用程序工作正常。甚至没有达到频道限制。
private void pushChannel_ChannelUriUpdated(Object sender, NotificationChannelUriEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(String.Format("Channel Uri is {0}",
e.ChannelUri.ToString()));
});
}