mirror of
https://github.com/farcasclaudiu/myfriendsaround.git
synced 2026-06-29 09:01:55 +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:
+26
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.ServiceModel;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.ServiceModel.Web;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides custom tile images.
|
||||
/// </summary>
|
||||
[ServiceContract]
|
||||
public interface IImageService
|
||||
{
|
||||
/// <summary>
|
||||
/// Get custom tile image.
|
||||
/// </summary>
|
||||
/// <param name="parameter">The tile image request parameter.</param>
|
||||
/// <returns>Custom tile image stream.</returns>
|
||||
[OperationContract, WebGet]
|
||||
Stream GetTileImage(string parameter);
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
using System.IO;
|
||||
using System.ServiceModel;
|
||||
using System.ServiceModel.Web;
|
||||
using System.Xml.Linq;
|
||||
using System;
|
||||
|
||||
using WindowsPhone.Recipes.Push.Server.Models;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Push server services.
|
||||
/// </summary>
|
||||
[ServiceContract]
|
||||
public interface IPushService
|
||||
{
|
||||
/// <summary>
|
||||
/// Register user name with a push channel uri.
|
||||
/// </summary>
|
||||
/// <param name="userName">User name.</param>
|
||||
/// <param name="channelUri">Push notification channel uri.</param>
|
||||
[OperationContract]
|
||||
void Register(string userName, Uri channelUri);
|
||||
|
||||
/// <summary>
|
||||
/// Get current server information/status.
|
||||
/// </summary>
|
||||
/// <returns>Current server status.</returns>
|
||||
[OperationContract]
|
||||
ServerInfo GetServerInfo();
|
||||
|
||||
/// <summary>
|
||||
/// Send a tile update with given parameter.
|
||||
/// </summary>
|
||||
/// <returns>User parameter to send with the tile update request.</returns>
|
||||
[OperationContract]
|
||||
void UpdateTile(Uri channelUri, string parameter);
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Image request event arguments.
|
||||
/// </summary>
|
||||
internal class ImageRequestEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Tile image maximum size in bytes.
|
||||
/// </summary>
|
||||
public const int MaxTileImageSize = 80 * 1024; // The max tile image size is 80k.
|
||||
|
||||
public string Parameter { get; private set; }
|
||||
public Stream ImageStream { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance with a memory stream maximum size of <see cref="ImageRequestEventArgs.MaxTileImageSize"/>.
|
||||
/// </summary>
|
||||
public ImageRequestEventArgs(string parameter)
|
||||
{
|
||||
Parameter = parameter;
|
||||
ImageStream = new MemoryStream(MaxTileImageSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.ServiceModel;
|
||||
using System.Text;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.IO;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a tile image REST service.
|
||||
/// </summary>
|
||||
[Export, PartCreationPolicy(CreationPolicy.Shared), ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
|
||||
internal class ImageService : IImageService
|
||||
{
|
||||
#region Constants
|
||||
|
||||
/// <value>Url of the GetTileImage REST service.</value>
|
||||
public const string GetTileImageService = "http://localhost:8000/ImageService/GetTileImage?parameter={0}";
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
|
||||
private ServiceHost _serviceHost;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Raise when dynamic image creation is requested.
|
||||
/// </summary>
|
||||
public event EventHandler<ImageRequestEventArgs> ImageRequest;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Operations
|
||||
|
||||
/// <summary>
|
||||
/// Host this service using WCF.
|
||||
/// </summary>
|
||||
public void Host()
|
||||
{
|
||||
_serviceHost = new ServiceHost(this);
|
||||
_serviceHost.Open();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a generated custom tile image stream for the given uri.
|
||||
/// </summary>
|
||||
/// <param name="parameter">The tile image request parameter.</param>
|
||||
/// <returns>A stream of the custom tile image generated.</returns>
|
||||
public Stream GetTileImage(string parameter)
|
||||
{
|
||||
if (ImageRequest != null)
|
||||
{
|
||||
var args = new ImageRequestEventArgs(parameter);
|
||||
ImageRequest(this, args);
|
||||
|
||||
// Seek the stream back to the begining just in case.
|
||||
args.ImageStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
return args.ImageStream;
|
||||
}
|
||||
|
||||
return Stream.Null;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Privates Logic
|
||||
|
||||
private void OnImageRequest(ImageRequestEventArgs args)
|
||||
{
|
||||
if (ImageRequest != null)
|
||||
{
|
||||
ImageRequest(this, args);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+184
@@ -0,0 +1,184 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.ServiceModel;
|
||||
using System.ComponentModel.Composition;
|
||||
|
||||
using WindowsPhone.Recipes.Push.Server.Models;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Current push server services implementation.
|
||||
/// </summary>
|
||||
[Export, PartCreationPolicy(CreationPolicy.Shared), ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
|
||||
internal class PushService : IPushService
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private ServiceHost _serviceHost;
|
||||
|
||||
/// <value>Dictionary contains users subscription objects.</value>
|
||||
private readonly Dictionary<string, Subscription> _subscribers = new Dictionary<string, Subscription>();
|
||||
|
||||
/// <value>Sync access to the subscribers dictionary.</value>
|
||||
private readonly object SubscribersSync = new object();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Raise when user subscribed.
|
||||
/// </summary>
|
||||
public event EventHandler<SubscriptionEventArgs> Subscribed;
|
||||
|
||||
/// <summary>
|
||||
/// Raise when current server status is requested.
|
||||
/// </summary>
|
||||
public event EventHandler<ServerInfoEventArgs> GetInfo;
|
||||
|
||||
/// <summary>
|
||||
/// Raise when user requests a tile update.
|
||||
/// </summary>
|
||||
public event EventHandler<TileUpdateRequestEventArgs> TileUpdateRequest;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Get subscription list.
|
||||
/// </summary>
|
||||
public IEnumerable<Subscription> Subscribers
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_subscribers)
|
||||
{
|
||||
var subscribers = new Subscription[_subscribers.Count];
|
||||
_subscribers.Values.CopyTo(subscribers, 0);
|
||||
return subscribers;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get subscription count.
|
||||
/// </summary>
|
||||
public int SubscribersCount
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_subscribers)
|
||||
{
|
||||
return _subscribers.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Operations
|
||||
|
||||
/// <summary>
|
||||
/// Register user with notification channel uri.
|
||||
/// </summary>
|
||||
/// <param name="userName">The user name to register.</param>
|
||||
/// <param name="channelUri">The notification channel uri.</param>
|
||||
public void Register(string userName, Uri channelUri)
|
||||
{
|
||||
if (string.IsNullOrEmpty(userName))
|
||||
{
|
||||
throw new ArgumentException("Invalid user name", "userName");
|
||||
}
|
||||
|
||||
if (channelUri == null)
|
||||
{
|
||||
throw new ArgumentNullException("channelUri");
|
||||
}
|
||||
|
||||
var subscription = new Subscription(userName, channelUri);
|
||||
|
||||
lock (SubscribersSync)
|
||||
{
|
||||
// Add or update existing.
|
||||
_subscribers[userName] = subscription;
|
||||
}
|
||||
|
||||
OnSubscribed(new SubscriptionEventArgs(subscription));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets current server info.
|
||||
/// </summary>
|
||||
/// <returns>An instance info object contains server status.</returns>
|
||||
public ServerInfo GetServerInfo()
|
||||
{
|
||||
var args = new ServerInfoEventArgs();
|
||||
OnGetInfo(args);
|
||||
return args.ServerInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a tile update with given parameter.
|
||||
/// </summary>
|
||||
/// <param name="channelUri">An instance info object contains server status.</param>
|
||||
/// <param name="parameter">User parameter to send with the tile update request.</param>
|
||||
public void UpdateTile(Uri channelUri, string parameter)
|
||||
{
|
||||
OnTileUpdateRequest(new TileUpdateRequestEventArgs(channelUri, parameter));
|
||||
}
|
||||
|
||||
internal Subscription TryGetSubscription(string userName)
|
||||
{
|
||||
lock (SubscribersSync)
|
||||
{
|
||||
Subscription subscription;
|
||||
if (!_subscribers.TryGetValue(userName, out subscription))
|
||||
{
|
||||
subscription = null;
|
||||
}
|
||||
|
||||
return subscription;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Privates Logic
|
||||
|
||||
public void Host()
|
||||
{
|
||||
_serviceHost = new ServiceHost(this);
|
||||
_serviceHost.Open();
|
||||
}
|
||||
|
||||
private void OnSubscribed(SubscriptionEventArgs args)
|
||||
{
|
||||
if (Subscribed != null)
|
||||
{
|
||||
Subscribed(this, args);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnGetInfo(ServerInfoEventArgs args)
|
||||
{
|
||||
if (GetInfo != null)
|
||||
{
|
||||
GetInfo(this, args);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTileUpdateRequest(TileUpdateRequestEventArgs args)
|
||||
{
|
||||
if (TileUpdateRequest != null)
|
||||
{
|
||||
TileUpdateRequest(this, args);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using WindowsPhone.Recipes.Push.Server.Models;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Server info event arguments.
|
||||
/// </summary>
|
||||
internal class ServerInfoEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the server info.
|
||||
/// </summary>
|
||||
public ServerInfo ServerInfo { get; set; }
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using WindowsPhone.Recipes.Push.Server.Models;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Subscription event arguments.
|
||||
/// </summary>
|
||||
internal class SubscriptionEventArgs : EventArgs
|
||||
{
|
||||
public Subscription Subscription { get; private set; }
|
||||
|
||||
public SubscriptionEventArgs(Subscription subscription)
|
||||
{
|
||||
Subscription = subscription;
|
||||
}
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace WindowsPhone.Recipes.Push.Server.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Tile update request event arguments.
|
||||
/// </summary>
|
||||
internal class TileUpdateRequestEventArgs : EventArgs
|
||||
{
|
||||
public Uri ChannelUri { get; private set; }
|
||||
|
||||
public string Parameter { get; private set; }
|
||||
|
||||
public TileUpdateRequestEventArgs(Uri channelUri, string parameter)
|
||||
{
|
||||
this.ChannelUri = channelUri;
|
||||
this.Parameter = parameter;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user