|
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(); |
|
} |
|
} |
|
} |