WP7推送通知返回空通道uri

本文关键字:通道 uri 返回 通知 WP7 | 更新日期: 2023-09-27 18:19:08

我有一个应用程序在windows phone市场,我已经设置了从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个月后返回null作为pushChannel。ChannelUriUpdated从未触发!我能做些什么来解决这个问题吗?

WP7推送通知返回空通道uri

您还需要检查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();
}