[英]WP7 Push Notifications returns null channel uri
我在Windows Phone Market中有一個應用程序,並且已經使用來自http://msdn.microsoft.com/的代碼設置了推送通知
public MainPage()
{
/// Holds the push channel that is created or found.
HttpNotificationChannel pushChannel;
// The name of our push channel.
string channelName = "ToastSampleChannel";
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);
// Register for this notification only if you need to receive the notifications while your application is running.
pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
pushChannel.Open();
// Bind this new channel for toast events.
pushChannel.BindToShellToast();
}
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);
// Register for this notification only if you need to receive the notifications while your application is running.
pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
// 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,但是在2-3個月之后,由於pushChannel.ChannelUriUpdated從未觸發返回null! 我可以做些什么來解決這個問題嗎?
您還需要檢查pushChannel.ChannelUri
是否為null
,是否創建新頻道。
這是pushChannel != null
時的代碼,在您的情況下,這將進入else
子句:
if (pushChannel.ChannelUri != null)
{
// This is raising my event to signal any subscribers
// that an new channelUri is available
RaiseGotPushUri(pushChannel.ChannelUri);
// Re-register the event handlers
pushChannel.ChannelUriUpdated += PushChannel_ChannelUriUpdated;
pushChannel.ShellToastNotificationReceived += PushChannel_ShellToastNotificationReceived;
pushChannel.ErrorOccurred += PushChannel_ErrorOccurred;
}
else
{
// If we never got the Uri back, unbind and reset everything...
// Dispose of the old channel
pushChannel.ChannelUriUpdated -= PushChannel_ChannelUriUpdated;
pushChannel.ShellToastNotificationReceived -= PushChannel_ShellToastNotificationReceived;
pushChannel.ErrorOccurred -= PushChannel_ErrorOccurred;
if (pushChannel.IsShellToastBound) pushChannel.UnbindToShellToast();
pushChannel.Close();
pushChannel.Dispose();
// ... and re-register the event handlers
pushChannel = new HttpNotificationChannel(channelName);//, _serviceName);
pushChannel.ChannelUriUpdated += PushChannel_ChannelUriUpdated;
pushChannel.ShellToastNotificationReceived += PushChannel_ShellToastNotificationReceived;
pushChannel.ErrorOccurred += PushChannel_ErrorOccurred;
// Ask for a new Uri
pushChannel.Open();
// Set the HttpNotificationChannel to handle the appropriate push notifications
pushChannel.BindToShellToast();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.