summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2013-04-15 14:08:35 +0400
committerGravatar Vitaly Takmazov2013-04-15 14:08:35 +0400
commite3409abca917fbc5fd49d6366276fff0e0f60d3e (patch)
tree22cdf10c0aeb2a074f662475d98fd0b89f7f945e
parent0f5b520aa44faedbdbab4cdc1020384e9720fe8d (diff)
Initial push notifications support
-rw-r--r--Juick/Classes/AccountManager.cs15
-rw-r--r--Juick/MainPage.xaml.cs90
-rw-r--r--Juick/ViewModels/AppViewModel.cs120
3 files changed, 138 insertions, 87 deletions
diff --git a/Juick/Classes/AccountManager.cs b/Juick/Classes/AccountManager.cs
index 4741992..1f9ffe5 100644
--- a/Juick/Classes/AccountManager.cs
+++ b/Juick/Classes/AccountManager.cs
@@ -54,7 +54,22 @@ namespace Juick.Classes
}
set { IsolatedStorageSettings.ApplicationSettings["authenticated"] = value; }
}
+
+ public string NotificationUri
+ {
+ get
+ {
+ string savedUri;
+ IsolatedStorageSettings.ApplicationSettings.TryGetValue<string>("notification_uri", out savedUri);
+ return savedUri;
+ }
+ set
+ {
+ IsolatedStorageSettings.ApplicationSettings["notification_uri"] = value;
+ }
+ }
+
public void SignOut(Page page)
{
IsAuthenticated = false;
diff --git a/Juick/MainPage.xaml.cs b/Juick/MainPage.xaml.cs
index b37bd8f..23418ff 100644
--- a/Juick/MainPage.xaml.cs
+++ b/Juick/MainPage.xaml.cs
@@ -19,50 +19,11 @@ namespace Juick
public MainPage()
{
// Holds the push channel that is created or found.
- //HttpNotificationChannel pushChannel; // unused variable
-
- // The name of our push channel.
- //string channelName = "JuickChannel"; // unused variable
+
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()));
-
- }*/
-
+
DataContext = App.AppContext;
Loaded += (o, args) =>
@@ -119,52 +80,7 @@ namespace Juick
}
- void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
- {
-
- Dispatcher.BeginInvoke(() =>
- {
- // Display the new URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
- System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());
- MessageBox.Show(String.Format("Channel Uri is {0}",
- e.ChannelUri.ToString()));
-
- });
- }
- void PushChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
- {
- StringBuilder 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 e.Collection.Keys)
- {
- message.AppendFormat("{0}: {1}\n", key, e.Collection[key]);
-
- if (string.Compare(
- key,
- "wp:Param",
- System.Globalization.CultureInfo.InvariantCulture,
- System.Globalization.CompareOptions.IgnoreCase) == 0)
- {
- relativeUri = e.Collection[key];
- }
- }
-
- // Display a dialog of all the fields in the toast.
- Dispatcher.BeginInvoke(() => MessageBox.Show(message.ToString()));
-
- }
- void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
- {
- // Error handling logic for your particular application would be here.
- Dispatcher.BeginInvoke(() =>
- MessageBox.Show(String.Format("A push notification {0} error occurred. {1} ({2}) {3}",
- e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData))
- );
- }
+
private void ApplicationBarIconButtonClick(object sender, EventArgs e)
{
diff --git a/Juick/ViewModels/AppViewModel.cs b/Juick/ViewModels/AppViewModel.cs
index 77970df..29e2a6b 100644
--- a/Juick/ViewModels/AppViewModel.cs
+++ b/Juick/ViewModels/AppViewModel.cs
@@ -5,11 +5,98 @@ using System.Linq;
using Juick.Classes;
using JuickApi;
using RestSharp;
+using Microsoft.Phone.Notification;
+using System.Diagnostics;
+using System.Text;
namespace Juick.ViewModels
{
public class AppViewModel : ViewModelBase
{
+ readonly HttpNotificationChannel pushChannel;
+
+ // The name of our push channel.
+ string channelName = "JuickChannel";
+
+ public AppViewModel()
+ {
+ 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 += (sender, e) => EnableNotifications(e.ChannelUri.ToString());
+ pushChannel.ErrorOccurred += (sender, e) => DisableNotifications();
+
+ // Register for this notification only if you need to receive the notifications while your application is running.
+ // Register for this notification only if you need to receive the notifications while your application is running.
+ pushChannel.ShellToastNotificationReceived += (sender, e) =>
+ {
+ StringBuilder 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 e.Collection.Keys)
+ {
+ message.AppendFormat("{0}: {1}\n", key, e.Collection[key]);
+
+ if (string.Compare(
+ key,
+ "wp:Param",
+ System.Globalization.CultureInfo.InvariantCulture,
+ System.Globalization.CompareOptions.IgnoreCase) == 0)
+ {
+ relativeUri = e.Collection[key];
+ }
+ }
+ Debug.WriteLine("Received: " + message.ToString());
+ };
+
+ 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 += (sender, e) => EnableNotifications(e.ChannelUri.ToString());
+ pushChannel.ErrorOccurred += (sender, e) => DisableNotifications();
+
+ // Register for this notification only if you need to receive the notifications while your application is running.
+ pushChannel.ShellToastNotificationReceived += (sender, e) =>
+ {
+ StringBuilder 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 e.Collection.Keys)
+ {
+ message.AppendFormat("{0}: {1}\n", key, e.Collection[key]);
+
+ if (string.Compare(
+ key,
+ "wp:Param",
+ System.Globalization.CultureInfo.InvariantCulture,
+ System.Globalization.CompareOptions.IgnoreCase) == 0)
+ {
+ relativeUri = e.Collection[key];
+ }
+ }
+ Debug.WriteLine("Received: " + message.ToString());
+ };
+
+ EnableNotifications(pushChannel.ChannelUri.ToString());
+ }
+ }
private ObservableCollection<PageViewModel> _pages;
public ObservableCollection<PageViewModel> Pages
{
@@ -51,5 +138,38 @@ namespace Juick.ViewModels
});
}
}
+
+ public void EnableNotifications(string Url)
+ {
+ if (!Account.IsAuthenticated) return;
+ Client.Authenticator = new HttpBasicAuthenticator(Account.UserName, Account.Password);
+ Debug.WriteLine(Url.ToString());
+ if (string.IsNullOrEmpty(Account.NotificationUri) || Account.NotificationUri != Url)
+ {
+ Account.NotificationUri = Url;
+ RegisterNotificationUrl(Url);
+ }
+ else
+ {
+ UnregisterNotificationUrl(Account.NotificationUri);
+ Account.NotificationUri = Url;
+ RegisterNotificationUrl(Url);
+ }
+ }
+
+ public void DisableNotifications()
+ {
+ UnregisterNotificationUrl(Account.NotificationUri);
+ Account.NotificationUri = string.Empty;
+ }
+
+ void RegisterNotificationUrl(string newUrl)
+ {
+ Client.ExecuteAsync(new RestRequest("/winphone/register?url=" + newUrl), response => Debug.WriteLine(response.StatusCode));
+ }
+ void UnregisterNotificationUrl(string oldUrl)
+ {
+ Client.ExecuteAsync(new RestRequest("/winphone/unregister?url=" + oldUrl), response => Debug.WriteLine(response.StatusCode));
+ }
}
}