Skip to content

Instantly share code, notes, and snippets.

@follesoe
Created April 11, 2012 12:02
Show Gist options
  • Save follesoe/2358895 to your computer and use it in GitHub Desktop.
Save follesoe/2358895 to your computer and use it in GitHub Desktop.
Class used to open push notification channel on Wordfeud WP7
// Maintains push notification channel
public class Pusher : IManagePushNotification
{
public event EventHandler<PusherUriEventArgs> ChannelUriUpdated;
private const string ChannelName = "WordfeudChannel";
private HttpNotificationChannel _pushChannel;
private bool _enabled;
public void Enable()
{
if (_enabled)
return;
// 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 += PushChannel_ChannelUriUpdated;
_pushChannel.ErrorOccurred += PushChannel_ErrorOccurred;
OpenChannel();
}
else
{
// The channel was already open, so just register for all the events.
_pushChannel.ChannelUriUpdated += PushChannel_ChannelUriUpdated;
_pushChannel.ErrorOccurred += PushChannel_ErrorOccurred;
// Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
NotifyChannelUpdated(_pushChannel.ChannelUri);
}
_enabled = true;
}
public void Disable()
{
if (!_enabled)
return;
_pushChannel.UnbindToShellTile();
_pushChannel.UnbindToShellToast();
_pushChannel.Close();
_pushChannel = null;
_enabled = false;
}
private void OpenChannel()
{
try
{
_pushChannel.Open();
// Bind this new channel for Toast events
_pushChannel.BindToShellToast();
// Bind this new channel for Tile events.
var uris = new Collection<Uri> { new Uri("http://avatars.wordfeud.com") };
_pushChannel.BindToShellTile(uris);
}
catch (InvalidOperationException e)
{
Debug.WriteLine(String.Format("Failed to open push channel: {0}", e));
// Schedule retry
ScheduleOpen();
}
}
private void ScheduleOpen()
{
Scheduler.ThreadPool.Schedule(TimeSpan.FromSeconds(5.0), OpenChannel);
}
private void NotifyChannelUpdated(Uri channelUri)
{
if (ChannelUriUpdated != null && channelUri != null)
{
Debug.WriteLine(channelUri.ToString());
ChannelUriUpdated(this, new PusherUriEventArgs(channelUri));
}
}
/// <summary>
/// Event handler for when the Push Channel Uri changes.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
NotifyChannelUpdated(e.ChannelUri);
}
/// <summary>
/// Event handler for when a Push Notification error occurs.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
{
Debug.WriteLine(String.Format("Push channel error occurred: {0}", e.ErrorType.ToString()));
// Check if we need to retry opening the channel
if (e.ErrorType == ChannelErrorType.ChannelOpenFailed || e.ErrorType == ChannelErrorType.PayloadFormatError)
{
// Schedule retry
ScheduleOpen();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment