每当我尝试在Windows Phone 7和Windows Phone 8中创建推送通知时,我的代码工作正常,但每当我尝试在Windows Phone 8.1上构建相同的代码时,它会在生成Chanal Uri时显示“System.NullReferenceException”在Windows Phone 8.1中。
代码如下:
String channelUri="XYZ";
HttpNotificationChannel myPushChannel = HttpNotificationChannel.Find(channelUri);
if (null == myPushChannel)
{
myPushChannel = new HttpNotificationChannel(channelUri);
myPushChannel.Open();
myPushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(myPushChannel_ShellToastNotificationReceived);
myPushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(myPushChannel_ChannelUriUpdated);
myPushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(myPushChannel_ErrorOccurred);
myPushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(myPushChannel_HttpNotificationReceived);
myPushChannel.BindToShellTile();
myPushChannel.BindToShellToast();
uri = myPushChannel.ChannelUri;
}
else
{
if (myPushChannel != null)
{
myPushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(myPushChannel_ShellToastNotificationReceived);
myPushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(myPushChannel_ChannelUriUpdated);
myPushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(myPushChannel_ErrorOccurred);
myPushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(myPushChannel_HttpNotificationReceived);
uri = myPushChannel.ChannelUri;
if (!myPushChannel.IsShellToastBound)
myPushChannel.BindToShellToast();
if (!myPushChannel.IsShellTileBound)
myPushChannel.BindToShellTile();
}
}
}
在Windows Phone 7/8中一切正常,但在Windows Phone 8.1上无法正常工作。
答案 0 :(得分:0)
如果我们谈论的是WP8.1 Universal Apps,而不是Silverlight,您应该使用下面的行来获取PushNotificationChannel:
PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();