mirror of
https://github.com/farcasclaudiu/myfriendsaround.git
synced 2026-06-29 13:02:05 +03:00
push notification helper library
http://windowsteamblog.com/windows_phone/b/wpdev/archive/2011/01/14/windows-push-notification-server-side-helper-library.aspx http://create.msdn.com/en-us/education/catalog/article/pnhelp-wp7
This commit is contained in:
+166
@@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.Windows.Input;
|
||||
|
||||
using WindowsPhone.Recipes.Push.Messasges;
|
||||
using WindowsPhone.Recipes.Push.Server.Services;
|
||||
using WindowsPhone.Recipes.Push.Server.Models;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the Ask user to Pin the application push notification pattern.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Only users can pin a Windows Phone application to the Start screen, therefore if a given
|
||||
/// application wants to use the tile functionality and the user didn’t pinned
|
||||
/// the app’s tile, we want to notify the user she is missing additional functionality.
|
||||
/// This pattern is implemented in both client and server-side.
|
||||
/// </remarks>
|
||||
[Export(typeof(PushPatternViewModel)), PartCreationPolicy(CreationPolicy.Shared)]
|
||||
internal sealed class AskToPinPushPatternViewModel : PushPatternViewModel
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <value>A dictionary for tracking tile messages.</value>
|
||||
private readonly Dictionary<string, TilePushNotificationMessage> _messages = new Dictionary<string, TilePushNotificationMessage>();
|
||||
|
||||
/// <value>Synchronizes access to the messages collection.</value>
|
||||
private readonly object MessageSync = new object();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ctor
|
||||
|
||||
/// <summary>
|
||||
/// Initialize new instance of this type with defaults.
|
||||
/// </summary>
|
||||
public AskToPinPushPatternViewModel()
|
||||
{
|
||||
InitializeDefaults();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Send a tile notification to all relevant subscribers.
|
||||
/// </summary>
|
||||
protected override void OnSend()
|
||||
{
|
||||
// Asynchronously try to send this message to all relevant subscribers.
|
||||
foreach (var subscriber in PushService.Subscribers)
|
||||
{
|
||||
// Create a tile message to send an update.
|
||||
var tileMsg = GetOrCreateMessage(subscriber.UserName);
|
||||
|
||||
tileMsg.SendAsync(
|
||||
subscriber.ChannelUri,
|
||||
result =>
|
||||
{
|
||||
Log(result);
|
||||
OnMessageSent(subscriber.UserName, result);
|
||||
},
|
||||
Log);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Once an application is activated again (the client side phone application
|
||||
/// has subscription logic on startup), try to update the tile again.
|
||||
/// In case that the application is not pinned, send raw notification message
|
||||
/// to the client, asking to pin the application. This raw notification message
|
||||
/// has to be well-known and handled by the client side phone application.
|
||||
/// In our case the raw message is AskToPin.
|
||||
/// </summary>
|
||||
protected override void OnSubscribed(SubscriptionEventArgs args)
|
||||
{
|
||||
// Asynchronously try to send Tile message to the relevant subscriber
|
||||
// with data already sent before so the tile won't change.
|
||||
var tileMsg = GetOrCreateMessage(args.Subscription.UserName, false);
|
||||
|
||||
tileMsg.SendAsync(
|
||||
args.Subscription.ChannelUri,
|
||||
result =>
|
||||
{
|
||||
Log(result);
|
||||
OnMessageSent(args.Subscription.UserName, result);
|
||||
},
|
||||
Log);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Privates
|
||||
|
||||
/// <summary>
|
||||
/// Once tile update sent, check if handled by the phone.
|
||||
/// In case that the application is not pinned, ask to pin.
|
||||
/// </summary>
|
||||
private void OnMessageSent(string userName, MessageSendResult result)
|
||||
{
|
||||
if (!CheckIfPinned(result))
|
||||
{
|
||||
AskUserToPin(result.ChannelUri);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Just in case that the application is running, send a raw message, asking
|
||||
/// the user to pin the application. This raw message has to be handled in client side.
|
||||
/// </summary>
|
||||
private void AskUserToPin(Uri uri)
|
||||
{
|
||||
new RawPushNotificationMessage(MessageSendPriority.High)
|
||||
{
|
||||
RawData = Encoding.ASCII.GetBytes(RawMessage)
|
||||
|
||||
}.SendAsync(uri, Log, Log);
|
||||
}
|
||||
|
||||
private bool CheckIfPinned(MessageSendResult result)
|
||||
{
|
||||
// We known if the application is pinned by checking the following send result flags:
|
||||
return result.DeviceConnectionStatus == DeviceConnectionStatus.Connected &&
|
||||
result.SubscriptionStatus == SubscriptionStatus.Active &&
|
||||
result.NotificationStatus == NotificationStatus.Received;
|
||||
}
|
||||
|
||||
private TilePushNotificationMessage GetOrCreateMessage(string userName, bool overrideExisting = true)
|
||||
{
|
||||
lock (MessageSync)
|
||||
{
|
||||
TilePushNotificationMessage message;
|
||||
if (!_messages.TryGetValue(userName, out message) || overrideExisting)
|
||||
{
|
||||
message = new TilePushNotificationMessage(MessageSendPriority.High)
|
||||
{
|
||||
BackgroundImageUri = BackgroundImageUri,
|
||||
Count = Count,
|
||||
Title = Title
|
||||
};
|
||||
|
||||
_messages[userName] = message;
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeDefaults()
|
||||
{
|
||||
DisplayName = "Ask to Pin";
|
||||
Description = "Only users can pin a Windows Phone application to the Start screen, therefore if a given application wants to use the tile functionality and the user didn’t pinned the app’s tile, we want to notify the user she is missing additional functionality. This pattern is implemented in both client and server-side.";
|
||||
BackgroundImageUri = TileImages.Length > 2 ? TileImages[2] : TileImages.FirstOrDefault();
|
||||
Count = 1;
|
||||
Title = "Game Update";
|
||||
RawMessage = "AskToPin";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+164
@@ -0,0 +1,164 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.Xml.Linq;
|
||||
|
||||
using WindowsPhone.Recipes.Push.Messasges;
|
||||
using WindowsPhone.Recipes.Push.Server.Services;
|
||||
using WindowsPhone.Recipes.Push.Server.Models;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the Counter push notification pattern.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Send a tile push notification message with a counter value.
|
||||
/// Each time a push notification message is sent the counter value
|
||||
/// increases by one, unless user is running the application and notifies
|
||||
/// the server.
|
||||
/// </remarks>
|
||||
[Export(typeof(PushPatternViewModel)), PartCreationPolicy(CreationPolicy.Shared)]
|
||||
internal sealed class CounterPushPatternViewModel : PushPatternViewModel
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <value>A dictionary for tracking tile message count while phone-application is not running.</value>
|
||||
private readonly Dictionary<string, int> _messageCounter = new Dictionary<string, int>();
|
||||
|
||||
/// <value>Synchronizes access to the message counter collection.</value>
|
||||
private readonly object MessageCounterSync = new object();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ctor
|
||||
|
||||
/// <summary>
|
||||
/// Initialize new instance of this type with defaults.
|
||||
/// </summary>
|
||||
public CounterPushPatternViewModel()
|
||||
{
|
||||
InitializeDefaults();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Send raw message to all subscribers. In case that the phone-application
|
||||
/// is not running, send tile update and increase tile counter.
|
||||
/// </summary>
|
||||
protected override void OnSend()
|
||||
{
|
||||
// Notify phone for having waiting messages.
|
||||
var rawMsg = new RawPushNotificationMessage(MessageSendPriority.High)
|
||||
{
|
||||
RawData = Encoding.ASCII.GetBytes(RawMessage)
|
||||
};
|
||||
|
||||
foreach (var subscriber in PushService.Subscribers)
|
||||
{
|
||||
rawMsg.SendAsync(
|
||||
subscriber.ChannelUri,
|
||||
result =>
|
||||
{
|
||||
Log(result);
|
||||
OnRawSent(subscriber.UserName, result);
|
||||
},
|
||||
Log);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// On subscription change, reset the subscriber tile counter if exist.
|
||||
/// </summary>
|
||||
protected override void OnSubscribed(SubscriptionEventArgs e)
|
||||
{
|
||||
// Create a tile message to reset tile count.
|
||||
var tileMsg = new TilePushNotificationMessage(MessageSendPriority.High)
|
||||
{
|
||||
Count = 0,
|
||||
BackgroundImageUri = BackgroundImageUri,
|
||||
Title = Title
|
||||
};
|
||||
|
||||
tileMsg.SendAsync(e.Subscription.ChannelUri, Log, Log);
|
||||
|
||||
ResetCounter(e.Subscription.UserName);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Privates
|
||||
|
||||
private void OnRawSent(string userName, MessageSendResult result)
|
||||
{
|
||||
// In case that the device is disconnected, no need to send a tile message.
|
||||
if (result.DeviceConnectionStatus == DeviceConnectionStatus.TempDisconnected)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Checking these three flags we can know what's the state of both the device and apllication.
|
||||
bool isApplicationRunning =
|
||||
result.SubscriptionStatus == SubscriptionStatus.Active &&
|
||||
result.NotificationStatus == NotificationStatus.Received &&
|
||||
result.DeviceConnectionStatus == DeviceConnectionStatus.Connected;
|
||||
|
||||
// In case that the application is not running, send a tile update with counter increase.
|
||||
if (!isApplicationRunning)
|
||||
{
|
||||
var tileMsg = new TilePushNotificationMessage(MessageSendPriority.High)
|
||||
{
|
||||
Count = IncreaseCounter(userName),
|
||||
BackgroundImageUri = BackgroundImageUri,
|
||||
Title = Title
|
||||
};
|
||||
|
||||
tileMsg.SendAsync(result.ChannelUri, Log, Log);
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetCounter(string userName)
|
||||
{
|
||||
lock (MessageCounterSync)
|
||||
{
|
||||
_messageCounter.Remove(userName);
|
||||
}
|
||||
}
|
||||
|
||||
private int IncreaseCounter(string userName)
|
||||
{
|
||||
lock (MessageCounterSync)
|
||||
{
|
||||
int counter;
|
||||
if (_messageCounter.TryGetValue(userName, out counter))
|
||||
{
|
||||
++counter;
|
||||
}
|
||||
else
|
||||
{
|
||||
counter = 1;
|
||||
}
|
||||
|
||||
_messageCounter[userName] = counter;
|
||||
return counter;
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeDefaults()
|
||||
{
|
||||
DisplayName = "Counter";
|
||||
Description = "Send push notification message of Tile type, with a counter value. Each time a push notification message is sent, the counter value increases by one, unless user is running the application and notifies the server.";
|
||||
BackgroundImageUri = TileImages.Length > 1 ? TileImages[1] : TileImages.FirstOrDefault();
|
||||
RawMessage = "Game Update";
|
||||
Count = 0;
|
||||
Title = "Updates";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+259
@@ -0,0 +1,259 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Media;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using Microsoft.Win32;
|
||||
|
||||
using WindowsPhone.Recipes.Push.Server.Behaviors;
|
||||
using WindowsPhone.Recipes.Push.Messasges;
|
||||
using WindowsPhone.Recipes.Push.Server.Models;
|
||||
using WindowsPhone.Recipes.Push.Server.Services;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the Custom Tile Image push notification pattern.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Send a tile update with a dynamically created image located in the remote server (in our case localhost).
|
||||
/// The image will be dynamically generated upon each request. The tile Count and Title in the
|
||||
/// payload are optional.
|
||||
/// </remarks>
|
||||
[Export(typeof(PushPatternViewModel)), PartCreationPolicy(CreationPolicy.Shared)]
|
||||
internal sealed class CustomTileImagePushPatternViewModel : PushPatternViewModel, IVisualHost
|
||||
{
|
||||
#region Constants
|
||||
|
||||
/// <value>Tile image maximum width.</value>
|
||||
public const int TileImageWidth = 173;
|
||||
|
||||
/// <value>Tile image maximum height.</value>
|
||||
public const int TileImageHeight = 173;
|
||||
|
||||
/// <value>Tile image dpi.</value>
|
||||
public const int TileImageDpi = 96;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
|
||||
/// <value>Collection of brushes for choosing text message color.</value>
|
||||
private Brush[] _textColors;
|
||||
|
||||
/// <value>Selected tile image background.</value>
|
||||
private ImageSource _tileBackground;
|
||||
|
||||
/// <value>Text for diplaying on the tile custom image.</value>
|
||||
private string _freeText;
|
||||
|
||||
/// <value>Tile custom image text size.</value>
|
||||
private double _textSize;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
[Import]
|
||||
private ImageService ImageService { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of brushes for choosing text message color.
|
||||
/// </summary>
|
||||
public Brush[] TextColors
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_textColors == null)
|
||||
{
|
||||
_textColors = (from property in typeof(Brushes).GetProperties(BindingFlags.Static | BindingFlags.Public)
|
||||
select (Brush)property.GetValue(null, null)).ToArray();
|
||||
}
|
||||
|
||||
return _textColors;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the visual element used for generating a custom image from.
|
||||
/// </summary>
|
||||
public Visual Visual
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get or sets the prefered tile background image source.
|
||||
/// </summary>
|
||||
public ImageSource TileBackground
|
||||
{
|
||||
get { return _tileBackground; }
|
||||
|
||||
set
|
||||
{
|
||||
if (_tileBackground != value)
|
||||
{
|
||||
_tileBackground = value;
|
||||
NotifyPropertyChanged("TileBackground");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a text to be displayed on the tile generated image.
|
||||
/// </summary>
|
||||
public string FreeText
|
||||
{
|
||||
get { return _freeText; }
|
||||
|
||||
set
|
||||
{
|
||||
if (_freeText != value)
|
||||
{
|
||||
_freeText = value;
|
||||
NotifyPropertyChanged("FreeText");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the text size of the tile generated image.
|
||||
/// </summary>
|
||||
public double TextSize
|
||||
{
|
||||
get { return _textSize; }
|
||||
|
||||
set
|
||||
{
|
||||
if (_textSize != value)
|
||||
{
|
||||
_textSize = value;
|
||||
NotifyPropertyChanged("TextSize");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Commands
|
||||
|
||||
/// <summary>
|
||||
/// Gets the command for picking a tile background image.
|
||||
/// </summary>
|
||||
public ICommand PickImageCommand { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ctor
|
||||
|
||||
/// <summary>
|
||||
/// Initialize new instance of this type with defaults.
|
||||
/// </summary>
|
||||
public CustomTileImagePushPatternViewModel()
|
||||
{
|
||||
InitializeDefaults();
|
||||
|
||||
PickImageCommand = new RelayCommand(
|
||||
p =>
|
||||
{
|
||||
// On execution, open file dialog for picking tile background image.
|
||||
var openDialog = new OpenFileDialog
|
||||
{
|
||||
Title = "Tile Background",
|
||||
Filter = "Jpeg|*.jpg|Bmp|*.bmp",
|
||||
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
|
||||
Multiselect = false
|
||||
};
|
||||
|
||||
if (openDialog.ShowDialog() == true)
|
||||
{
|
||||
TileBackground = new BitmapImage(new Uri(openDialog.FileName));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected
|
||||
|
||||
protected override void OnActivated()
|
||||
{
|
||||
base.OnActivated();
|
||||
|
||||
// Register to the ImageService.ImageRequest event. This event is raised
|
||||
// whenever ImageService.GetTileImage is called.
|
||||
ImageService.ImageRequest += Service_ImageRequest;
|
||||
}
|
||||
|
||||
protected override void OnDeactivated()
|
||||
{
|
||||
base.OnDeactivated();
|
||||
|
||||
ImageService.ImageRequest -= Service_ImageRequest;
|
||||
}
|
||||
|
||||
protected override void OnSend()
|
||||
{
|
||||
// Starts by sending a tile notification to all relvant subscribers.
|
||||
// This tile notification updates the tile with custom image.
|
||||
var tileMsg = new TilePushNotificationMessage(MessageSendPriority.High)
|
||||
{
|
||||
Count = Count,
|
||||
Title = Title
|
||||
};
|
||||
|
||||
foreach (var subscriber in PushService.Subscribers)
|
||||
{
|
||||
// Set the tile background image uri with the address of the ImageService.GetTileImage,
|
||||
// REST service, using current subscriber channel uri as a parameter to bo sent to the service.
|
||||
tileMsg.BackgroundImageUri = new Uri(string.Format(ImageService.GetTileImageService, string.Empty));
|
||||
tileMsg.SendAsync(subscriber.ChannelUri, Log, Log);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Privates
|
||||
|
||||
private void Service_ImageRequest(object sender, Services.ImageRequestEventArgs e)
|
||||
{
|
||||
// This event is raised by our local push-service as result of
|
||||
// the tile msg we've sent to each subscriber. This is the time
|
||||
// to pick the right tile image for the subscriber.
|
||||
if (Visual != null)
|
||||
{
|
||||
RenderImage(Visual, e.ImageStream);
|
||||
}
|
||||
}
|
||||
|
||||
private static void RenderImage(Visual visual, Stream stream)
|
||||
{
|
||||
// The next lines of code uses WPF to dynamically create an image.
|
||||
// In a real server we shouldn't use WPF, hence a 3rd party image generator library is required.
|
||||
var renderer = new RenderTargetBitmap(TileImageWidth, TileImageHeight, TileImageDpi, TileImageDpi, PixelFormats.Pbgra32);
|
||||
renderer.Render(visual);
|
||||
|
||||
var pngEncoder = new PngBitmapEncoder();
|
||||
pngEncoder.Frames.Add(BitmapFrame.Create(renderer));
|
||||
pngEncoder.Save(stream);
|
||||
}
|
||||
|
||||
private void InitializeDefaults()
|
||||
{
|
||||
DisplayName = "Custom Tile";
|
||||
Description = "Send a tile update with a dynamically created image located in the remote server (in our case localhost). The image will be dynamically generated upon each request. The tile Count and Title in the payload are optional.";
|
||||
TextSize = 20;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.Windows.Input;
|
||||
using System.IO;
|
||||
using Microsoft.Win32;
|
||||
|
||||
using WindowsPhone.Recipes.Push.Messasges;
|
||||
using WindowsPhone.Recipes.Push.Server.Models;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the One Time push notification pattern.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is the simplest push notification pattern of just pushing single time message
|
||||
/// to a registered client.
|
||||
/// </remarks>
|
||||
[Export(typeof(PushPatternViewModel)), PartCreationPolicy(CreationPolicy.Shared)]
|
||||
internal sealed class OneTimePushPatternViewModel : PushPatternViewModel
|
||||
{
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating if tile message should be sent.
|
||||
/// </summary>
|
||||
public bool IsTileEnabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating if toast message should be sent.
|
||||
/// </summary>
|
||||
public bool IsToastEnabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating if raw message should be sent.
|
||||
/// </summary>
|
||||
public bool IsRawEnabled { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ctor
|
||||
|
||||
/// <summary>
|
||||
/// Initialize new instance of this type with defaults.
|
||||
/// </summary>
|
||||
public OneTimePushPatternViewModel()
|
||||
{
|
||||
InitializeDefaults();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Depends on what message was selected, send all subscribers zero or all three push message types (Tile, Toast, Raw).
|
||||
/// </summary>
|
||||
protected override void OnSend()
|
||||
{
|
||||
var messages = new List<PushNotificationMessage>();
|
||||
|
||||
if (IsTileEnabled)
|
||||
{
|
||||
// Prepare a tile push notification message.
|
||||
messages.Add(new TilePushNotificationMessage(MessageSendPriority.High)
|
||||
{
|
||||
BackgroundImageUri = BackgroundImageUri,
|
||||
Count = Count,
|
||||
Title = Title
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
if (IsToastEnabled)
|
||||
{
|
||||
// Prepare a toast push notification message.
|
||||
messages.Add(new ToastPushNotificationMessage(MessageSendPriority.High)
|
||||
{
|
||||
Title = ToastTitle,
|
||||
SubTitle = ToastSubTitle
|
||||
});
|
||||
}
|
||||
|
||||
if (IsRawEnabled)
|
||||
{
|
||||
// Prepare a raw push notification message.
|
||||
messages.Add(new RawPushNotificationMessage(MessageSendPriority.High)
|
||||
{
|
||||
RawData = Encoding.ASCII.GetBytes(RawMessage)
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var subscriber in PushService.Subscribers)
|
||||
{
|
||||
messages.ForEach(m => m.SendAsync(subscriber.ChannelUri, Log, Log));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Privates
|
||||
|
||||
private void InitializeDefaults()
|
||||
{
|
||||
DisplayName = "One Time";
|
||||
Description = "This is the simplest push notification pattern of just pushing single time message to a registered client.";
|
||||
Count = 1;
|
||||
Title = "Game Update";
|
||||
ToastTitle = Title;
|
||||
ToastSubTitle = "Game has been released";
|
||||
RawMessage = ToastSubTitle;
|
||||
IsTileEnabled = true;
|
||||
IsToastEnabled = true;
|
||||
IsRawEnabled = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+352
@@ -0,0 +1,352 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.Windows.Input;
|
||||
|
||||
using WindowsPhone.Recipes.Push.Server.Services;
|
||||
using System.Windows;
|
||||
using WindowsPhone.Recipes.Push.Server.Models;
|
||||
using WindowsPhone.Recipes.Push.Messasges;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a base class for all push pattern view models.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A push pattern view model contains the logic and behavior of a server side push notification pattern.
|
||||
/// For demonstration purposes, and since we are using WPF as the 'face' of this server, each view model
|
||||
/// exposes properties and commands to be controlled and activated by this server UI.
|
||||
/// You can find more information about the MVVM pattern in <see cref="http://en.wikipedia.org/wiki/Model_View_ViewModel"/>.
|
||||
/// </remarks>
|
||||
internal abstract class PushPatternViewModel : ViewModelBase
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <value>Indicates if current pattern is active or not.</value>
|
||||
private bool _isActive;
|
||||
|
||||
/// <value>Collection of tile image relative uri's available in the phone application.</value>
|
||||
private Uri[] _tileImages;
|
||||
|
||||
/// <value>Selected tile background image uri.</value>
|
||||
private Uri _backgroundImageUri;
|
||||
|
||||
/// <value>Tile message count.</value>
|
||||
private int _count;
|
||||
|
||||
/// <value>Tile message title.</value>
|
||||
private string _title;
|
||||
|
||||
/// <value>Toast message title.</value>
|
||||
private string _toastTitle;
|
||||
|
||||
/// <value>Toast message sub title.</value>
|
||||
private string _toastSubTitle;
|
||||
|
||||
/// <value>Raw text message.</value>
|
||||
private string _rawMessage;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
[Import]
|
||||
protected PushService PushService { get; private set; }
|
||||
|
||||
[Import]
|
||||
private IMessageSendResultLogger ResultLogger { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets current pattern display name.
|
||||
/// </summary>
|
||||
public string DisplayName { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets current pattern description text.
|
||||
/// </summary>
|
||||
public string Description { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets value for indicating whether this pattern is active or not.
|
||||
/// </summary>
|
||||
public bool IsActive
|
||||
{
|
||||
get
|
||||
{
|
||||
return _isActive;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if (_isActive != value)
|
||||
{
|
||||
_isActive = value;
|
||||
if (_isActive)
|
||||
{
|
||||
OnActivated();
|
||||
}
|
||||
else
|
||||
{
|
||||
OnDeactivated();
|
||||
}
|
||||
|
||||
NotifyPropertyChanged("IsActive");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of tile image uris available in the phone client application.
|
||||
/// </summary>
|
||||
public Uri[] TileImages
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_tileImages == null)
|
||||
{
|
||||
_tileImages = new Uri[]
|
||||
{
|
||||
ToUri("TileBackground1.jpg"),
|
||||
ToUri("TileBackground2.jpg"),
|
||||
ToUri("TileBackground3.jpg"),
|
||||
};
|
||||
|
||||
BackgroundImageUri = _tileImages[0];
|
||||
}
|
||||
|
||||
return _tileImages;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the tile background uri.
|
||||
/// </summary>
|
||||
public Uri BackgroundImageUri
|
||||
{
|
||||
get { return _backgroundImageUri; }
|
||||
|
||||
set
|
||||
{
|
||||
if (_backgroundImageUri != value)
|
||||
{
|
||||
_backgroundImageUri = value;
|
||||
NotifyPropertyChanged("BackgroundImageUri");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the tile count.
|
||||
/// </summary>
|
||||
public int Count
|
||||
{
|
||||
get { return _count; }
|
||||
|
||||
set
|
||||
{
|
||||
if (_count != value)
|
||||
{
|
||||
_count = value;
|
||||
NotifyPropertyChanged("Count");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the tile tiltle.
|
||||
/// </summary>
|
||||
public string Title
|
||||
{
|
||||
get { return _title; }
|
||||
|
||||
set
|
||||
{
|
||||
if (_title != value)
|
||||
{
|
||||
_title = value;
|
||||
NotifyPropertyChanged("Title");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the toast title.
|
||||
/// </summary>
|
||||
public string ToastTitle
|
||||
{
|
||||
get { return _toastTitle; }
|
||||
|
||||
set
|
||||
{
|
||||
if (_toastTitle != value)
|
||||
{
|
||||
_toastTitle = value;
|
||||
NotifyPropertyChanged("ToastTitle");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the toast sub-title.
|
||||
/// </summary>
|
||||
public string ToastSubTitle
|
||||
{
|
||||
get { return _toastSubTitle; }
|
||||
|
||||
set
|
||||
{
|
||||
if (_toastSubTitle != value)
|
||||
{
|
||||
_toastSubTitle = value;
|
||||
NotifyPropertyChanged("ToastSubTitle");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the raw text message.
|
||||
/// </summary>
|
||||
public string RawMessage
|
||||
{
|
||||
get { return _rawMessage; }
|
||||
|
||||
set
|
||||
{
|
||||
if (_rawMessage != value)
|
||||
{
|
||||
_rawMessage = value;
|
||||
NotifyPropertyChanged("RawMessage");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Commands
|
||||
|
||||
/// <summary>
|
||||
/// Gets the command which executes the send operation.
|
||||
/// </summary>
|
||||
public ICommand SendCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
return new RelayCommand(p =>
|
||||
{
|
||||
try
|
||||
{
|
||||
OnSend();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Send Error");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected
|
||||
|
||||
/// <summary>
|
||||
/// Override this to send push message according to the pattern behavior.
|
||||
/// </summary>
|
||||
protected abstract void OnSend();
|
||||
|
||||
/// <summary>
|
||||
/// Override this to add additional pattern-activation logic.
|
||||
/// </summary>
|
||||
protected virtual void OnActivated()
|
||||
{
|
||||
UpdateClient();
|
||||
|
||||
PushService.Subscribed += PushService_Subscribed;
|
||||
PushService.GetInfo += PushService_GetInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override this to add additional pattern-deactivation logic.
|
||||
/// </summary>
|
||||
protected virtual void OnDeactivated()
|
||||
{
|
||||
PushService.Subscribed -= PushService_Subscribed;
|
||||
PushService.GetInfo -= PushService_GetInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override this to add logic when clients login.
|
||||
/// </summary>
|
||||
protected virtual void OnSubscribed(SubscriptionEventArgs args)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override this to add logic when clients request server's info.
|
||||
/// </summary>
|
||||
protected virtual void OnGetInfo(ServerInfoEventArgs args)
|
||||
{
|
||||
args.ServerInfo = new ServerInfo
|
||||
{
|
||||
PushPattern = DisplayName,
|
||||
Counter = Count
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs push message result.
|
||||
/// </summary>
|
||||
protected void Log(MessageSendResult result)
|
||||
{
|
||||
ResultLogger.Log(DisplayName, result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs push message error.
|
||||
/// </summary>
|
||||
protected void Log(MessageSendException exception)
|
||||
{
|
||||
ResultLogger.Log(DisplayName, exception);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event Handlers
|
||||
|
||||
private void UpdateClient()
|
||||
{
|
||||
// Notify subscribers to get new info from the server.
|
||||
foreach (var subscriber in PushService.Subscribers)
|
||||
{
|
||||
new RawPushNotificationMessage(MessageSendPriority.High)
|
||||
{
|
||||
RawData = Encoding.ASCII.GetBytes("Update Info")
|
||||
}.SendAsync(subscriber.ChannelUri);
|
||||
}
|
||||
}
|
||||
|
||||
private void PushService_Subscribed(object sender, SubscriptionEventArgs args)
|
||||
{
|
||||
OnSubscribed(args);
|
||||
}
|
||||
|
||||
private void PushService_GetInfo(object sender, ServerInfoEventArgs args)
|
||||
{
|
||||
OnGetInfo(args);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helpers
|
||||
|
||||
private static Uri ToUri(string imageName)
|
||||
{
|
||||
return new Uri(string.Format("/Resources/TileImages/{0}", imageName), UriKind.Relative);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.Windows.Input;
|
||||
using System.IO;
|
||||
using Microsoft.Win32;
|
||||
|
||||
using WindowsPhone.Recipes.Push.Messasges;
|
||||
using WindowsPhone.Recipes.Push.Server.Models;
|
||||
using WindowsPhone.Recipes.Push.Server.Services;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the Tile Schedule push notification pattern.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This push pattern is passive. The user schedule a tile image update request,
|
||||
/// by using the Windows Phone API, and at the time, MPNS fetches the image using
|
||||
/// the uri provided with the request.
|
||||
/// </remarks>
|
||||
[Export(typeof(PushPatternViewModel)), PartCreationPolicy(CreationPolicy.Shared)]
|
||||
internal sealed class TileSchedulePushPatternViewModel : PushPatternViewModel
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private string _imageFileName;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
[Import]
|
||||
private ImageService ImageService { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a image file name sent by the user.
|
||||
/// </summary>
|
||||
public string ImageFileName
|
||||
{
|
||||
get { return _imageFileName; }
|
||||
|
||||
set
|
||||
{
|
||||
if (_imageFileName != value)
|
||||
{
|
||||
_imageFileName = value;
|
||||
NotifyPropertyChanged("ImageFileName");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ctor
|
||||
|
||||
/// <summary>
|
||||
/// Initialize new instance of this type with defaults.
|
||||
/// </summary>
|
||||
public TileSchedulePushPatternViewModel()
|
||||
{
|
||||
InitializeDefaults();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Overrides
|
||||
|
||||
protected override void OnActivated()
|
||||
{
|
||||
base.OnActivated();
|
||||
|
||||
// Register to the PushService.TileUpdateRequest event. This event is raised
|
||||
// whenever a user asks to update its tile.
|
||||
PushService.TileUpdateRequest += PushService_TileUpdateRequest;
|
||||
|
||||
// Register to the ImageService.ImageRequest event. This event is raised
|
||||
// whenever ImageService.GetTileImage is called.
|
||||
ImageService.ImageRequest += Service_ImageRequest;
|
||||
}
|
||||
|
||||
protected override void OnDeactivated()
|
||||
{
|
||||
base.OnDeactivated();
|
||||
|
||||
PushService.TileUpdateRequest -= PushService_TileUpdateRequest;
|
||||
ImageService.ImageRequest -= Service_ImageRequest;
|
||||
}
|
||||
|
||||
protected override void OnSend()
|
||||
{
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Privates
|
||||
|
||||
private void PushService_TileUpdateRequest(object sender, TileUpdateRequestEventArgs e)
|
||||
{
|
||||
// Send a tile notification message to the relevant device.
|
||||
var tileMsg = new TilePushNotificationMessage(MessageSendPriority.High)
|
||||
{
|
||||
BackgroundImageUri = new Uri(string.Format(ImageService.GetTileImageService, e.Parameter))
|
||||
};
|
||||
|
||||
tileMsg.SendAsync(e.ChannelUri, Log, Log);
|
||||
}
|
||||
|
||||
private void Service_ImageRequest(object sender, Services.ImageRequestEventArgs e)
|
||||
{
|
||||
ImageFileName = e.Parameter;
|
||||
|
||||
// This event is raised by our local push-service as result of
|
||||
// the tile msg we've sent to a subscriber. This is the time
|
||||
// to pick the right tile image for the subscriber.
|
||||
string imageFile = Path.Combine("Resources/TileImages/Numbers", e.Parameter);
|
||||
if (File.Exists(imageFile))
|
||||
{
|
||||
using (var reader = File.OpenRead(imageFile))
|
||||
{
|
||||
byte[] imageBuffer = new byte[reader.Length];
|
||||
int bytesRead = reader.Read(imageBuffer, 0, imageBuffer.Length);
|
||||
e.ImageStream.Write(imageBuffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeDefaults()
|
||||
{
|
||||
DisplayName = "Tile Schedule";
|
||||
Description = "This push pattern is passive. The user schedule a tile image update request, by using the Windows Phone API, and at the time, MPNS fetches the image using the uri provided with the request.";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user