Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Last active December 16, 2015 05:29
Show Gist options
  • Save AlbertoMonteiro/5384940 to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/5384940 to your computer and use it in GitHub Desktop.

Estou usando Push Notifications e estou passando por um probema que ao tentar abrir um channel inexistente recebo uma exceção dizendo que o channel está aberto.

O primeiro método ListenShellToastNotification roda beleza, mas o segundo ListenShellTileNotification é onde ocorre a exceção.

A linha 96 o retorno é null, então ele entra no if para criar o channel, e quando tenta executar o open recebo uma InvalidOperationException cuja a mensagem diz que o channel já está aberto.

#DESCOBRI O PROBLEMA

O problema é que eu tentava abrir 2 canais diferentes "ToastSampleChannel" e "TileSampleChannel", quando na verdade só posso ter 1. Adicionei outro arquivo, o Funcionando.cs, que é o código certo!

using System;
using System.Net;
using System.Text;
using System.Windows;
using MyApp.WindowsPhone7.Maping;
using MyApp.WindowsPhone7.ViewModel;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Notification;
namespace MyApp.WindowsPhone7
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
MapsCreator.CreateMaps();
Loaded += MainLoaded;
}
private void MainLoaded(object sender, RoutedEventArgs e)
{
ListenNotification("SampleChannel");
}
private static void ListenNotification(string channelName)
{
// Try to find the push channel.
HttpNotificationChannel 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 += (o, args) => ChannelUriUpdated(args);
pushChannel.ErrorOccurred += (o, args) => ErrorOccurred(args);
// Register for this notification only if you need to receive the notifications while your application is running.
pushChannel.ShellToastNotificationReceived += (o, args) => ShellToastNotificationReceived(args);
pushChannel.Open();
// Bind this new channel for toast events.
pushChannel.BindToShellToast();
pushChannel.BindToShellTile();
}
else
{
// The channel was already open, so just register for all the events.
pushChannel.ChannelUriUpdated += (o, args) => ChannelUriUpdated(args);
pushChannel.ErrorOccurred += (o, args) => ErrorOccurred(args);
// Register for this notification only if you need to receive the notifications while your application is running.
pushChannel.ShellToastNotificationReceived += (o, args) => ShellToastNotificationReceived(args);
// 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());
}
ChamaToast(pushChannel.ChannelUri.ToString());
ChamaTile(pushChannel.ChannelUri.ToString());
}
private static void ChamaToast(string url)
{
System.Diagnostics.Debug.WriteLine("http://192.168.25.2/teste/Home/NotificaToast/?url=" + url);
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://192.168.25.2/teste/Home/NotificaToast/?url=" + url);
httpWebRequest.BeginGetResponse(ar =>
{
httpWebRequest.EndGetResponse(ar);
if (ar.IsCompleted)
System.Diagnostics.Debug.WriteLine("Foi Toast");
}, null);
}
private static void ChamaTile(string url)
{
System.Diagnostics.Debug.WriteLine("http://192.168.25.2/teste/Home/NotificaTile/?url=" + url);
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://192.168.25.2/teste/Home/NotificaTile/?url=" + url);
httpWebRequest.BeginGetResponse(ar =>
{
httpWebRequest.EndGetResponse(ar);
if (ar.IsCompleted)
System.Diagnostics.Debug.WriteLine("Foi");
}, null);
}
private static void ShellToastNotificationReceived(NotificationEventArgs args)
{
var message = new StringBuilder();
string relativeUri = string.Empty;
message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString());
// Parse out the information that was part of the message.
foreach (string key in args.Collection.Keys)
{
message.AppendFormat("{0}: {1}\n", key, args.Collection[key]);
if (string.Compare(key, "wp:Param", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CompareOptions.IgnoreCase) == 0)
{
relativeUri = args.Collection[key];
}
}
// Display a dialog of all the fields in the toast.
System.Diagnostics.Debug.WriteLine(message.ToString());
}
private static void ErrorOccurred(NotificationChannelErrorEventArgs args)
{
System.Diagnostics.Debug.WriteLine("A push notification {0} error occurred. {1} ({2}) {3}", args.ErrorType, args.Message, args.ErrorCode, args.ErrorAdditionalData);
}
private static void ChannelUriUpdated(NotificationChannelUriEventArgs args)
{
System.Diagnostics.Debug.WriteLine("Channel Uri is {0}", args.ChannelUri);
}
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/Sobre.xaml", UriKind.RelativeOrAbsolute));
}
private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
{
var mainViewModel = (MainViewModel)DataContext;
mainViewModel.Logof();
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MyApp.WindowsPhone7.Maping;
using MyApp.WindowsPhone7.ViewModel;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Notification;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace MyApp.WindowsPhone7
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
MapsCreator.CreateMaps();
Loaded += MainLoaded;
}
private void MainLoaded(object sender, RoutedEventArgs e)
{
ListenShellToastNotification("ToastSampleChannel");
ListenShellTileNotification("TileSampleChannel");
}
private static void ListenShellToastNotification(string channelName)
{
// Try to find the push channel.
HttpNotificationChannel 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 += (o, args) => ChannelUriUpdated(args);
pushChannel.ErrorOccurred += (o, args) => ErrorOccurred(args);
// Register for this notification only if you need to receive the notifications while your application is running.
pushChannel.ShellToastNotificationReceived += (o, args) => ShellToastNotificationReceived(args);
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 += (o, args) => ChannelUriUpdated(args);
pushChannel.ErrorOccurred += (o, args) => ErrorOccurred(args);
// Register for this notification only if you need to receive the notifications while your application is running.
pushChannel.ShellToastNotificationReceived += (o, args) => ShellToastNotificationReceived(args);
// 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());
}
HttpWebRequest httpWebRequest = null;
do
{
System.Diagnostics.Debug.WriteLine("http://192.168.25.2/teste/Home/NotificaToast/?url=" + pushChannel.ChannelUri);
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://192.168.25.2/teste/Home/NotificaToast/?url=" + pushChannel.ChannelUri);
} while (pushChannel.ChannelUri == null);
httpWebRequest.BeginGetResponse(ar =>
{
httpWebRequest.EndGetResponse(ar);
if (ar.IsCompleted)
System.Diagnostics.Debug.WriteLine("Foi");
}, null);
}
private static void ListenShellTileNotification(string channelName)
{
// Try to find the push channel.
HttpNotificationChannel 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 += (o, args) => ChannelUriUpdated(args);
pushChannel.ErrorOccurred += (o, args) => ErrorOccurred(args);
pushChannel.Open();
// Bind this new channel for toast events.
pushChannel.BindToShellTile();
}
else
{
// The channel was already open, so just register for all the events.
pushChannel.ChannelUriUpdated += (o, args) => ChannelUriUpdated(args);
pushChannel.ErrorOccurred += (o, args) => ErrorOccurred(args);
// 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());
}
HttpWebRequest httpWebRequest = null;
do
{
System.Diagnostics.Debug.WriteLine("http://192.168.25.2/teste/Home/NotificaTile/?url=" + pushChannel.ChannelUri);
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://192.168.25.2/teste/Home/NotificaTile/?url=" + pushChannel.ChannelUri);
} while (pushChannel.ChannelUri == null);
httpWebRequest.BeginGetResponse(ar =>
{
httpWebRequest.EndGetResponse(ar);
if (ar.IsCompleted)
System.Diagnostics.Debug.WriteLine("Foi");
}, null);
}
private static void ShellToastNotificationReceived(NotificationEventArgs args)
{
var message = new StringBuilder();
string relativeUri = string.Empty;
message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString());
// Parse out the information that was part of the message.
foreach (string key in args.Collection.Keys)
{
message.AppendFormat("{0}: {1}\n", key, args.Collection[key]);
if (string.Compare(key, "wp:Param", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CompareOptions.IgnoreCase) == 0)
{
relativeUri = args.Collection[key];
}
}
// Display a dialog of all the fields in the toast.
System.Diagnostics.Debug.WriteLine(message.ToString());
}
private static void ErrorOccurred(NotificationChannelErrorEventArgs args)
{
System.Diagnostics.Debug.WriteLine("A push notification {0} error occurred. {1} ({2}) {3}", args.ErrorType, args.Message, args.ErrorCode, args.ErrorAdditionalData);
}
private static void ChannelUriUpdated(NotificationChannelUriEventArgs args)
{
System.Diagnostics.Debug.WriteLine("Channel Uri is {0}", args.ChannelUri);
}
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/Sobre.xaml", UriKind.RelativeOrAbsolute));
}
private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
{
var mainViewModel = (MainViewModel)DataContext;
mainViewModel.Logof();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment